Seen in #clojure – October 7th w/ maybe-m monad

2 views
Skip to first unread message

Jacek Laskowski

unread,
Oct 13, 2010, 4:23:15 PM10/13/10
to clo...@googlegroups.com
Hi,

I was reading the blog entry Seen in #clojure – October 7th [1] and
couldn't stop thinking it might be easier with monads. Since I'm new
to monads, Clojure and functional programming in general I'd
appreciate any comments on my solution, esp. with regards to maybe-m
monad's use. I guess there should be a way to ease weeks, days, hours
and minutes functions since they all share the same structure.

Any tips on doing it in a Clojure idiomatic way *with* monads are
greatly appreciated. Thanks.

(defn weeks
[v]
(let [divider (* 60 24 7)]
[(int (/ v divider))(rem v divider)]))

(defn days
[v]
(let [divider (* 60 24)]
[(int (/ v divider))(rem v divider)]))

(defn hours
[v]
(let [divider 60]
[(int (/ v divider))(rem v divider)]))

(defn minutes [v]
(second (hours v)))

(defn expand-minutes
[i]
(domonad maybe-m
[[ms remaining] (weeks i)
[ds remaining] (days remaining)
[hs remaining] (hours remaining)]
(println ms "weeks" ds "days" hs "hours" remaining "minutes")))

[1] http://matthewm.net/blog/2010/10/13/seen-in-clojure-october-7th

Jacek

--
Jacek Laskowski
Notatnik Projektanta Java EE - http://jaceklaskowski.pl

Reply all
Reply to author
Forward
0 new messages