Oct 11, 2010

Infinite data source - Clojure

As the other programming languages we can get number sequences in clojure too.
We can get a list of sequence numbers from a function like;
user=>(def num (iterate inc 0))
This will create an infinite list starts from 0. We can get a list of sequence numbers by;
user=>(take 10 num )
(0,1,2,3,4,5,6,7,8,9)
This will give a list from 0 to 9, 10 incriments.
If i need a list from 10 to some amount, it is like;
user=>(def num_2 (iterate inc 10 ))
user=>(take 5 num_2)
(10,11,12,13,14)

List comprehension

Think if I need to get a list of numbers which can be devide by 4 and I need to add 1 to that number.

user=>(def num_1(iterate inc 0))
user=>(def num_4p1 (for [x num_1 :when(zero? (rem x 4))](inc x)))
Then to get 5 of these things,
user=>(take 5 num_4p1)
This will give an output;
(1,5,9,13,17)
This is not the only way of creating sequences. We can use maps and filters to do this type of task and get the sane output.
user=>(def x (map inc (filter (fn [x] (zero? (rem x 4))) num_1)))

Lisp
Sequence functions

function - (first coll)

This gives the first element of a list like,
user=>(first (list 12 43 546 75))
12
If the function is empty it will return nil as the result,
user=> (first (list))
nil

Function - (rest coll)

This gives the result without the first element. It is like;
user=> (rest (list 12 23 35)
(23 35)

Function - (map x  coll*)

Apply x laizy to each item in the sequence.
user=> (map (fn [x] true) (range 4))
(true true true true)

Not only the true but alsom we can do other calculations as well.
So it may like this;
(map (fn [x] (* 5 x))(range 10))
In this function it will multiply all the numbers fron 0 to 9 by 5, and give the output as,
(0 5 10 15 20 25 30 35 40 45)

We can use this to get a x*x kind of list, like;
user=>(map (fn [a b] (* a b)) (range 5) (range 5))
This gives the out put as,
(0 1 4 9 16)

function - (every? pred coll)
This will return true is the "pred" value is always true in a sequence.
Try this out,
user=>(every? pos? (range 10))
This will give the output as "false"
The "pos?" will return true if the value is greater than 0, so in this range the first element is 0. The output is like first one is false and others are true.
You can modify this like;
user=>(every? pos? (range 1 10))
Now the result will be true, becouse of, all values are more than 0.

In this it will get the values from 1 to 10, try to devide it from 2 and check wether there remaining parts are  equal to 0.
(every? (fn [x] (= 0 (rem x 2))) (range 1 10))
This will give the result as "False " because of some elements remaining parts are not equal to 0. So we can modify this function like this.
(every? (fn [x] (= 0 (rem x 2))) (range 2 10 2))
This function will get a list like (2 4 6...). So these all numbers will give its remainder as 0 when it devides by 2. Thats why this value is "true"

 function - (not-every? pred coll)
This is like same as the (every? ) function, the only difference is when "every?" is giving true "not-every" will give the output as "false". It gives the complement of the "every?" function.

No comments:

Post a Comment

Your comments are always welcome ...