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

2 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Jacek Laskowski

ungelesen,
13.10.2010, 16:23:1513.10.10
an 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

Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten