Showing posts with label learning. Show all posts
Showing posts with label learning. Show all posts

Monday, June 7, 2010

clojure.core/lazy-seq makes sense

clojure.core/lazy-seq makes sense

Few days ago on #clojure bartj asked about an infamous wiki entry.
I remember that while ago, when I tried to understand laziness in Clojure I stumbled upon this entry as well,
and it caused me only confusion, as it is totally outdated.
Let's try to shed some light on it.

Not lazy seq

When you construct a seq in non lazy way you should expect that all elements are realized at creation time.
(let [a (cons 1 (cons 2 nil))] (first a))
=> 1
Nothing special here. Let's introduce side effects to see what happens.
(let [a (cons
(do (println "one") 1)
(cons
(do (println "two") 2)
nil))]
(first a))
: one
: two

=> 1
As you expected all elements has been realized while constructing a, though we only needed the first element.
And that's exactly where laziness kicks in.

lazy-seq

First lets construct lazy seq with no side effects.
(let [a (lazy-seq (cons 1
(lazy-seq (cons 2 nil))))]
(first a))
=> 1
So what is new? You have to type more and effect is the same. Why?
Adding side effects will help understanding it.
(let [a (lazy-seq (cons
(do (println "one") 1)
(lazy-seq (cons
(do (println "two") 2) nil))))]
(first a))
: one
=> 1
Now you should be able to see the benefit. Result is the same, except the whole sequence hasn't been realized,
except for the first element, as it was the only element that was needed.

Laziness is a very powerful tool:
  • you can construct infinite sequences,
  • have side effects create elements on demand, if seq construction is heavy, you might want to only issue creation of elements you'll use,
  • it has same interface as non-lazy seq, code using it does not have to care about it.

To sum it up

(let [a (cons
(do (println "one") 1)
(cons
(do (println "two") 2) nil))])
: one
: two

=> nil
We didn't need even single element but the whole seq was realized.
(let [a (lazy-seq (cons
(do (println "one") 1)
(lazy-seq (cons
(do (println "two") 2) nil))))])
=> nil
If not even a single element is needed, than not even a single element will be realized.
So there you have it clojure.core/lazy-seq explained.

Monday, January 11, 2010

clojure.core/str makes sense now

clojure.core/str makes sense

While reading excellent Programming Clojure by Stuart Halloway, instead just reading I started looking into how things are done by Clojure itself.
Yesterday it took me some time before I understood how things are done by clojure.core/str. This post is documentation of my findings.
Following modification of said function was very helpful in understanding how it works:
:tag metadata key is a symbol naming a class or a Class object that indicates the Java type of the object in the var, or its return value if the object is a fn..
Next are three parameters body pairs.
([] "")
No arguments (arity 0) will result in "" (empty string)

([#^Object x]
(if (nil? x) "" (. x (toString))))
One argument call (arity 1) will call java.lang.Object.toString method.
#^Object is a Type Hint.
if should not need much explanation.

([x & ys]
((fn [#^StringBuilder sb more]
(if more
(recur (. sb (append (str (first more)))) (next more))
(str sb)))
(new StringBuilder #^String (str x)) ys)))
This is where it starts to be interesting.
First unnamed function is defined that accepts StringBuilder sb and more params.
If more is true, that is not nil or false than call recursively self with new parameters
  • sb with appended result of calling str with first element of more,
  • more with elements after first.
If more is false, that in our context will happen only when more is nil, call sb's toString.
Next just defined function is getting called with new StringBuilder constructed from x as sb and ys as more.

It took me some time to realize that ys are turning into more.

So there you have it clojure.core/str explained.