Re: LoL which style for Clojure

2013-03-24 Thread Ben Smith-Mannschott
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

Re: LoL which style for Clojure

2013-03-24 Thread Matching Socks
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)))

Re: LoL which style for Clojure

2013-03-24 Thread Marko Topolnik
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

Re: LoL which style for Clojure

2013-03-23 Thread Marko Topolnik
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

Re: LoL which style for Clojure

2013-03-23 Thread Thomas Heller
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

Re: LoL which style for Clojure

2013-03-23 Thread Ben Wolfson
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

Re: LoL which style for Clojure

2013-03-22 Thread Jim - FooBar();
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

Re: LoL which style for Clojure

2013-03-22 Thread Laurent PETIT
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}

Re: LoL which style for Clojure

2013-03-22 Thread jamieorc
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

Re: LoL which style for Clojure

2013-03-22 Thread Chris Hapgood
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

Re: LoL which style for Clojure

2013-03-22 Thread Robert Pitts
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

Re: LoL which style for Clojure

2013-03-22 Thread Timothy Baldridge
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.