Re: Looking for advice to fine tune a simple function

2014-02-19 Thread Mark Fisher
it's more idiomatic to use *when* rather than *if* for cases where you won't be considering the false path. (when (= x y) z) cond is used more as a multi-if with a drop out at the end (usually using :else which because it's a keyword is truthy when evaluated). On Wednesday, 19 February 2014

Re: Looking for advice to fine tune a simple function

2014-02-18 Thread Philipp Meier
Hi, Am Sonntag, 16. Februar 2014 23:31:46 UTC+1 schrieb Laurent Droin: Here is a function that I came up with that takes a bpm (heart beats per minute) value, as well as a sequence of 4 values that represent the boundaries defining the 5 different heart rate zones for a particular

Re: Looking for advice to fine tune a simple function

2014-02-18 Thread Johanna Belanger
You can use the *pad* argument in *partition* to add x as the value of :zone-5, like *(partition 2 2 [x] params)*. (Note you have to supply *step* if you supply *pad*.) Then you don't need the *or* or the *(last params)*. Also, like Billy did, you can use *some* instead of (*first (filter

Re: Looking for advice to fine tune a simple function

2014-02-18 Thread Laurent Droin
Wow, that's amazing. Thanks Billy and Johanna. I'm going to try all this tonight. What I'm not sure of (I don't have a good understanding yet about lazy sequences) is whether or not the sequence given to some is lazy or not. For example, if I have thousands of parameters but x is = the first

Re: Looking for advice to fine tune a simple function

2014-02-18 Thread Johanna Belanger
I know. Isn't Clojure beautiful? Sigh... In the docs for *partition*http://clojuredocs.org/clojure_core/clojure.core/partition, it says that partition returns a lazy sequence. And if you look at the source section of the docs for *some*http://clojuredocs.org/clojure_core/clojure.core/some you

Re: Looking for advice to fine tune a simple function

2014-02-18 Thread Laurent Droin
He he... more research work for me tonight. Thanks Johanna for the pointers. -- 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

Re: Looking for advice to fine tune a simple function

