When would you use let
vs an anonymous function?
could you write an example comparison?
No...
in Scheme, let
expands to an anonymous function; they're equivalent for all intents and purposes
let
is just easier to read and write
and i guess maybe faster in Clojure
Thank you 🙂 I was just reading about anonymous functions and couldn't figure out when it would be preferable to use them. But maybe I should have played with them a bit more first, seen if they really do react the same.
It's counter-intuitive, but not naming things is often the clearer and simpler approach. Names mean that the programmer trying to understand the code later has to meet all those people, so to speak. Also, naming things right is hard and every name is an opportunity to mis-name something, and therefore introduce confusion between the name and what it actually does. (Intermediate reading on this: https://en.wikipedia.org/wiki/Tacit_programming ) So most Clojurists find that if a function only gets used in one place, then it's better not to name it or its arguments (which means using the #(foo %)
syntax for anonymous functions). Sometimes the middle ground has the most clarity: leave the function anonymous but name its arguments (which means using the (fn [bar] (...))
anonymous function syntax).
That’s really helpful, thank you!
Never preferable?
given the choice...yes, never
anonymous functions are useful for many more things than what let
does
a typical example is...passing them to filter
, remove
, map
...
Thank you!