Skip to content

Instantly share code, notes, and snippets.

@juergenhoetzel
Created June 11, 2010 20:49
Show Gist options
  • Save juergenhoetzel/435032 to your computer and use it in GitHub Desktop.
Save juergenhoetzel/435032 to your computer and use it in GitHub Desktop.
(defn calc-slice-count [thing]
"Calculates the number of possible slices using the formula:
(n + r - 1)!
------------
r!(n - 1)!
where n is (count thing) and r is 2"
(let [! #(reduce * (take % (iterate inc 1)))
n (count thing)
r 2]
(/ (! (- (+ n r) 1))
(* (! r) (! (- n 1))))))
(extend-type String
Sliceable
(slice [this s e] (.substring this s (inc e)))
(sliceCount [this] (calc-slice-count this)))
(slice "abc" 0 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment