Yield implementation

2011-02-22 Thread Olek
Hi, I know that it is not a lispy way, but most of us come from imperative Java world, and if there is anyone who needs python/ruby yield here it is: http://bit.ly/ejQrLl and the example usage is: http://bit.ly/ekBHr0 Cheers, Olek -- You received this message because you are subscribed to the

Ordering of defn's?

2011-02-22 Thread Jonathan Mitchem
I'm new to Lisps in general, and very new to Clojure. When I was trying out CL, I could put my "defun/defn"s in any order in the file, and it would load and run fine in the REPL. However, in Clojure, it seems to be much more C/C++-like and I have to define things before I use them in other defns.

Transforming map entries

2011-02-22 Thread yair
I'm hoping this is a dumb question and I've missed something obvious. I have a map with various key-value pairs and I want to transform some of the values, e.g. (def mymap {:first "john" :last "smith" :age 25}) and say I want to change the strings to be upper case. Right now all I can think of doi

Re: Ordering of defn's?

2011-02-22 Thread .Bill Smith
Hi Jonathan, welcome to Clojure. Yes, you have to define or declare things before you refer to them. It sounds like you already know how to define things. To declare things, use the declare macro. Cheers, Bill Smith On Feb 22, 12:05 am, Jonathan Mitchem wrote: > I'm new to Lisps in general, an

Re: Is there an on-going running service

2011-02-22 Thread Aaron Bedra
Although a while loop does work here, it's just a hack to get the desired behavior. Have you considered using a scheduled thread pool as your controller? For example: (def ^{:private true} pool (atom nil)) (def ^{:private true} num-threads 3) (defn- thread-pool [] (swap! pool (fn [p] (or p

Re: Transforming map entries

2011-02-22 Thread Damien Lepage
Hi, I thought update-in could help but it looks as complicated as your solution: (use 'clojure.contrib.string) (loop [m mymap, ks (keys m)] (let [new-m (update-in m [(first ks)] #(if (string? %) (upper-case %) %)) more (rest ks)] (if-not (empty? more) (recur new-m more)

Re: Transforming map entries

2011-02-22 Thread rob levy
The usual intuitive options for this are reduce, zipmap, or into. You can also write a lazily recursive solution. I wonder why there's no function in core that lazily re-constructs the map with the results of the function? It seems to have been discussed on the list at least once or twice. It s

Re: Transforming map entries

2011-02-22 Thread Nurullah Akkaya
You can use map to get your new sequence of pairs, the turn it into a hash-map using into, (->> {:first "john" :last "smith" :age 25} (map #(let [[k v] %] (if (string? v) [k (.toUpperCase v)] [k v]))) (into {})) Best, -- Nurullah Akkaya http://nakkaya.com On Tuesday, February 22, 2011 at 5:08

Re: Transforming map entries

2011-02-22 Thread Daniel Bell
I can't think of anything core, but (let [f #(. % toUpperCase)] (zipmap (keys skills) (map f (vals skills doesn't seem too bad. On Feb 21, 8:08 pm, yair wrote: > I'm hoping this is a dumb question and I've missed something obvious. > I have a map with various key-value pairs and I want to

Re: Ordering of defn's?

2011-02-22 Thread Alan
On Feb 21, 10:05 pm, Jonathan Mitchem wrote: > I'm new to Lisps in general, and very new to Clojure. > > When I was trying out CL, I could put my "defun/defn"s in any order in > the file, and it would load and run fine in the REPL.  However, in > Clojure, it seems to be much more C/C++-like and I

Re: Ordering of defn's?

2011-02-22 Thread Daniel Bell
This is true. But you "declare" lets you throw out all the names you need to at the beginning to avoid circular definitions and the like. eg. (declare sum) (defn average [coll] (/ (sum coll) (count coll))) (defn sum [coll] (apply + coll)) On Feb 21, 11:05 pm, Jonathan Mitchem wrote: > I'm

Re: Transforming map entries

2011-02-22 Thread Alan
On Feb 22, 3:23 pm, rob levy wrote: > The usual intuitive options for this are reduce, zipmap, or into.  You can > also write a lazily recursive solution.  I wonder why there's no function in > core that lazily re-constructs the map with the results of the function?  It > seems to have been discus

Re: Transforming map entries

2011-02-22 Thread yair
Another idea I had is something like this: (hashmap (flatten (map #(...) oldmap))), since calling map on a map returns a sequence of whatever the function returns, but this isn't the neatest solution either... On Feb 23, 10:23 am, rob levy wrote: > The usual intuitive options for this are reduce,

Re: Transforming map entries

2011-02-22 Thread Benny Tsai
There is fmap from clojure.contrib.generic.functor, which expects a function of arity 1, for just the value: (use 'clojure.contrib.generic.functor) (require '[clojure.string :as str]) (def my-map {:first "john" :last "smith" :age 25}) (defn my-fn [value] (if (string? value) (str/upper-case

Re: Transforming map entries

2011-02-22 Thread rob levy
Yeah, after I wrote that, it occurred to me that there are good reasons why Clojure nudges you away from doing lazy operations on maps, although I hadn't thought much about it. I didn't think of walk. That is probably the most elegant out of the approaches I have seen to this common problem. On

Re: Transforming map entries

2011-02-22 Thread rob levy
Yeah, fmap is perfect and definitely the most elegant for the specific problem described by the OP. I had never heard of that one. On Tue, Feb 22, 2011 at 6:56 PM, Benny Tsai wrote: > There is fmap from clojure.contrib.generic.functor, which expects a > function of arity 1, for just the value:

Re: Ordering of defn's?

2011-02-22 Thread Jonathan Mitchem
Hm, I see now. Does Java work like that too? (C# doesn't.) Are there any plans for this to change in the future? I can work with it now, but it makes me uncomfortable. It feels like a step backwards. I fully understand if it was just a feature put on hold, just to get the reader/compiler out

akka

2011-02-22 Thread Mark Engelberg
I've just spent the afternoon perusing the docs for Akka 1.0 (akka.io), which was recently announced. It's a library written in Scala for building distributed, fault-tolerant systems. The library offers both a Scala API and a straight Java API. Interestingly, Akka borrows heavily from Clojure for

Re: Transforming map entries

2011-02-22 Thread Mike Meyer
On Tue, 22 Feb 2011 15:36:02 -0800 (PST) Daniel Bell wrote: > I can't think of anything core, but > > (let [f #(. % toUpperCase)] > (zipmap (keys skills) (map f (vals skills doesn't seem too bad. Does clojure guarantee that keys & vals return things in the proper order for this to work? S

Re: Transforming map entries

2011-02-22 Thread Alan
Augh, my eyes! Seriously though, you can save yourself some pain. If you're going to destructure in an anonymous function, just use (fn) instead of the now- actually-longhand #() shorthand form: (fn [[k v]] ...) vs #(let [[k v] %] ...) is clearer and doesn't stop you using another #() nested withi

Re: Transforming map entries

2011-02-22 Thread Alan
Yes, it is guaranteed, and I'm dubious about your claim about serializing. (seq foo) will return the entries in foo in the same order always; but (seq (assoc foo 1 2)) may return the entries in a completely different order. You can treat (keys x) as if it were defined by (map key (seq x)): user=>

Re: Transforming map entries

2011-02-22 Thread Benny Tsai
I was introduced to fmap by Haskell. Specifically, the (IMHO) most excellent "Learn You a Haskell for Great Good!" online tutorial. Highly recommended! On Feb 22, 5:04 pm, rob levy wrote: > Yeah, fmap is perfect and definitely the most elegant for the specific > problem described by the OP.  I h

Re: Ordering of defn's?

2011-02-22 Thread Jonathan Mitchem
Basically, when I'm solving the problem, I'd think "average is sum of the items divided by the count". So... (defn average [coll] (/ (sum coll) (count coll))) Then, "since count is defined, I just need to define sum". (defn sum [coll] (reduce + coll)) In that order: 1 2 Without declare, I

Re: Ordering of defn's?

2011-02-22 Thread Mark Engelberg
On Tue, Feb 22, 2011 at 4:12 PM, Jonathan Mitchem wrote: > I fully understand if it was just a feature put on hold, just to get > the reader/compiler out there.  If it was a design decision, then > that's kind of different. I'm not crazy about this behavior either, but my understanding is that th

Re: Ordering of defn's?

2011-02-22 Thread Mark Engelberg
On Tue, Feb 22, 2011 at 4:28 PM, Jonathan Mitchem wrote: > Basically, when I'm solving the problem, I'd think "average is sum of > the items divided by the count". > > So... > (defn average [coll] >  (/ (sum coll) (count coll))) > > Then, "since count is defined, I just need to define sum". > > (d

Re: Transforming map entries

2011-02-22 Thread Ken Wesson
On Tue, Feb 22, 2011 at 7:26 PM, Benny Tsai wrote: > I was introduced to fmap by Haskell.  Specifically, the (IMHO) most > excellent "Learn You a Haskell for Great Good!" online tutorial. > Highly recommended! Take off every 'monad'! For great justice! -- You received this message because you a

Re: Transforming map entries

2011-02-22 Thread Daniel Bell
I don't know if it's specified in the documentation anywhere, but (= map-I-made-up (zipmap (keys map-I-made-up) (vals map-I-made-up))) returns true. On Feb 22, 5:17 pm, Mike Meyer wrote: > On Tue, 22 Feb 2011 15:36:02 -0800 (PST) > > Daniel Bell wrote: > > I can't think of anything

Re: Transforming map entries

2011-02-22 Thread Ken Wesson
On Tue, Feb 22, 2011 at 7:36 PM, Daniel Bell wrote: > I don't know if it's specified in the documentation anywhere It doesn't seem to be. > but > (= map-I-made-up >  (zipmap >    (keys map-I-made-up) >    (vals map-I-made-up))) > > returns true. However, it is clearly intentional behavior nonet

Re: Ordering of defn's?

2011-02-22 Thread Mikhail Kryshen
On Tue, 22 Feb 2011 16:12:50 -0800 (PST) Jonathan Mitchem wrote: > Hm, I see now. Does Java work like that too? (C# doesn't.) No. > Are there any plans for this to change in the future? I can work with > it now, but it makes me uncomfortable. It feels like a step > backwards. > > I fully u

Implementing search algorithm for binary tree

2011-02-22 Thread HB
Hi, I'm trying to implement searching algorithm for binary tree. Here is my Java implementation: public Node findNode(Integer data) { Node current = root; while (!data.equals(current.getData())) { if (data < current.getData()) current = current.getL

Re: Implementing search algorithm for binary tree

2011-02-22 Thread Ken Wesson
On Tue, Feb 22, 2011 at 8:37 PM, HB wrote: > Hi, > > I'm trying to implement searching algorithm for binary tree. > Here is my Java implementation: > >    public Node findNode(Integer data) { >        Node current = root; >        while (!data.equals(current.getData())) { >            if (data < c

Re: Transforming map entries

2011-02-22 Thread Mike Meyer
On Tue, 22 Feb 2011 16:23:00 -0800 (PST) Alan wrote: > Yes, it is guaranteed, and I'm dubious about your claim about > serializing. (seq foo) will return the entries in foo in the same > order always; but (seq (assoc foo 1 2)) may return the entries in a > completely different order. You can trea

Release.Next Version Number

2011-02-22 Thread Christopher Redinger
As you can see on the Release.Next Planning page [1], there is an open question about whether or not the next version of Clojure should be 1.3 or 2.0. The issue at hand is that we are introducing backwards incompatible API changes. Not looking to start a debate at this point, but just taking a t

Distributed atom :)

2011-02-22 Thread Andreas Kostler
Hello all, How could one simulate a distributed atom, e.g. a ref that get updated atomically and thus synchronised in two different processes. Has anyone thought about this in Clojure before? Kind Regards Andreas -- "Programs must be written for people to read, and only incidentally for machine

Re: Distributed atom :)

2011-02-22 Thread Ken Wesson
On Tue, Feb 22, 2011 at 10:37 PM, Andreas Kostler wrote: > Hello all, > How could one simulate a distributed atom, e.g. a ref that get updated > atomically and thus synchronised in two different processes. > Has anyone thought about this in Clojure before? Well, the simplest and most obvious thi

Importing and using Enum types

2011-02-22 Thread Kasim
Hi Hackers, I am trying to import and use following Enum class from Clojure: http://build.xuggle.com/view/Stable/job/xuggler_jdk5_stable/javadoc/java/api/com/xuggle/xuggler/IContainer.Type.html Basically, I need it so that I can feed it to a Java method that requires it. I'd appreciate any idea o

Re: Importing and using Enum types

2011-02-22 Thread Ken Wesson
On Tue, Feb 22, 2011 at 11:38 PM, Kasim wrote: > Hi Hackers, > I am trying to import and use following Enum class from Clojure: > http://build.xuggle.com/view/Stable/job/xuggler_jdk5_stable/javadoc/java/api/com/xuggle/xuggler/IContainer.Type.html > > Basically, I need it so that I can feed it to a

Re: Trouble with type hints

2011-02-22 Thread Nick
After looking at it more, it appears that I was executing something intense and lazy immediately prior to this function which then included a doall upon the result of the previous calculation. When I modified the previous function to remove its laziness, the time required for this calculation drop