On Sat, Aug 13, 2011 at 9:45 AM, jaime <xiejianm...@gmail.com> wrote:

> The only reason that I can imagine is this: because we often use
> higher-order functions, these higher-order functions will accept
> functions as its parameters, in such a situation, when we want to use
> a higher-order function but don't want to pass any "real" functions to
> it, then we can use function like "identity" and "identity" here is
> just to fill the role of parameter of higher-order function.
>
> Guys, is my guess correct or not?


Yup. As an example, in clojure.java.jdbc, there are "naming strategies" that
determine how to map between entity names in the database (strings) and
keywords in the Clojure maps that represent those entities. The default
behavior from keyword to string is "do nothing" (other than call 'name' to
convert the keyword to a string) so the code uses the identity function as
the default strategy when the user doesn't override it.

In my own code I have a data mapper that wraps c.j.jdbc and it takes an
optional key-gen function that can calculate and assoc in the primary key
for a record. Again, the default behavior is to "do nothing" and let the
database had the key generation:

   (defn save-row

  "Given a table name (string), a row (struct) and an

   optional key-gen function, either insert it (if no

   :id) or update it. In both cases, return the :id.

   If no key-gen function is provided the database

   must auto-generate the :id."

  ([table r] (save-row table r identity))

  ([table r key-gen] ...))

If you call (save-row :user {:name "Sean"}) it calls itself as (save-row
:user {:name "Sean"} identity).

I could call it like this to generate the primary key as the hash of the
name column: (save-row :user {:name "Sean"} (fn [m] (assoc m :id (.hashCode
(:name m)))))
-- 
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

Reply via email to