Re: Extensive use of let?

2009-02-25 Thread Joshua Fox
Of course,  the use of let does not make the code any more imperative or
less functional, as long as there are no side effects. Also, the scope is
limited to the let  block, keeping it "clean," and there should be no harm
to performance.
IMHO, the code with let is simply more readable and therefore more elegant.

Joshua

On Wed, Feb 25, 2009 at 10:57 PM, levand  wrote:

>
> Recently, in my code, I have been struggling with which of the two
> equivalent forms is, in a general sense, "better".
>
> (defn my-fn1 [input]
>  (let [value1 (op1 input)
>value2 (op2 input)
>value3 (op4 value1 value2)]
>(op5 value3)))
>
> (defn my-fn2 [input]
>  (op5 (op4 (op1 input) (op2 input
>
> Now, the second is definitely cleaner and more elegant, besides being
> smaller, which is a non-trivial benefit when I have a fair amount of
> code to page through.
>
> However, if I've been away from the code awhile, it's much easier to
> come back determine what the code is doing when its written the first
> way, especially when it uses descriptive names. An operation that is
> impenetrable when written in nested form can become quite simple when
> each step is broken down and labeled.
>
> Clojure is my first Lisp - should I just stick with the second form
> until I learn to "see through" the nested s-expressions?
>
> It's not that I'm trying to make my code more imperative - Although I
> come from a Java background, I love functional programming, and it is
> a delight to see how much I can do without side-effects. But I do miss
> the self-documentation that well-named variables can provide.
>
> Any thoughts? Also, is there any performance degradation from the
> first way, or can the compiler optimize it away?
> >
>

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Extensive use of let?

2009-02-25 Thread Jason Wolfe


There's also a middle ground:

(defn my-fn2 [input]
  (op5
   (op4
(op1 input)
(op2 input

If your "op" names are descriptive, this can still be very easy to
read, with significantly fewer characters than the "let" version
(you'll get use to reading inside-out quickly).  You can also easily
add per-expression end-of-line comments this way.

That said, I end up using all three versions depending on the length
of the code, how clear the purpose is from the function names, etc.
None is inherently best IMHO, it's just a matter of taste and
circumstance.

Cheers,
Jason

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Extensive use of let?

2009-02-25 Thread Laurent PETIT
Hello,

I fear there isn't (at least for me) a simple general answer for such a
general problem, because :

 * it will really depend on the context : are op1 and op2 themselves cryptic
names that would benefit from having their result clearly named ? are op1
and op2 "low level operations" for which a more "abstract" name given in the
let could help the rest of the let body be itself more abstract too ?
 * it is possible to abuse both forms if you replace a careful thought of
the pros and cons with a "global rule" which, while certainly written with
the best intent in mind, endlessly becomes eventually followed blindly by
some (if not most) people.

I prefer being more general and keep in mind these guideline (and they
really are just guidelines) "rules" :
 - the code should be clear and reveal the intent (but it should not
consider readers are newbies that don't know the language idioms)
 - the code should have no repetition

So more or less 2 of the XP rules ... (don't remember the other ones right
now, but they are interesting, too)

2009/2/25 levand 

>
> Recently, in my code, I have been struggling with which of the two
> equivalent forms is, in a general sense, "better".
>
> (defn my-fn1 [input]
>  (let [value1 (op1 input)
>value2 (op2 input)
>value3 (op4 value1 value2)]
>(op5 value3)))
>
> (defn my-fn2 [input]
>  (op5 (op4 (op1 input) (op2 input
>
> Now, the second is definitely cleaner and more elegant, besides being
> smaller, which is a non-trivial benefit when I have a fair amount of
> code to page through.
>
> However, if I've been away from the code awhile, it's much easier to
> come back determine what the code is doing when its written the first
> way, especially when it uses descriptive names. An operation that is
> impenetrable when written in nested form can become quite simple when
> each step is broken down and labeled.
>
> Clojure is my first Lisp - should I just stick with the second form
> until I learn to "see through" the nested s-expressions?
>
> It's not that I'm trying to make my code more imperative - Although I
> come from a Java background, I love functional programming, and it is
> a delight to see how much I can do without side-effects. But I do miss
> the self-documentation that well-named variables can provide.
>
> Any thoughts? Also, is there any performance degradation from the
> first way, or can the compiler optimize it away?
> >
>

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Extensive use of let?

2009-02-25 Thread Kevin Downey

You should look at "->"
it lest you take (op3 (op2 (op1 input))) and write it as (-> input op1 op2 op3)
there is also "comp" which composes functions, and partial for partial
application.

some example comp usage:
http://github.com/hiredman/clojurebot/blob/297e266b0badf0f301a556e95771b940a80016e7/hiredman/clojurebot/tweet.clj#L11

On Wed, Feb 25, 2009 at 12:57 PM, levand  wrote:
>
> Recently, in my code, I have been struggling with which of the two
> equivalent forms is, in a general sense, "better".
>
> (defn my-fn1 [input]
>  (let [value1 (op1 input)
>        value2 (op2 input)
>        value3 (op4 value1 value2)]
>    (op5 value3)))
>
> (defn my-fn2 [input]
>  (op5 (op4 (op1 input) (op2 input
>
> Now, the second is definitely cleaner and more elegant, besides being
> smaller, which is a non-trivial benefit when I have a fair amount of
> code to page through.
>
> However, if I've been away from the code awhile, it's much easier to
> come back determine what the code is doing when its written the first
> way, especially when it uses descriptive names. An operation that is
> impenetrable when written in nested form can become quite simple when
> each step is broken down and labeled.
>
> Clojure is my first Lisp - should I just stick with the second form
> until I learn to "see through" the nested s-expressions?
>
> It's not that I'm trying to make my code more imperative - Although I
> come from a Java background, I love functional programming, and it is
> a delight to see how much I can do without side-effects. But I do miss
> the self-documentation that well-named variables can provide.
>
> Any thoughts? Also, is there any performance degradation from the
> first way, or can the compiler optimize it away?
> >
>



-- 
And what is good, Phaedrus,
And what is not good—
Need we ask anyone to tell us these things?

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Extensive use of let?

2009-02-26 Thread Luke VanderHart

Very interesting ideas, everyone... thanks a lot for the input.

Yeah, I recognize that each case is going to be different - I guess I
was just looking for suggestions on how to manage it. Which I found...
Comp and partial look particularly interesting. Thanks!

-Luke

On Feb 25, 5:09 pm, Kevin Downey  wrote:
> You should look at "->"
> it lest you take (op3 (op2 (op1 input))) and write it as (-> input op1 op2 
> op3)
> there is also "comp" which composes functions, and partial for partial
> application.
>
> some example comp 
> usage:http://github.com/hiredman/clojurebot/blob/297e266b0badf0f301a556e957...
>
>
>
>
>
> On Wed, Feb 25, 2009 at 12:57 PM, levand  wrote:
>
> > Recently, in my code, I have been struggling with which of the two
> > equivalent forms is, in a general sense, "better".
>
> > (defn my-fn1 [input]
> >  (let [value1 (op1 input)
> >        value2 (op2 input)
> >        value3 (op4 value1 value2)]
> >    (op5 value3)))
>
> > (defn my-fn2 [input]
> >  (op5 (op4 (op1 input) (op2 input
>
> > Now, the second is definitely cleaner and more elegant, besides being
> > smaller, which is a non-trivial benefit when I have a fair amount of
> > code to page through.
>
> > However, if I've been away from the code awhile, it's much easier to
> > come back determine what the code is doing when its written the first
> > way, especially when it uses descriptive names. An operation that is
> > impenetrable when written in nested form can become quite simple when
> > each step is broken down and labeled.
>
> > Clojure is my first Lisp - should I just stick with the second form
> > until I learn to "see through" the nested s-expressions?
>
> > It's not that I'm trying to make my code more imperative - Although I
> > come from a Java background, I love functional programming, and it is
> > a delight to see how much I can do without side-effects. But I do miss
> > the self-documentation that well-named variables can provide.
>
> > Any thoughts? Also, is there any performance degradation from the
> > first way, or can the compiler optimize it away?
>
> --
> And what is good, Phaedrus,
> And what is not good—
> Need we ask anyone to tell us these things?
--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Opinions on -> macro? (was Re: Extensive use of let?)

2009-02-27 Thread John D. Hume

On Wed, Feb 25, 2009 at 4:11 PM, Jason Wolfe  wrote:
> (you'll get use to reading inside-out quickly).

As a Java/Ruby guy who is not used to reading inside out, I'm curious
as to whether people who ARE accustomed to LISP find the -> macro
distracting since it flops things around. Are there circumstances
where you prefer it?

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Opinions on -> macro? (was Re: Extensive use of let?)

2009-02-27 Thread James Reeves

On Feb 27, 6:39 pm, "John D. Hume"  wrote:
> As a Java/Ruby guy who is not used to reading inside out, I'm curious
> as to whether people who ARE accustomed to LISP find the -> macro
> distracting since it flops things around. Are there circumstances
> where you prefer it?

It's pretty useful for nested keywords:

  (:name (:profile (:user message)))

  (-> message :user :profile :name)

- James
--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Opinions on -> macro? (was Re: Extensive use of let?)

2009-02-27 Thread Allen Rohner


>
> It's pretty useful for nested keywords:
>
>   (:name (:profile (:user message)))
>
>   (-> message :user :profile :name)
>
> - James

That is really cool. Once again the language and the community impress
me with how elegant the language is.

Allen

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Opinions on -> macro? (was Re: Extensive use of let?)

2009-02-28 Thread Stuart Sierra

On Feb 27, 1:39 pm, "John D. Hume"  wrote:
> As a Java/Ruby guy who is not used to reading inside out, I'm curious
> as to whether people who ARE accustomed to LISP find the -> macro
> distracting since it flops things around. Are there circumstances
> where you prefer it?

Definitely.  When you're applying a bunch of functions to the same
object, it can be clearer to use ->.  I don't use it often, but when I
do, my code is much easier to read.

-Stuart Sierra
--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Opinions on -> macro? (was Re: Extensive use of let?)

2009-02-28 Thread Laurent PETIT
2009/2/28 Stuart Sierra 

>
> On Feb 27, 1:39 pm, "John D. Hume"  wrote:
> > As a Java/Ruby guy who is not used to reading inside out, I'm curious
> > as to whether people who ARE accustomed to LISP find the -> macro
> > distracting since it flops things around. Are there circumstances
> > where you prefer it?
>
> Definitely.  When you're applying a bunch of functions to the same
> object, it can be clearer to use ->.  I don't use it often, but when I
> do, my code is much easier to read.


What you really wanted to say is "a bunch of functions to one object".
(If it is to the same object, then it's doto that should be use)
?


>
> -Stuart Sierra
> - Afficher le texte des messages précédents -
> >
>

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Opinions on -> macro? (was Re: Extensive use of let?)

2009-02-28 Thread Belfabius

First, I have to say thanks.  I'm only a part-time Clojure user, and I
didn't know of the -> macro until today.  Second, I think the ->
syntax leads to more readable code for precisely those situations
where you're coding a sequence of actions.  Finally, I've got a
comment about what I think might be a useful addition to Clojure.

In my day job, I've been using F#.  F# has a |> operator which relies
on automatic currying to do what, at first blush, I though the ->
macro did.  In other words, if you've got, for example,  a list of
data in F#, you can do something like:

listOfNum |> (map someFn) |>  (filter anotherFn) |> max   ;; note
the brackets aren't necessary

;;
I  added them for readability

which is functionally equivalent to:

max ( filter anotherFn (map someFn listOfNum)))

I quickly learned the -> macro can't be used in the same way for one
simple reason; the value that is threaded through to the forms is
always placed in the first argument position.  How about adding a
macro -- it could be called |> just as in F# -- that places the value
in the last argument position?  (It's dead simple to write -- I just
copied and modified the -> macro.)

With the new macro it would be possible to write code like:

( |> list-of-num
( map some-fn )
( filter another-fn )
( max ))

instead of:

(max (filter another-fn (map some-fn list-of-num)))

IMHO, the |> macro, just like the -> macro, makes the sequence of the
actions much easier to follow.

Cheers,
--
BF




On Feb 27, 1:39 pm, "John D. Hume"  wrote:
> On Wed, Feb 25, 2009 at 4:11 PM, Jason Wolfe  wrote:
> > (you'll get use to reading inside-out quickly).
>
> As a Java/Ruby guy who is not used to reading inside out, I'm curious
> as to whether people who ARE accustomed to LISP find the -> macro
> distracting since it flops things around. Are there circumstances
> where you prefer it?
--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Opinions on -> macro? (was Re: Extensive use of let?)

2009-03-01 Thread Meikel Brandmeyer

Hi,

Am 01.03.2009 um 02:19 schrieb Belfabius:


I quickly learned the -> macro can't be used in the same way for one
simple reason; the value that is threaded through to the forms is
always placed in the first argument position.  How about adding a
macro -- it could be called |> just as in F# -- that places the value
in the last argument position?  (It's dead simple to write -- I just
copied and modified the -> macro.)


See here for a in-depth discussion of the pipe macro. :)

http://groups.google.com/group/clojure/browse_frm/thread/66ff0b89229be894/c3d4a6dae45d4852

Sincerely
Meikel



smime.p7s
Description: S/MIME cryptographic signature