For what it’s worth, I gave your problem a try and come up with the following 
implementation based on Mark Engelberg’s suggestion.  It should do less 
generating work up front so you don’t have to filter unwanted results.  This 
code depends on `combinations` preserving the order of the items.  I’m not sure 
that’s guaranteed, but the current implementation works that way, which is 
convenient.  My `sized-subsets` is a modified version of the combinatorics 
`subsets` that limits the results to the inclusive min and max counts given.

By the way, if you’re using subvectors you should be aware of bug CLJ-2065, 
reduce-kv fails on subvectors.  There’s an easy work-around in the bug report.

http://dev.clojure.org/jira/browse/CLJ-2065 
<http://dev.clojure.org/jira/browse/CLJ-2065>

(require '[clojure.math.combinatorics :as mc])

;; inclusive sizes
(defn sized-subsets [items min-count max-count]
  (mapcat (fn [n] (mc/combinations items n))
          (range min-count (inc max-count))))


(defn subv-ordered-partitions [coll & {from :min to :max}]
  (let [v (vec coll)
        cnt (count v)
        smin (dec (or from 1))
        smax (dec (or to cnt))]
    (map (fn [splits]
           (map (fn [start end] (subvec v start end))
                (conj splits 0)
                (concat splits (list cnt))))
         (sized-subsets (range 1 cnt) smin smax))))

Steve Miner


> On Mar 16, 2017, at 12:59 PM, Paul Gowder <paul.gow...@gmail.com> wrote:
> 
> For sake of completeness/if this is useful for anyone else, a full 
> implementation of the number-the-possible-split-locations method, including 
> the original API with :min and :max options. Could probably be tidied up with 
> transducers and such for all those filters but does the job. 
> 
> (require '[clojure.math.combinatorics :as c])
> (defn breaks->partition 
>  ([v brks]
>   (breaks->partition 0 [] v brks))
>  ([start pars v brks]
>   (if (empty? brks)
>     (conj pars (subvec v start (count v)))
>     (let [this-part (subvec v start (first brks))]
>       (recur (first brks) (conj pars this-part) v (rest brks))))))
> 
> (defn min-parts [min splits]
>  (>= (count splits) (- min 1)))
> 
> (defn max-parts [max splits]
>  (<= (count splits) (- max 1)))
> 
> (defn ordered-partitions [v & {:keys [max min]}]
>  (let 
>    [s (c/subsets (range 1 (count v)))
>     fs (cond
>          (and max min) 
>          (filter 
>            (partial max-parts max) 
>            (filter (partial min-parts min) s))
>          max (filter (partial max-parts max) s)
>          min (filter (partial min-parts min) s)
>          :else s)]
> (map (partial breaks->partition v) fs)))
> 
> It does, alas, take more than 10 times as long as Mike's version.  Which 
> proves that one should never try to do anything faster than the core.matrix 
> guy.  :-) 

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to