need help eliminating ordinary recursion/thinking the Clojure way

2011-11-11 Thread Mike
I'm having a conceptual problem, thinking functionally, and I was hoping the fine Clojurians could take a peek. Boiled down, I need to do a combination of map and reduce on a collection. My walker needs to look at pairs along the collection, but then sort of inject new values and restart when

Re: need help eliminating ordinary recursion/thinking the Clojure way

2011-11-11 Thread Michael Gardner
Since you're given a sorted list of intervals, you don't actually need to restart the whole merging process from the start after each merge; you can just replace the last interval in your output with the new, merged interval and continue from there. So `reduce' is the perfect tool for the job;

Re: need help eliminating ordinary recursion/thinking the Clojure way

2011-11-11 Thread Mike
Ahhh...excellent, I see why I was blind. =) If you just build your reduction collection in reverse, then the head of the reduction is always the one you want to compare with as you traverse the incoming collection. So you either cons one item or two onto the result, and when you're all done

Re: thinking in clojure

2010-09-17 Thread Michael Ossareh
On Thu, Sep 16, 2010 at 07:53, Laurent PETIT laurent.pe...@gmail.com wrote: 2010/9/16 Meikel Brandmeyer m...@kotka.de Hi Laurent, On 16 Sep., 15:54, Laurent PETIT laurent.pe...@gmail.com wrote: you don't like my one-liner ? :-) I saw your message only after I sent mine. :) (update-in

Re: thinking in clojure

2010-09-17 Thread Michael Ossareh
(loop [data (sorted-map)          collection newData          meeting (first collection)]   (def key ( )) As a general rule, def should only be used at the top level. You probably want (let [key ...] (if ...) here. Hey, thanks for pointing this out - I was actually just trying to

Re: thinking in clojure

2010-09-17 Thread Michael Ossareh
Meikel,     (recur (cond (not (nil? (data key)))                (true? true) *ieeck* Please do (cond ... :else default-clause). Not true, or (true? true) or other stuff. Wow, I somehow missed the :else option in cond? I've got that (true? true) stuff scattered all over my code - going to

Re: thinking in clojure

2010-09-17 Thread Michael Wood
On 17 September 2010 00:56, Michael Ossareh ossa...@gmail.com wrote: Meikel,     (recur (cond (not (nil? (data key)))                (true? true) *ieeck* Please do (cond ... :else default-clause). Not true, or (true? true) or other stuff. Wow, I somehow missed the :else option in cond?

Re: thinking in clojure

2010-09-17 Thread Michael Ossareh
On Fri, Sep 17, 2010 at 08:58, Michael Wood esiot...@gmail.com wrote: On 17 September 2010 00:56, Michael Ossareh ossa...@gmail.com wrote: Meikel,     (recur (cond (not (nil? (data key)))                (true? true) *ieeck* Please do (cond ... :else default-clause). Not true, or (true?

thinking in clojure

2010-09-16 Thread Michael Ossareh
Hi Guys, One of the things that has struck me about clojure, by virtue of being a lisp, is the concision of the code - I really find it very attractive. However yesterday I found something that I couldn't work out how to do in a concise manner. Clearly commenting the code was a priority once I

Re: thinking in clojure

2010-09-16 Thread David Nolen
On Wed, Sep 15, 2010 at 2:48 PM, Michael Ossareh ossa...@gmail.com wrote: Hi Guys, One of the things that has struck me about clojure, by virtue of being a lisp, is the concision of the code - I really find it very attractive. However yesterday I found something that I couldn't work out how

Re: thinking in clojure

2010-09-16 Thread Mike Meyer
On Wed, 15 Sep 2010 11:48:09 -0700 Michael Ossareh ossa...@gmail.com wrote: Hi Guys, One of the things that has struck me about clojure, by virtue of being a lisp, is the concision of the code - I really find it very attractive. However yesterday I found something that I couldn't work out

Re: thinking in clojure

2010-09-16 Thread Laurent PETIT
Something along those lines : (def t { 0 {:title some :meetings [ :d1 ]} 2 {:title some2 :meetings [ :d2 ]}}) user= (pprint (update-in t [1] (fnil update-in {:title title :meetings []}) [:meetings] (comp vec concat) [:d1 :d2]int (update-in t [1] (fnil update-in {:title title :meetings []})

Re: thinking in clojure

2010-09-16 Thread Meikel Brandmeyer
Hi, maybe this does what you want. I'm not sure, since you add the new meeting to *all* meetings? And where does the :title come from when you add a new meeting? I assume you have a map of a meeting title and a meeting and want to add this to the corresponding entry in the data map. If the

Re: thinking in clojure

2010-09-16 Thread Meikel Brandmeyer
Hi, On 16 Sep., 15:36, Meikel Brandmeyer m...@kotka.de wrote:   (if (not (nil? (next collection) You can save the nil?. (if (not (next collection))) will also work. nil is logically false. And of course this should be (if (next collection)). The not belongs to the You can save part.

Re: thinking in clojure

2010-09-16 Thread Laurent PETIT
2010/9/16 Meikel Brandmeyer m...@kotka.de Hi Laurent, On 16 Sep., 15:54, Laurent PETIT laurent.pe...@gmail.com wrote: you don't like my one-liner ? :-) I saw your message only after I sent mine. :) (update-in coll [k] (fnil update-in *default-value*) [:meetings] (comp vec concat)

Re: thinking in clojure

2010-09-16 Thread Btsai
My poor brain can't handle nested calls to update-in, so this is what I came up with: (defn add-meetings [data k meetings] (cond (nil? (data k)) (assoc data k {:title title :meetings meetings}) :else (update-in data [k :meetings] concat meetings))) On Sep 16, 8:53 am, Laurent PETIT

Re: thinking in clojure

2010-09-16 Thread Laurent PETIT
So nested calls to update-in are needed in order to be able to provide a specific default value, everytime just having an empty map created isn't sufficient. So instead of (update-in {} [:a :b] identity) which returns {:a {:b nil}} , you can break the key path at :a so that the default value if

Re: thinking in clojure

2010-09-16 Thread Laurent PETIT
And note that the pattern works at any level, and is easily readable, since update-in flattens the arguments of the modifying function : If you want to touch the path [:a :b :c :d] and provide specific default values at each level if the key is not found, it's as simple as : (update-in coll

Re: thinking in clojure

2010-09-16 Thread Btsai
That pattern will be a great addition to the toolbox, thank you :) On Sep 16, 9:58 am, Laurent PETIT laurent.pe...@gmail.com wrote: And note that the pattern works at any level, and is easily readable, since update-in flattens the arguments of the modifying function : If you want to touch the

Re: Thinking in Clojure

2010-09-03 Thread Sean Corfield
On Thu, Sep 2, 2010 at 6:29 PM, HB hubaghd...@gmail.com wrote: Usually we create some domain entities, map them with Hibernate/ iBatis. I don't know how a Clojure application would be build without objects. I wonder if watching this talk by Rich Hickey will help?

Re: Thinking in Clojure

2010-09-03 Thread Bob Hutchison
On 2010-09-02, at 10:02 PM, HB wrote: So in idiomatic Clojure applications, maps are considered like objects? And to operate on them we pass them to functions? I think that considering maps as a state representation is reasonable. There are alternatives, but that's fine-tuning I think.

Re: Thinking in Clojure

2010-09-03 Thread Sean Allen
, this OOP/FP is really powerful approach (please note I'm not saying Clojure isn't good, I don't seel flame war) How to think in Clojure? how to achieve this shift? Reading a bit about OOP in either Common Lisp or Dylan might help ease you into 'thinking clojure'. It can provide a bridge between

Re: Thinking in Clojure

2010-09-03 Thread Peter Buckley
I'm only a little ways through Joy of Clojure (my first Clojure book) but bear with me as I'm thinking aloud on what it means for me to think in Clojure. I hope list members will forgive me if I get things wrong - and please correct my working concept(s) as well. One of the things that stuck out

Re: Thinking in Clojure

2010-09-03 Thread MarkH
Sean and Peter made comments that ring very true for me. I've always had a problem with Java/Smalltalk/C# type OO. I also thought the Common Lisp/Dylan way of generic functions and data structures made more sense. Like Peter mentioned, I tend to think in terms of verbs and transformations of

Re: Thinking in Clojure

2010-09-03 Thread Chouser
On Thu, Sep 2, 2010 at 11:36 PM, Peter Buckley buckmeist...@gmail.com wrote: One of the things that stuck out for me that I heard somewhere (can't remember exactly) was that OOP is about framing questions in terms of nouns and FP is about framing questions in terms of verbs. Perhaps you heard

Re: Thinking in Clojure

2010-09-03 Thread Michael Ossareh
On Thu, Sep 2, 2010 at 21:05, Miki miki.teb...@gmail.com wrote: I'd go over SICP, though it not in Clojure but in Scheme - it will show you how to think functional. +1 on this. http://mitpress.mit.edu/sicp/ -- !new number! 415-400-6772 -- You received this message because you are

Re: Thinking in Clojure

2010-09-03 Thread HB
Really nice example Peter, Thanks, I appreciate it. On Sep 3, 6:36 am, Peter Buckley buckmeist...@gmail.com wrote: I'm only a little ways through Joy of Clojure (my first Clojure book) but bear with me as I'm thinking aloud on what it means for me to think in Clojure. I hope list members will

Re: Thinking in Clojure

2010-09-03 Thread CuppoJava
I had the exact same problem transitioning from OOP to Lisp, and I can only offer my own experiences. I finally understood lisp, by programming a new pet project FROM SCRATCH, in the most STRAIGHTFORWARD way possible. I originally started by porting over a program I had written in Java, and found

Re: Thinking in Clojure

2010-09-03 Thread Raoul Duke
On Fri, Sep 3, 2010 at 8:23 AM, Michael Ossareh ossa...@gmail.com wrote: I'd go over SICP, though it not in Clojure but in Scheme - it will show you how to think functional. +1 on this. http://mitpress.mit.edu/sicp/ i think some folks argue that http://www.htdp.org/ is even better :-) or at

Re: Thinking in Clojure

2010-09-03 Thread jt
This may help! http://groups.csail.mit.edu/mac/classes/6.001/abelson-sussman-lectures/ These twenty video lectures by Hal Abelson and Gerald Jay Sussman are a complete presentation of the course, given in July 1986 for Hewlett- Packard employees, and professionally produced by Hewlett-Packard

Re: Thinking in Clojure

2010-09-03 Thread HB
I'm interested in Conjure webframework and I'm considering trying to read its source code and participate later. What do you think? Do you other projects in mind? This discussion is amazing guys :) On Sep 3, 8:25 pm, CuppoJava patrickli_2...@hotmail.com wrote: I had the exact same problem

Thinking in Clojure

2010-09-02 Thread HB
Hey, I finished reading Programming Clojure and Practical Clojure and I'm hooked :) Please count me in the Clojure club. But I failed how to think in Clojure. My main career is around Java web applications (Hibernate, Spring, Lucene) and Web services. Lets not talk about Java web frameworks

Re: Thinking in Clojure

2010-09-02 Thread David Nolen
On Thu, Sep 2, 2010 at 9:29 PM, HB hubaghd...@gmail.com wrote: Hey, I finished reading Programming Clojure and Practical Clojure and I'm hooked :) Please count me in the Clojure club. But I failed how to think in Clojure. My main career is around Java web applications (Hibernate, Spring,

Re: Thinking in Clojure

2010-09-02 Thread HB
So in idiomatic Clojure applications, maps are considered like objects? And to operate on them we pass them to functions? On Sep 3, 4:55 am, David Nolen dnolen.li...@gmail.com wrote: On Thu, Sep 2, 2010 at 9:29 PM, HB hubaghd...@gmail.com wrote: Hey, I finished reading Programming Clojure

Re: Thinking in Clojure

2010-09-02 Thread Wilson MacGyver
I highly recommend Joy of Clojure. It's a good 2nd book on clojure. It shows you the why things are the way they are, and how to do things the clojure way as much as possible. On Thu, Sep 2, 2010 at 9:29 PM, HB hubaghd...@gmail.com wrote: Hey, I finished reading Programming Clojure and

Re: Thinking in Clojure

2010-09-02 Thread Miki
I'd go over SICP, though it not in Clojure but in Scheme - it will show you how to think functional. On Sep 2, 6:29 pm, HB hubaghd...@gmail.com wrote: Hey, I finished reading Programming Clojure and Practical Clojure and I'm hooked :) Please count me in the Clojure club. But I failed how to

Thinking in Clojure - static fields?

2009-01-27 Thread Whirlycott
I'm new to Clojure and I'm wondering how I'm supposed to be thinking about various things. I want to use the c3p0 database connection pool in a Clojure app. In Java, I would simply create an instance of this class and either assign it to a static field or perhaps make it available in a

Thinking in Clojure - static fields?

2009-01-27 Thread Whirlycott
I'm new to Clojure and I'm wondering how I'm supposed to be thinking about various things. I want to use the c3p0 database connection pool in a Clojure app. In Java, I would simply create an instance of this class and either assign it to a static field or perhaps make it available in a

Re: Thinking in Clojure - static fields?

2009-01-27 Thread Timothy Pratley
Here is one way: (let [dbc (make-db)] (defn get-apples [] (.query dbc select * from apples))) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to

Re: Thinking in Clojure - static fields?

2009-01-27 Thread Chouser
On Mon, Jan 26, 2009 at 11:13 PM, Whirlycott p...@whirlycott.com wrote: I'm new to Clojure and I'm wondering how I'm supposed to be thinking about various things. I want to use the c3p0 database connection pool in a Clojure app. In Java, I would simply create an instance of this class and