On Feb 3, 8:55 pm, braver <delivera...@gmail.com> wrote:
> Meikel -- cool lazy solution, but doesn't generalize to replace <=
> with a predicate.  Here's my non-lazy, reduce-based one generic with
> instantiations:
>
> (defn clis-pred
>   [pred s]
>   (let [[x & xs] s [r zs _]
>     (reduce (fn [[r zs z] e]
>       (if (pred z e) [r (conj zs e) e] [(conj r zs) [e] e]))
>       [[] [x] x] xs)] (conj r zs)))
>
> (def clis-incr    (partial clis-pred <))
> (def clis-nondecr (partial clis-pred <=))
> (def clis-nonincr (partial clis-pred >=))
> (def clis-decr    (partial clis-pred >))
>
> Cheers,
> Alexy
If your input is a vector, the following will do the trick (relies on
subvec):

(use 'clojure.contrib.seq-utils)
(defn clis-pred [pred vec]
  (->> vec
       (partition 2 1)
       (map reverse)
       (map (partial reduce pred))
       (cons false)
       (positions false?)
       (partition-all 2 1)
       (map (partial apply subvec vec))))

In the general case, partition-by can be used:

(use 'clojure.contrib.seq-utils)
(defn clis-pred [pred seq]
  (->> seq
       (partition 2 1)
       (reductions (fn [id [prev curr]] (if (pred curr prev) id (+ id
1))) 0)
       (map vector seq)
       (partition-by second)
       (map (partial map first))))

Cheers,

Carlos

-- 
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