when function

2012-05-21 Thread Christian Guimaraes
Hi all,

I'm struggling with when code structures and my imperative mindset.

What is the better approach (or functional approach) to work with a code
like the below?

(defn parse-group [group-identifier line]
  (when (= group-identifier ID1)
(handle-id1 line))
  (when (= group-identifier ID2)
(handle-id2 line))
  (when (= group-identifier ID3)
(handle-id3 line)))

Thank you.
-- christian

-- 
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: when function

2012-05-21 Thread Bill Caputo
On May 21, 2012, at 10:54 AM, Christian Guimaraes wrote:
 I'm struggling with when code structures and my imperative mindset.
 
 What is the better approach (or functional approach) to work with a code like 
 the below?

I think you're looking for cond (from memory, syntax might be wrong):

(defn parse-group [group-identifier line]
(cond
(= group-identitifer ID1) (handle-id1 line)
...
:else (do-something)))

etc

(doc cond) will give you what you need I think

bill

 (defn parse-group [group-identifier line]
   (when (= group-identifier ID1)
 (handle-id1 line))
   (when (= group-identifier ID2)
 (handle-id2 line))
   (when (= group-identifier ID3)
 (handle-id3 line)))

-- 
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: when function

2012-05-21 Thread Timothy Baldridge
In this case I prefer either:

  (cond (= group-identifier ID1)
(handle-id1 line)
  (= group-identifier ID2)
   (handle-id2 line)
  (= group-identifier ID3)
(handle-id3 line))

Or just use core.match:

(match [group-identifier]
   [ID1]  (handle-id1 line)
   [ID2]  (handle-id1 line)
   [ID3]  (handle-id1 line))

But then again, I have this thing for pattern matching...it's so elegant.

Timothy Baldridge

On Mon, May 21, 2012 at 10:54 AM, Christian Guimaraes
cguimaraes...@gmail.com wrote:
 Hi all,

 I'm struggling with when code structures and my imperative mindset.

 What is the better approach (or functional approach) to work with a code
 like the below?

 (defn parse-group [group-identifier line]
   (when (= group-identifier ID1)
     (handle-id1 line))
   (when (= group-identifier ID2)
     (handle-id2 line))
   (when (= group-identifier ID3)
     (handle-id3 line)))

 Thank you.
 -- christian


 --
 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



-- 
“One of the main causes of the fall of the Roman Empire was
that–lacking zero–they had no way to indicate successful termination
of their C programs.”
(Robert Firth)

-- 
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: when function

2012-05-21 Thread Jay Fields
I'm surprised no one went for condp

(defn parse-group [group-identifier line]
  (condp = group-identifier
ID1 (handle-id1 line)
ID2 (handle-id2 line)
ID3 (handle-id3 line)
(handle-unknown-id line)))

or something like that...


On Mon, May 21, 2012 at 11:59 AM, Timothy Baldridge tbaldri...@gmail.comwrote:

 In this case I prefer either:

  (cond (= group-identifier ID1)
(handle-id1 line)
   (= group-identifier ID2)
   (handle-id2 line)
   (= group-identifier ID3)
(handle-id3 line))

 Or just use core.match:

 (match [group-identifier]
   [ID1]  (handle-id1 line)
   [ID2]  (handle-id1 line)
   [ID3]  (handle-id1 line))

 But then again, I have this thing for pattern matching...it's so elegant.

 Timothy Baldridge

 On Mon, May 21, 2012 at 10:54 AM, Christian Guimaraes
 cguimaraes...@gmail.com wrote:
  Hi all,
 
  I'm struggling with when code structures and my imperative mindset.
 
  What is the better approach (or functional approach) to work with a code
  like the below?
 
  (defn parse-group [group-identifier line]
(when (= group-identifier ID1)
  (handle-id1 line))
(when (= group-identifier ID2)
  (handle-id2 line))
(when (= group-identifier ID3)
  (handle-id3 line)))
 
  Thank you.
  -- christian
 
 
  --
  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



 --
 “One of the main causes of the fall of the Roman Empire was
 that–lacking zero–they had no way to indicate successful termination
 of their C programs.”
 (Robert Firth)

 --
 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: when function

2012-05-21 Thread Sean Corfield
On Mon, May 21, 2012 at 8:54 AM, Christian Guimaraes
cguimaraes...@gmail.com wrote:
 What is the better approach (or functional approach) to work with a code
 like the below?

