Skip to content

Instantly share code, notes, and snippets.

@unthingable
Last active March 22, 2016 19:17

Revisions

  1. unthingable revised this gist Jan 27, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions euclidean.clj
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,8 @@

    (defn recombine
    "Distribute tail: [[1] [1] [1] [0] [0]] -> [[1 0] [1 0] [1]]"
    ([a b] (list a b))
    ([a b c] (list a b c))
    ([a b] [a b])
    ([a b c] [a b c])
    ([a b c & more]
    (let [s (concat [a b c] more)
    [head tail] (split-seq s)
    @@ -20,7 +20,7 @@

    (defn E [k n]
    "Evenly distribute K beats among N subdivisions"
    (let [seed (concat (repeat k '(1)) (repeat (- n k) '(0)))]
    (let [seed (concat (repeat k [1]) (repeat (- n k) [0]))]
    (flatten (apply recombine seed))))


  2. unthingable revised this gist Jan 27, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions euclidean.clj
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,10 @@
    (defn split-seq [s]
    "Extract a tail of all same elements: [1 1 0 0 0] -> [[1 1] [0 0 0]]"
    (let [l (last s)]
    (split-with #(not= l %) s)))

    (defn recombine
    "Distribute tail: [[1] [1] [1] [0] [0]] -> [[1 0] [1 0] [1]]"
    ([a b] (list a b))
    ([a b c] (list a b c))
    ([a b c & more]
    @@ -17,6 +19,7 @@
    (drop r-len (drop-last r-len s))))))))

    (defn E [k n]
    "Evenly distribute K beats among N subdivisions"
    (let [seed (concat (repeat k '(1)) (repeat (- n k) '(0)))]
    (flatten (apply recombine seed))))

  3. unthingable created this gist Jan 27, 2014.
    39 changes: 39 additions & 0 deletions euclidean.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    (defn split-seq [s]
    (let [l (last s)]
    (split-with #(not= l %) s)))

    (defn recombine
    ([a b] (list a b))
    ([a b c] (list a b c))
    ([a b c & more]
    (let [s (concat [a b c] more)
    [head tail] (split-seq s)
    recombined (map concat head tail)
    r-len (count recombined)]
    (if (empty? head) ;; even pattern (all supbatterns same)
    s
    (apply recombine (concat
    recombined
    (drop r-len (drop-last r-len s))))))))

    (defn E [k n]
    (let [seed (concat (repeat k '(1)) (repeat (- n k) '(0)))]
    (flatten (apply recombine seed))))



    ;; output
    > (E 3 6)
    (1 0 1 0 1 0)
    > (E 3 8)
    (1 0 0 1 0 0 1 0)
    > (E 4 6)
    (1 0 1 1 0 1)
    > (E 3 8)
    (1 0 0 1 0 0 1 0)
    > (E 2 6)
    (1 0 0 1 0 0)
    > (E 3 4)
    (1 0 1 1)
    > (E 5 11)
    (1 0 0 1 0 1 0 1 0 1 0)