On Dec 15, 4:05 pm, Laurent PETIT <laurent.pe...@gmail.com> wrote:
> Hello,
>
> it seems to me that your example is unnecessary complicated.
> Let's refactor it a bit before trying to obtain your goal.
>
> First,
>
> your example can be, for the purpose of your goal, simplified as :
>
> (loop [a a0]
>   (if (predicate-fn a)
>     (return-fn a)
>     (recur (recur-fn a))))
>
> So now, what can we do with this ?
>
> you keep applying function recur-fn to a, starting with a = a0.
> This is a job for iterate : (iterate recur-fn a0) will create this lazy
> sequence starting with a0, and where each new value is made from (recur-fn
> a)
>
> Then you want to stop when predicate-fn returns true.
> This is a job for remove, for example : (first (remove (comp not
> predicate-fn) (iterate recur-fn a0)))
>
> The final step is to apply return-fn to the result:
> (return-fn
>   (first (remove
>                     (comp not predicate-fn)
>                     (iterate recur-fn a0)))
>

Shouldn't this be:

 (return-fn
   (first (filter predicate-fn (iterate recur-fn a0)))


> As for return-fn, predicate-fn and recur-fn :
>
> (def b f1)
> (def c (comp f2 b))
> (def d (comp f3 c))
> (def e (comp f4 d))
> (def g (comp f5 c))
> (def h (comp f5 f2))
>
> (def return-fn e)
> (def predicate-fn #(if (or (f6? (b %)) (<= (g %) (h %))))
> (def recur-fn #(f7 (d a) (b a)))
>
> Is it something like that you expected ?
>
> 2009/12/15 samppi <rbysam...@gmail.com>
>
> > I'm trying to rewrite a loop to use higher-level functions instead.
> > For pure functions f1, f2, f3, f4, f5, f6?, and f7, and a Clojure
> > object a0, how can one rewrite the following loop to use map, reduce,
> > etc.?
>
> >  (loop [a a0]
> >    (let [b (f1 a)
> >          c (f2 b)
> >          d (f3 c)
> >          e (f4 d)
> >          g (f5 c)
> >          h (-> e f2 f5)]
> >      (if (or (f6? b) (<= g h))
> >        e
> >        (recur (f7 d b)))))
>
> > --
> > 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<clojure%2bunsubscr...@googlegroups.com>
> > 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 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