2014-02-18 Thread Laurent Droin
Now that I have a better understanding of what some does (i didn't interpret the doc properly), it does totally make sense that it would be recursive, so that's great. While reducing my code with Johanna's feedback, I noticed I kept using cond and not if. Is there any meaningful difference

Re: Looking for advice to fine tune a simple function

2014-02-17 Thread Andy-
On Sunday, February 16, 2014 11:19:58 PM UTC-5, Bruno Kim Medeiros Cesar wrote: (BTW, Andy, how do you format your code so prettily?) I usually just paste it into pygments.org and then paste it back into google groups (which accepts the generated HTML). Cheers -- You received this message

Re: Looking for advice to fine tune a simple function

2014-02-17 Thread Laurent Droin
Wow, thanks Gianluco, Andy and Bruno. Lots of good feedback I am trying to process. It's amazing how coming up with a satisfying functional programing style function is a complex process when you've been doing imperative programing all your life. It's really a whole different way of thinking.

Re: Looking for advice to fine tune a simple function

2014-02-17 Thread Johanna Belanger
Thanks for this! On Monday, February 17, 2014 4:49:38 AM UTC-8, Andy- wrote: On Sunday, February 16, 2014 11:19:58 PM UTC-5, Bruno Kim Medeiros Cesar wrote: (BTW, Andy, how do you format your code so prettily?) I usually just paste it into pygments.org and then paste it back into google

Re: Looking for advice to fine tune a simple function

2014-02-17 Thread Johanna Belanger
Hi, I'm fairly new to clojure as well, but I like this way, and I *think* it's idiomatic: Data structure: [{:zone-max 100 :zone-key :hr-zone-1} {:zone-max 120 :zone-key :hr-zone-2} {:zone-max 140 :zone-key :hr-zone-3} {:zone-max 160 :zone-key :hr-zone-4} {:zone-max 333 :zone-key

Re: Looking for advice to fine tune a simple function

2014-02-17 Thread Laurent Droin
All the good feedback here had me thinking a lot... Especially Andy who pushed me towards more abstraction. I loved the idea of functions that return functions and researching all this led me to embrace partials. Here is my current implementation of quantize. It came out of a lot of trial and

Re: Looking for advice to fine tune a simple function

2014-02-17 Thread Andy-
On Monday, February 17, 2014 6:43:18 PM UTC-5, Laurent Droin wrote: (def inf Long/MAX_VALUE) (defn quantize [x markers values] (let [f (partial (fn([%1 %2 %3] (cond (= %1 %2) %3))) x)] (first (filter #(not (nil? %))(map f (conj markers inf) values) The reason I didn't go

Re: Looking for advice to fine tune a simple function

2014-02-17 Thread Laurent Droin
All good points Andy. Thanks. I'll continue working on it until I'm happy. I am almost never happy with anonymous functions either. In this particular case, though, the function is so simple that I felt it would be overkill to externalize it into another function (unless I'm really going to use

Re: Looking for advice to fine tune a simple function

2014-02-17 Thread Laurent Droin
So here is a new attempt at the quantize function so that I no longer have to deal with the infinity problem: (defn quantize-2 [x params] (let [f (partial (fn [a [b c]] (cond(= a c) b)) x)] (or (first (filter #(not (nil? %)) (map f (into [](partition 2 2 params))) (last

Re: Looking for advice to fine tune a simple function

2014-02-17 Thread Laurent Droin
So here is a new attempt at the quantize function so that I no longer have to deal with the infinity problem: (defn quantize-2 [x params] (let [f (partial (fn [a [b c]] (cond(= a c) b)) x)] (println (first (filter #(not (nil? %))(map f (into [](partition 2 params)) (or

Re: Looking for advice to fine tune a simple function

2014-02-17 Thread Laurent Droin
So here is a new attempt at the quantize function so that I no longer have to deal with the infinity problem: (defn quantize-2 [x params] (let [f (partial (fn [a [b c]] (cond(= a c) b)) x)] (or (first (filter #(not (nil? %)) (map f (into [](partition 2 params) (last

Re: Looking for advice to fine tune a simple function

2014-02-17 Thread Laurent Droin
Hmmm, looks like I can replace #(not (nil? %)) by (complement nil?) which seems more elegant. Also, it looks like I don't need that (into [] ), which will keep the code cleaner. I think I could also get rid of the (or) by always adding (last params) at the end of the sequence from which I

Looking for advice to fine tune a simple function

2014-02-16 Thread Laurent Droin
Hi, Disclaimer - I am completely new to Clojure. I just implemented my very first (simple) program, letting me find out, from a GPX file, how much time is spent in the various heart rate zones. Now that it's working, I'm reviewing the code and trying to use best practices. From what I have

Re: Looking for advice to fine tune a simple function

2014-02-16 Thread gianluca torta
to me it seems that you are anyway relying on the assumption that the sequence is ordered, so I think it would be convenient to drop the ands Gianluca On Sunday, February 16, 2014 11:31:46 PM UTC+1, Laurent Droin wrote: Hi, Disclaimer - I am completely new to Clojure. I just implemented my

Re: Looking for advice to fine tune a simple function

2014-02-16 Thread Andy-
I'm also very new to clojure but this is how I'd do it: (def hr-zones { [0 100] :low [101 120] :fat-burn [121 140] :aerobic [141 160] :anaerobic [161 333] :max}) (defn hr-zone [hr] (some (fn [x] (and (= (first (key x)) hr (second (key x))) (val x))) hr-zones))

Re: Looking for advice to fine tune a simple function

2014-02-16 Thread Andy-
I realized I could use that at some of my code so I wrote it. Not sure if it's the best possible implementation but here it is: (defn quantizer Returns a function that quantizes input data which when called with 'x' returns: o 1st val if-Inf x = 1st bound o 2st val if 1st bound

Re: Looking for advice to fine tune a simple function

2014-02-16 Thread Bruno Kim Medeiros Cesar
I can't claim to be an experienced Clojure developer, specially so regarding maintainability as I'm the only reader of what I write. Andy provided a great piece of code, although I scratched my head for a second or two unwrapping the last two lines. You'll be surprised how often (partition *