Way back when I started with Clojure i was doing this:
(let [constant-data (something-expensive)]
(defn my-fn [x]
(do-something-with x constant-data)))
But was advised instead to do this:
(def my-fn
(let [constant-data (something-expensive)]
(fn [x]
(do-something-with x
Doug Hoyte's book Let Over Lambda—50 Years of Lisp devotes its very title
to the technique of (let...(fn...)).
On Friday, March 22, 2013 2:59:43 PM UTC-4, jamieorc wrote:
Curious which style is preferred in Clojure and why:
(defn f1 []
(let [x {:foo 1 :bar 2 :baz 3}]
(keys x)))
On Sunday, March 24, 2013 10:53:28 AM UTC+1, bsmith.occs wrote:
Way back when I started with Clojure i was doing this:
(let [constant-data (something-expensive)]
(defn my-fn [x]
(do-something-with x constant-data)))
But was advised instead to do this:
(def my-fn
(let
What if there's some computation in there, but such that should be
performed at compile time? I still prefer the outside let whenever I want
to make dead sure it's not getting reallocated on each call. If there was
some well-specified and easily understood guarantee (for example, like the
one
Just out of curiosity, does it have to be a function?
(def data {:foo 1 :bar 2 :baz 3})
(def data-keys (keys data))
If one item is constant the other probably is too?
Cheers,
/thomas
On Friday, March 22, 2013 7:59:43 PM UTC+1, jamieorc wrote:
Curious which style is preferred in Clojure and
On Sat, Mar 23, 2013 at 4:16 AM, Thomas Heller th.hel...@gmail.com wrote:
Just out of curiosity, does it have to be a function?
(def data {:foo 1 :bar 2 :baz 3})
(def data-keys (keys data))
If one item is constant the other probably is too?
In this case, yes, but it's easy to imagine cases
def/defn et. al are top-level form definitions...very rarely (I'd say
never) you'd have a def/defn inside a 'let' or inside anything for that
matter...The 1st one looks good :)
Jim
On 22/03/13 18:59, jamieorc wrote:
Curious which style is preferred in Clojure and why:
(defn f1 []
(let [x
2013/3/22 jamieorc jamie...@gmail.com
Curious which style is preferred in Clojure and why:
(defn f1 []
(let [x {:foo 1 :bar 2 :baz 3}]
(keys x)))
(let [x {:foo 1 :bar 2 :baz 3}]
(defn f2 []
(keys x)))
In either case, AFAIK, the compiler will recognize {:foo 1 :bar 2 :baz 3}
Thanks, that's what I expected, especially after doing some (time... )
experiments.
On Friday, March 22, 2013 3:05:10 PM UTC-4, Laurent PETIT wrote:
2013/3/22 jamieorc jami...@gmail.com javascript:
Curious which style is preferred in Clojure and why:
(defn f1 []
(let [x {:foo 1 :bar
For the example given, I would say it depends on what you are trying to
express.
The function f1 is a function that needs some internal data x to operate
-x might be considered an implementation detail.
The function f2 operates on well known data x -x might be considered
configuration of
I've certainly seen this at least a few spots within the 4clojure codebase –
https://github.com/4clojure/4clojure/blob/develop/src/foreclojure/utils.clj#L66-L70
(quick example, I believe there are more)
On Friday, March 22, 2013 3:02:20 PM UTC-4, Jim foo.bar wrote:
def/defn et. al are
The question should probably be asked: is there a benefit in a given
situation to having the let be outside the scope of the defn? I would argue
that most times it is not, and putting the let outside the function
clutters the code and makes it harder to see the functions defined in the
namespace.
12 matches
Mail list logo