On Wed, Sep 14, 2011 at 5:58 AM, Leonardo Borges
<leonardoborges...@gmail.com> wrote:
> (defn neighbors
>   ([size yx] (neighbors [[-1 0] [1 0] [0 -1] [0 1]] size yx))
>   ([deltas size yx]
>     (filter (fn [new-yx]
>               (every? #(< -1 % size) new-yx))
>      (map #(map + yx %) deltas))))
>
> This syntax made me scratch my head since I believe it was the first
> time I saw it.

In case it wasn't clear from Chouser's response, the key difference is
that this declares two versions of neighbors, one that takes three
arguments and one that takes four - essentially overloading on arity.
Here's an example from some of our code at World Singles:

(defn save-row
  "Given a table name (string), a record and an optional
   key-gen function, either insert it after applying the
   key-gen function (if no pk) or update it. In both
   cases, return the pk. The default key-gen function
   is a no-op (identity).
   When operating on a MongoDB collection, the logic is
   much simpler because the pk is always :_id and key
   generation is always handled by MongoDB. Also, we
   always return the entire updated record (since we
   can run functions against the database)."
  ([table record]            (save-row table record identity :id 0))
  ([table record key-gen]    (save-row table record key-gen :id 0))
  ([table record key-gen pk] (save-row table record key-gen pk 0))
  ([table record key-gen pk retry]
    ...))

Mostly we call this as (save-row :table-name {:some "record"}) but we
can also supply a key generation function (save-row :table-name {:some
"record"} add-uuid), for tables that don't have a generated PK column,
and we can specify the PK name if it isn't :id (save-row :table-row
{:some "record"} identity :email).

Ignore retry - we need to refactor that into a private helper function
:) It's part of the machinery that let's automatically retry
operations on MongoDB if the replica set has no active primary node
since it can take a while to failover...

Hope that helps?
-- 
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