On Dec 11, 11:14 am, ngocdaothanh <ngocdaoth...@gmail.com> wrote:

> Because of indents, my previous Clojure code lied to my eyes that x,
> y, f, g are not at the same block level. This is my difficulty with
> Clojure. In short, I can't see a rough algorithm from any Clojure code
> any more just by seeing the shape (levels) of blocks. To understand a
> Clojure code, I have to look at every bit of code, look closer.
>

You should come up with a real code example instead of a bunch of (f
x) (y z) expressions... The example you show is not very idiomatic
clojure simply because it has side-effects. (Of course you need side-
effects, but they should be isolated).

If the code structure gets too complicated, simplify it by writing
functions or macros to better express your intent.

> > In Clojure we still need to look farther up the screen...
>
> Things in Erlang are immutable, so I think Clojure has no advantage
> over Erlang here.
>
> > Practice using comp & partial...
>
> What do you mean by "comp & partial"? Could you please explain?

comp is function composition, and partial is partial application. eg:

((comp negate inc) 6) -> -7

((partial + 1) 5) -> 6

using these to create new functions is called point-free style
I'm not so much a fan, and I don't think point-free style is
particularly idiomatic in Clojure (unlike in Haskell for example), but
it's sometimes less noisy than #() or fn expressions, so it's good to
know.

They're usually best used with filter and map.

>
> > (let [x 1
> >       y (+ x 2)]
> >   (f x)
> >   (g y))
>
> Well, I know my example code can be rewritten like that, but I just
> could not give a better one.

I don't think there's any use for comp or partial in this case. it's
too simple.

>
> Rearranging like this reminds me of C, in which every variables must
> be listed beforehand. Since all Clojurians say "lazy" is good, I would
> say I prefer C++ because I can be lazy in it, I only have to declare a
> variable when I actually use it.

Your code examples are a bit pathological. Usually (hopefully), you
would have Clojure code that looks like this:

(if-let [source (get-stuff-from-database)]
  (reduce +
    (map somefn
      (filter somepred (extract-data source))))
  0) ; no data

Which is more functional in style. One thing you can do is to learn to
read these from right to left, which takes a bit of practice, but pays
off in the end, in my opinion. The ->> and -> macros are sometimes
useful for transforming the right-to-left -readable forms into forms
that look more like pipelines:

(->> (extract-data source)
        (filter somepred)
        (map somefn)
        (reduce +))

There's no need for temporary names here. If a step needs explanation,
just add comments.

Hope this helps.

--
Jarkko

-- 
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 
first post.
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

Reply via email to