what's the new syntax for (dotimes _ (apply f [i]) (print "*")) ?

4 views
Skip to first unread message

wubbie

unread,
Dec 31, 2008, 11:41:06 PM12/31/08
to Clojure
Hi all,

what's the new syntax for this?
It is part of the code below which was translation by Stu.

; using dotimes instead of repeat from cl
(defn plot [f min max step]
(doseq i (range min max step)
(dotimes _ (apply f [i]) (print "*"))
(println)))

Thanks,
Sun

Chouser

unread,
Jan 1, 2009, 12:37:06 AM1/1/09
to clo...@googlegroups.com
On Wed, Dec 31, 2008 at 11:41 PM, wubbie <sun...@gmail.com> wrote:
>
> Hi all,
>
> what's the new syntax for this?
> It is part of the code below which was translation by Stu.

That's a nifty little function. This just fixes the syntax:

(defn plot [f min max step]
(doseq [i (range min max step)]
(dotimes [_ (apply f [i])] (print "*"))
(println)))

But we can add features and tightens up the implementation a bit:

(defn plot [f & range-args]
(doseq [i (apply range range-args)]
(println (apply str (replicate (f i) "*")))))

This gets rid of the distracting _ and the extra 'println', and also
allows us to use the optional args for 'range':

(plot #(* % %) 8)

--Chouser

wubbie

unread,
Jan 1, 2009, 8:53:41 AM1/1/09
to Clojure
Thanks Chouser,

Happy new year!

sun


On Jan 1, 12:37 am, Chouser <chou...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages