What is the purpose of the Identity function?
At the risk of sounding completely dense, I am having a hard time understanding the purpose of the Identity function. As far as I can tell all it does is return what is passed to it. Review code I see it used all the time, and cannot understand why it is needed. Any insight? Thanks Base -- 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
Re: What is the purpose of the Identity function?
My two cents: You can use it with the operator -, in order to pass something to another function, or for propagating an input of some sort. It sounds sensed? Bye, Alfredo -- 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
Re: What is the purpose of the Identity function?
I usually use identity as a predicate for functions such as filter, drop-while, take-while, etc. Consider this silly example: imagine you had an operation that fetches stuff from a resource (DB, network, etc.) and that upon failing it returns nil. Additionally, imagine that you're interested in running this operation for several resources and keeping those values which didn't fail. You can do so with identity: user= (defn my-operation-that-might-fail [x] (if (= x foo) x nil)) #'user/my-operation-that-might-fail user= (def some-values [bar foo baz]) #'user/some-values user= (filter identity (map your-pred some-values)) (foo) user= and you have your single operation that didn't fail. My 0.5cts. U -- 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
Re: What is the purpose of the Identity function?
I just grepped the clojure source code and an interesting use is in walk.clj https://github.com/clojure/clojure/blob/master/src/clj/clojure/walk.clj#L62 `walk` has flexibility with higher order functions, but identity helps with the simple case of just returning the forms elegantly. I thought it was cool :) Ambrose On Sat, May 7, 2011 at 11:51 PM, Base basselh...@gmail.com wrote: At the risk of sounding completely dense, I am having a hard time understanding the purpose of the Identity function. As far as I can tell all it does is return what is passed to it. Review code I see it used all the time, and cannot understand why it is needed. Any insight? Thanks Base -- 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 -- 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
Re: What is the purpose of the Identity function?
Hi, you can also use it to do funny stuff with juxt. (map (juxt identity f) some-seq) Sincerely Meikel -- 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
Re: What is the purpose of the Identity function?
On Sat, May 7, 2011 at 8:51 AM, Base basselh...@gmail.com wrote: At the risk of sounding completely dense, I am having a hard time understanding the purpose of the Identity function. As far as I can tell all it does is return what is passed to it. It's most useful when you have functions that take functions as arguments. For example, I have code that performs a SQL query and then runs a map-reduce transformation on that. Sometimes, however, I want just the original data so I can pass in identity (to map) and have it be a no-op. Identity on its own isn't really useful - but in combination with higher-order functions, it can be very indispensible! -- Sean A Corfield -- (904) 302-SEAN An Architect's View -- http://corfield.org/ World Singles, LLC. -- http://worldsingles.com/ Railo Technologies, Inc. -- http://www.getrailo.com/ Perfection is the enemy of the good. -- Gustave Flaubert, French realist novelist (1821-1880) -- 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
Re: What is the purpose of the Identity function?
Ahhh - Thanks all. Most educational! This does make sense - I will try and deconstruct some of the examples where this is used to get a sense of when / why it is used. But this helps *tremendously*!! Thanks!!! On May 7, 2:09 pm, Sean Corfield seancorfi...@gmail.com wrote: On Sat, May 7, 2011 at 8:51 AM, Base basselh...@gmail.com wrote: At the risk of sounding completely dense, I am having a hard time understanding the purpose of the Identity function. As far as I can tell all it does is return what is passed to it. It's most useful when you have functions that take functions as arguments. For example, I have code that performs a SQL query and then runs a map-reduce transformation on that. Sometimes, however, I want just the original data so I can pass in identity (to map) and have it be a no-op. Identity on its own isn't really useful - but in combination with higher-order functions, it can be very indispensible! -- Sean A Corfield -- (904) 302-SEAN An Architect's View --http://corfield.org/ World Singles, LLC. --http://worldsingles.com/ Railo Technologies, Inc. --http://www.getrailo.com/ Perfection is the enemy of the good. -- Gustave Flaubert, French realist novelist (1821-1880) -- 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
Re: What is the purpose of the Identity function?
On Sat, 7 May 2011 12:09:45 -0700 Sean Corfield seancorfi...@gmail.com wrote: Identity on its own isn't really useful - but in combination with higher-order functions, it can be very indispensible! Bingo. An HOF accepts a function that filters/mogrifies data before processing it in some way. Sometimes, you *don't* want to have that extra step. There are two ways to do that: one is to have two variants of the HOF - one which uses the function, and one which doesn't (which may mean it's not an HOF). The other is identity. Clojure does both, depending. You can see the first in sort and sort-by, where sort-by uses a keyfn to extract keys from items in the collection. You could just identity for the keyfn to sort by items, but this case is so common it gets it's own function - sort. Similarly, filter takes a predicate to check which items need to be removed. If you just want to remove false values, the appropriate predicate is identity. This case isn't very common, so there's no second version. mike -- Mike Meyer m...@mired.org http://www.mired.org/ Independent Software developer/SCM consultant, email for more information. O ascii ribbon campaign - stop html mail - www.asciiribbon.org -- 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