Tech behind Tech

Raw information. No finesse :)

Map, Reduce and Filter in Clojure

with 2 comments


Map

Map applies a function to each element in a collection and returns a new collection with the results of each function.

map.png
user> (map #(+ 10 %1) [ 1 3 5 7 ])
(11 13 15 17)

Reduce

Reduce applies a function on all elements of a collection and returns a value. This value could also be another collection.

reduce.png
user> (reduce * [2 3 4])
24

Filter

Filter applies a predicate function to each element in a collection and returns a new filtered collection.

filter.png
user> (filter even? [1 2 3 4 5 6])
(2 4 6)

Written by Siva Jagadeesan

July 26, 2010 at 10:00 pm

Posted in Clojure

Tagged with , , ,

2 Responses

Subscribe to comments with RSS.

  1. Reduce can basically return whatever you want, not just a scalar value. For instance, it can return another collection.

    Amit Rathore

    July 27, 2010 at 6:10 am

  2. Thanks Amit for pointing out. I have updated my blog.

    Siva Jagadeesan

    July 27, 2010 at 6:34 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 146 other followers

%d bloggers like this: