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

-- 
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 patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to