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 :hr-zone-5}]
And function:
(defn hr-zone
"Return the HR zone as a keyword according to the bpm value"[bpm zones]
(:zone-key (first (filter #(<= bpm (:zone-max %)) (sort-by :zone-max
zones)))))
That's just over my parentheses-parsing comfort level, so I'd probably
thread it for readability:
(defn hr-zone
"Return the HR zone as a keyword according to the bpm value"[bpm zones] (->>
zones
(sort-by :zone-max)
(filter #(<= bpm (:zone-max %)))
(first)
(:zone-key)))
This returns nil if given a bpm higher than the defined zones, and it
considers negative heart rates to be in zone one, so you might want to
handle the boundaries differently.
Happy coding,
Johanna
On Monday, February 17, 2014 6:00:40 AM UTC-8, Laurent Droin wrote:
>
> 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. I like it but it will take some practice and your feedback really
> helps. It looks like I'm going to spend more time on it. I'll update.
>
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.