Replacing nested let statements with assignments

2012-10-18 Thread JvJ
I'm not sure if anyone's already done this, but I recently got tired of writing code that looked like this: (let [a 1] (ns cljutils.core) (defn- form-check Ensures the form represents an assignment. Such as (:= a 1) [form] (and (= 3 (count form)) (= := (first form)) (symbol?

Replacing nested let statements with assignments

2012-10-18 Thread JvJ
I'm not sure if anyone's done this before, but I'm fed up with writing code that looks like this: (let [a 1] (println this is a: a) (let [b 2] (println this is b: b) (let [c 3] (println this is c: c) (+ a b c I'd rather do something more like

Re: Replacing nested let statements with assignments

2012-10-18 Thread David Nolen
On Thu, Oct 18, 2012 at 12:01 PM, JvJ kfjwhee...@gmail.com wrote: I'm not sure if anyone's done this before, but I'm fed up with writing code that looks like this: What problem does this solve given you can do the following? (let [a 1 _ (println a) b 2 _ (println b) c

Re: Replacing nested let statements with assignments

2012-10-18 Thread keeds
I'm confused. How does the following not work? (let [a 1 b 2 c 3] (println a) (println b) (println c) (+ a b c)) On Thursday, October 18, 2012 5:01:33 PM UTC+1, JvJ wrote: I'm not sure if anyone's done this before, but I'm fed up with writing code that looks like this: (let [a 1]

Re: Replacing nested let statements with assignments

2012-10-18 Thread Ben Wolfson
On Thu, Oct 18, 2012 at 9:12 AM, keeds akee...@gmail.com wrote: I'm confused. How does the following not work? (let [a 1 b 2 c 3] (println a) (println b) (println c) (+ a b c)) It works, but all of the expressions on the RHS of the let expression's binding vector have to be applied

Re: Replacing nested let statements with assignments

2012-10-18 Thread JvJ
I didn't realize you could bind to empty identifiers like that. Alright, that makes more sense. I figured I was missing something. On Thursday, 18 October 2012 12:11:49 UTC-4, David Nolen wrote: On Thu, Oct 18, 2012 at 12:01 PM, JvJ kfjwh...@gmail.com javascript:wrote: I'm not sure if

Re: Replacing nested let statements with assignments

2012-10-18 Thread JvJ
Exactly. A big part of the reason was that I needed to do things between when other variables were initialized. On Thursday, 18 October 2012 12:17:17 UTC-4, Ben wrote: On Thu, Oct 18, 2012 at 9:12 AM, keeds ake...@gmail.com javascript: wrote: I'm confused. How does the following not

Re: Replacing nested let statements with assignments

2012-10-18 Thread David Nolen
On Thu, Oct 18, 2012 at 12:23 PM, JvJ kfjwhee...@gmail.com wrote: I didn't realize you could bind to empty identifiers like that. Alright, that makes more sense. I figured I was missing something. Just to be clear _ has not special meaning beyond convention. I could have used x but that

Re: Replacing nested let statements with assignments

2012-10-18 Thread Ambrose Bonnaire-Sergeant
There's nothing special going on, no empty identifiers. It's just a common convention to use _ when uninterested in the return value. (let [_ 1] _) ;= 1 Pretty evil to actually use bindings called _ though :) Thanks, Ambrose On Fri, Oct 19, 2012 at 12:23 AM, JvJ kfjwhee...@gmail.com wrote:

Re: Replacing nested let statements with assignments

2012-10-18 Thread AtKaaZ
Thank you for this clarification! On Thu, Oct 18, 2012 at 6:26 PM, David Nolen dnolen.li...@gmail.com wrote: On Thu, Oct 18, 2012 at 12:23 PM, JvJ kfjwhee...@gmail.com wrote: I didn't realize you could bind to empty identifiers like that. Alright, that makes more sense. I figured I was

Re: Replacing nested let statements with assignments

2012-10-18 Thread Evan Gamble
For the situation where the lets are nested because you're checking the values in some way after each binding, I wrote a macro called let?. I find it very useful and use it in nearly all my code. https://github.com/egamble/let-else -- You received this message because you are subscribed to

Re: Replacing nested let statements with assignments

2012-10-18 Thread Grant Rettke
On Thu, Oct 18, 2012 at 11:11 AM, David Nolen dnolen.li...@gmail.com wrote: On Thu, Oct 18, 2012 at 12:01 PM, JvJ kfjwhee...@gmail.com wrote: I'm not sure if anyone's done this before, but I'm fed up with writing code that looks like this: What problem does this solve given you can do the

Re: Replacing nested let statements with assignments

2012-10-18 Thread Alan Malloy
It's rare to get tired of this, because nobody does it: it's not common because your interleaved statements are side-effecting only, which is not encouraged in Clojure, and rarely needed. Certainly sometimes it's the best way to do something, but not so often that I'd become frustrated; if

Re: Replacing nested let statements with assignments

2012-10-18 Thread Grant Rettke
On Thu, Oct 18, 2012 at 1:55 PM, Alan Malloy a...@malloys.org wrote: It's rare to get tired of this, because nobody does it: it's not common because your interleaved statements are side-effecting only, which is not encouraged in Clojure, and rarely needed. Certainly sometimes it's the best way

Re: Replacing nested let statements with assignments

2012-10-18 Thread David Nolen
On Thu, Oct 18, 2012 at 2:55 PM, Alan Malloy a...@malloys.org wrote: It's rare to get tired of this, because nobody does it: it's not common because your interleaved statements are side-effecting only, which is not encouraged in Clojure, and rarely needed. Certainly sometimes it's the best

Re: Replacing nested let statements with assignments

2012-10-18 Thread Grant Rettke
On Thu, Oct 18, 2012 at 1:32 PM, Grant Rettke gret...@acm.org wrote: On Thu, Oct 18, 2012 at 11:11 AM, David Nolen dnolen.li...@gmail.com wrote: On Thu, Oct 18, 2012 at 12:01 PM, JvJ kfjwhee...@gmail.com wrote: I'm not sure if anyone's done this before, but I'm fed up with writing code that

Re: Replacing nested let statements with assignments

2012-10-18 Thread David Nolen
On Thu, Oct 18, 2012 at 3:06 PM, Grant Rettke gret...@acm.org wrote: (- ((fn [] (let [a 1] (println this is a: a) a))) ((fn [a] (let [b 2] (println this is b: b) (list a b ((fn [[a b]] (let [c 3] (println this is c:

Re: Replacing nested let statements with assignments

2012-10-18 Thread Ben Wolfson
On Thu, Oct 18, 2012 at 12:01 PM, Grant Rettke gret...@acm.org wrote: On Thu, Oct 18, 2012 at 1:55 PM, Alan Malloy a...@malloys.org wrote: It's rare to get tired of this, because nobody does it: it's not common because your interleaved statements are side-effecting only, which is not

Re: Replacing nested let statements with assignments

2012-10-18 Thread Grant Rettke
On Thu, Oct 18, 2012 at 2:07 PM, David Nolen dnolen.li...@gmail.com wrote: On Thu, Oct 18, 2012 at 3:06 PM, Grant Rettke gret...@acm.org wrote: (- ((fn [] (let [a 1] (println this is a: a) a))) ((fn [a] (let [b 2] (println this is b: b)

Re: Replacing nested let statements with assignments

2012-10-18 Thread Grant Rettke
On Thu, Oct 18, 2012 at 2:09 PM, Ben Wolfson wolf...@gmail.com wrote: On Thu, Oct 18, 2012 at 12:01 PM, Grant Rettke gret...@acm.org wrote: It isn't side effecting it is sequencing. Clojure's let is already sequential, like Scheme's let*: The bindings are sequential, so each binding can see

Re: Replacing nested let statements with assignments

2012-10-18 Thread Alan Malloy
On Oct 18, 12:02 pm, David Nolen dnolen.li...@gmail.com wrote: On Thu, Oct 18, 2012 at 2:55 PM, Alan Malloy a...@malloys.org wrote: It's rare to get tired of this, because nobody does it: it's not common because your interleaved statements are side-effecting only, which is not encouraged in

Re: Replacing nested let statements with assignments

2012-10-18 Thread Mark Engelberg
When I want to add print commands for debugging, I usually either do it the way David Nolen described, i.e., binding _ to a printf statement, or I use a little utility macro like this (picked up from stackoverflow): (defmacro dbg[x] `(let [x# ~x] (println dbg: '~x = x#) x#)) I agree with Evan

Re: Replacing nested let statements with assignments

2012-10-18 Thread Grant Rettke
On Thu, Oct 18, 2012 at 2:50 PM, Mark Engelberg mark.engelb...@gmail.com wrote: Either way works well. I think Evan's way results in somewhat more compact code for the common case, whereas Cgrand's way feels a little more versatile (and his flatter cond is what I use). I strongly urge you to

Re: Replacing nested let statements with assignments

2012-10-18 Thread David Nolen
On Thu, Oct 18, 2012 at 4:15 PM, Grant Rettke gret...@acm.org wrote: On Thu, Oct 18, 2012 at 2:50 PM, Mark Engelberg mark.engelb...@gmail.com wrote: Either way works well. I think Evan's way results in somewhat more compact code for the common case, whereas Cgrand's way feels a little

Re: Replacing nested let statements with assignments

2012-10-18 Thread Grant Rettke
On Thu, Oct 18, 2012 at 3:17 PM, David Nolen dnolen.li...@gmail.com wrote: On Thu, Oct 18, 2012 at 4:15 PM, Grant Rettke gret...@acm.org wrote: On Thu, Oct 18, 2012 at 2:50 PM, Mark Engelberg mark.engelb...@gmail.com wrote: Either way works well. I think Evan's way results in somewhat more

Re: Replacing nested let statements with assignments

2012-10-18 Thread David Nolen
On Thu, Oct 18, 2012 at 4:22 PM, Grant Rettke gret...@acm.org wrote: When you use def inside a defn is it equivalent to a let binding like this? (defn foo [] (def a 1) (println a)) (defn foo [] ((fn [a] (println a)) 1)) Not equivalent. -- You received this message because

Re: Replacing nested let statements with assignments

2012-10-18 Thread Grant Rettke
On Thu, Oct 18, 2012 at 3:32 PM, David Nolen dnolen.li...@gmail.com wrote: On Thu, Oct 18, 2012 at 4:22 PM, Grant Rettke gret...@acm.org wrote: When you use def inside a defn is it equivalent to a let binding like this? (defn foo [] (def a 1) (println a)) (defn foo [] ((fn [a]

Re: Replacing nested let statements with assignments

2012-10-18 Thread Mark Engelberg
A def, even inside defn, creates and binds a global variable. -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your

Re: Replacing nested let statements with assignments

2012-10-18 Thread Grant Rettke
On Thu, Oct 18, 2012 at 3:42 PM, Mark Engelberg mark.engelb...@gmail.com wrote: A def, even inside defn, creates and binds a global variable. Woa, I see, thanks! -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to

Re: Replacing nested let statements with assignments

2012-10-18 Thread Grant Rettke
On Thu, Oct 18, 2012 at 3:45 PM, Grant Rettke gret...@acm.org wrote: On Thu, Oct 18, 2012 at 3:42 PM, Mark Engelberg mark.engelb...@gmail.com wrote: A def, even inside defn, creates and binds a global variable. Woa, I see, thanks! Anyone voted for internal define lately? -- You received

Re: Replacing nested let statements with assignments

2012-10-18 Thread David Nolen
On Thu, Oct 18, 2012 at 4:45 PM, Grant Rettke gret...@acm.org wrote: On Thu, Oct 18, 2012 at 3:45 PM, Grant Rettke gret...@acm.org wrote: On Thu, Oct 18, 2012 at 3:42 PM, Mark Engelberg mark.engelb...@gmail.com wrote: A def, even inside defn, creates and binds a global variable. Woa, I

Re: Replacing nested let statements with assignments

2012-10-18 Thread JvJ
Exactly. Not only debugging, but java interop that involved calling methods with side effects. On Thursday, 18 October 2012 15:02:47 UTC-4, David Nolen wrote: On Thu, Oct 18, 2012 at 2:55 PM, Alan Malloy al...@malloys.orgjavascript: wrote: It's rare to get tired of this, because nobody

Re: Replacing nested let statements with assignments

2012-10-18 Thread Grant Rettke
I figured you would use doto for that. On Thu, Oct 18, 2012 at 4:09 PM, JvJ kfjwhee...@gmail.com wrote: Exactly. Not only debugging, but java interop that involved calling methods with side effects. On Thursday, 18 October 2012 15:02:47 UTC-4, David Nolen wrote: On Thu, Oct 18, 2012 at

Re: Replacing nested let statements with assignments

2012-10-18 Thread Grant Rettke
On Thu, Oct 18, 2012 at 4:05 PM, David Nolen dnolen.li...@gmail.com wrote: On Thu, Oct 18, 2012 at 4:45 PM, Grant Rettke gret...@acm.org wrote: Anyone voted for internal define lately? At this point I think it's highly unlikely to change - the behavior is pretty well documented: I see. Just a

Re: Replacing nested let statements with assignments

2012-10-18 Thread JvJ
On a side note, I was partially inspired by Haskell's do notation, which is imperative-looking syntactic sugar for monadic binds. On Thursday, 18 October 2012 12:01:33 UTC-4, JvJ wrote: I'm not sure if anyone's done this before, but I'm fed up with writing code that looks like this: (let

Re: Replacing nested let statements with assignments

2012-10-18 Thread JvJ
On a side note, I was partially inspired by Haskell's do notation, which is imperative-looking syntactic sugar for monadic bind operators. -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com

Re: Replacing nested let statements with assignments

2012-10-18 Thread JvJ
Most of what could be accomplished by an internal define could be done with a let statement. But if you don't want to add the brackets, you can create your own function definition macro that converts defs to lets. On Thursday, 18 October 2012 17:12:04 UTC-4, Grant Rettke wrote: On Thu, Oct

Re: Replacing nested let statements with assignments

2012-10-18 Thread JvJ
The doto form is great, but as far as I know, it only lets you thread a single object. I'm looking at creating several objects consecutively. On Thursday, 18 October 2012 17:11:08 UTC-4, Grant Rettke wrote: I figured you would use doto for that. On Thu, Oct 18, 2012 at 4:09 PM, JvJ

Re: Replacing nested let statements with assignments

2012-10-18 Thread Mark Engelberg
On Thu, Oct 18, 2012 at 1:45 PM, Grant Rettke gret...@acm.org wrote: Anyone voted for internal define lately? On the one hand, internal define would be nice because it would help alleviate the nested let problem and possibly be more intuitive for newcomers. On the other hand, sometimes

Re: Replacing nested let statements with assignments

2012-10-18 Thread AtKaaZ
deja-vu :) On Thu, Oct 18, 2012 at 11:16 PM, JvJ kfjwhee...@gmail.com wrote: On a side note, I was partially inspired by Haskell's do notation, which is imperative-looking syntactic sugar for monadic bind operators. -- You received this message because you are subscribed to the Google

Re: Replacing nested let statements with assignments

2012-10-18 Thread Stuart Sierra
It's slightly different, but libraries such as Flow or Prismatic's Graph can be used to achieve a similar effect. Flow: https://github.com/stuartsierra/flow Graph: http://blog.getprismatic.com/blog/2012/10/1/prismatics-graph-at-strange-loop.html Example using Flow: (def the-flow (flow b

Re: Replacing nested let statements with assignments

2012-10-18 Thread Herwig Hochleitner
2012/10/18 Mark Engelberg mark.engelb...@gmail.com When I want to add print commands for debugging, I usually either do it the way David Nolen described, i.e., binding _ to a printf statement, or I use a little utility macro like this (picked up from stackoverflow): (defmacro dbg[x] `(let

Re: Replacing nested let statements with assignments

2012-10-18 Thread Mark Engelberg
On Thu, Oct 18, 2012 at 4:11 PM, Herwig Hochleitner hhochleit...@gmail.comwrote: FWIW, when just wanting to print out debug values, I use a custom reader tag similar to the above macro: (let [x #log/spy (+ a b)] (usage-of x)) That's nice! I haven't done anything with reader macros.

Re: Replacing nested let statements with assignments

2012-10-18 Thread Mark Engelberg
OK, just looked it up and realized that it's just how # works, and not a special kind of macro. On Thu, Oct 18, 2012 at 5:25 PM, Mark Engelberg mark.engelb...@gmail.comwrote: On Thu, Oct 18, 2012 at 4:11 PM, Herwig Hochleitner hhochleit...@gmail.com wrote: FWIW, when just wanting to print