4 ms·
How large are these s-exp ? Lisp is unreadable when nested too much (I'm starring at an xml->s-exp dump and I don't even wanna try to read it), but usually lisp
by agumonkey 13y ago
How large are these s-exp ? Lisp is unreadable when nested too much (I'm starring at an xml->s-exp dump and I don't even wanna try to read it), but usually lisp code and data tend to be factored into little combinators and separated specific functions. If you can, try to split them.
- dpritchett 13y agoHere's a link to an example of that coping strategy from some bad Clojure I wrote last night [1]. I sometimes use an accumulation of lets for a similar effect, but only when describing simpler transformations. The lets might look like so (warning- silly example): (defn n-squared-over-two-plus-three-as-str [n] (let [squared (* n n) halved (/ squared 2.0) added (+ halved 3)] (str added))) > (n-squared-over-two-plus-three-as-str 1) "3.5" Sure, I could've just done this as a deeply-nested structure: > ((fn [n] (str (+ 3 (#(/ % 2.0) (* n n))))) 1) "3.5" ... but it's harder to read, debug, and reason about. You wouldn't do that in a non-functional language either! I suppose the tolerance for deep nesting is different for different programmers. [1] https://github.com/dpritchett/cloball/blob/62300d31666ab12613f82e929fd11fcacb9f4007/src/cloball/core.clj#L38 https://github.com/dpritchett/cloball/blob/62300d31666ab1261...
- agumonkey 13y agoThanks for the edition, the code wasn't as obvious. I believe Haskell `where` construct is to write code this way (beside the flipped order).
- minikomi 13y agoThat looks like a good place to use the arrow macro http://clojuredocs.org/clojure_core/clojure.core/-%3E http://clojuredocs.org/clojure_core/clojure.core/-%3E
- dpritchett 13y agoYep, that's what the link in my original post was hoping to demonstrate. I find rolling up a few lets one after the other more natural in some other situations.
- dpritchett 13y agoAlthough I'm second-guessing my let example now. Lets are great for assigning names to your building blocks, but I don't really like using them to obscure my processing flow as above. Perhaps this would be a better compromise: (defn example [n] (let [square #(* % %) halve #(/ % 2.0) add3 #(+ % 3)] (-> n square halve add3 str))) > (example 1) "3.5"
- minikomi 13y agoIf the function name or docstring is descriptive enough, I'd just go for (defn square-halve-add3-to-string [n] (-> n (#(* % %)) (/ 2.0) (+ 3) str))