A simple translation to condp:

(defn parse-group [group-identifier line]
  (condp = group-identifier
ID1 (handle-id1 line)
ID2 (handle-id2 line)
ID3 (handle-id3 line)))

But you may want something more extensible:

(def group-handlers
  {ID1 handle-id1,
   ID2 handle-id2,
   ID3 handle-id3})

(defn parse-group [group-identifier line]
  (if-let [handler (group-handlers group-identifier)]
(handler line)
(println unknown group identifier)))

A multimethod might also be an appropriate way forward for you.
-- 
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.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: when function

2012-05-21 Thread Jonas


On Monday, May 21, 2012 6:54:28 PM UTC+3, Christian Guimaraes wrote:

 Hi all,

 I'm struggling with when code structures and my imperative mindset.

 What is the better approach (or functional approach) to work with a code 
 like the below?

 (defn parse-group [group-identifier line]
   (when (= group-identifier ID1)
 (handle-id1 line))
   (when (= group-identifier ID2)
 (handle-id2 line))
   (when (= group-identifier ID3)
 (handle-id3 line)))

 Thank you.
 -- christian




Hi

Maybe something like

(defn parse-group [group-identifier line]
  (condp = group-identifier
ID1 (handle-id1 line)
ID2 (handle-id2 line)
ID3 (handle-id3 line)))

or, if you want to leverage the fact that maps are functions:

(defn parse-group [group-identifier line]
  (({ID1 handle-id1
 ID2 handle-id2
 ID3 handle-id3} group-identifier) line))

but then you lose some readability IMO.

On Monday, May 21, 2012 6:54:28 PM UTC+3, Christian Guimaraes wrote:

 Hi all,

 I'm struggling with when code structures and my imperative mindset.

 What is the better approach (or functional approach) to work with a code 
 like the below?

 (defn parse-group [group-identifier line]
   (when (= group-identifier ID1)
 (handle-id1 line))
   (when (= group-identifier ID2)
 (handle-id2 line))
   (when (= group-identifier ID3)
 (handle-id3 line)))

 Thank you.
 -- christian




-- 
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: when function

2012-05-21 Thread Bronsa
or simply use `case`
(case group-identifier
   ID1  (handle-id1 line)
ID2 (handle-id2 line)
ID3 (handle-id3 line))

2012/5/21 Timothy Baldridge tbaldri...@gmail.com

 In this case I prefer either:

  (cond (= group-identifier ID1)
(handle-id1 line)
   (= group-identifier ID2)
   (handle-id2 line)
   (= group-identifier ID3)
(handle-id3 line))

 Or just use core.match:

 (match [group-identifier]
   [ID1]  (handle-id1 line)
   [ID2]  (handle-id1 line)
   [ID3]  (handle-id1 line))

 But then again, I have this thing for pattern matching...it's so elegant.

 Timothy Baldridge

 On Mon, May 21, 2012 at 10:54 AM, Christian Guimaraes
 cguimaraes...@gmail.com wrote:
  Hi all,
 
  I'm struggling with when code structures and my imperative mindset.
 
  What is the better approach (or functional approach) to work with a code
  like the below?
 
  (defn parse-group [group-identifier line]
(when (= group-identifier ID1)
  (handle-id1 line))
(when (= group-identifier ID2)
  (handle-id2 line))
(when (= group-identifier ID3)
  (handle-id3 line)))
 
  Thank you.
  -- christian
 
 
  --
  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



 --
 “One of the main causes of the fall of the Roman Empire was
 that–lacking zero–they had no way to indicate successful termination
 of their C programs.”
 (Robert Firth)

 --
 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: when function

2012-05-21 Thread Timothy Baldridge
 A multimethod might also be an appropriate way forward for you.

+1 For multimethods, don't hardcode it if you don'tt have to.

Timothy

-- 
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: when function

2012-05-21 Thread Christian Guimaraes
Hi,

I think that multimethods will fit better in my case, since I have more
than three ID's to handle.

Thank you all for the answers.

-- christian.

On Mon, May 21, 2012 at 5:04 PM, Timothy Baldridge tbaldri...@gmail.comwrote:

  A multimethod might also be an appropriate way forward for you.

 +1 For multimethods, don't hardcode it if you don'tt have to.

 Timothy

 --
 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