Skip to content

Instantly share code, notes, and snippets.

@tomjack
Created April 19, 2011 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjack/5b709e90dbb330d1fb62 to your computer and use it in GitHub Desktop.
Save tomjack/5b709e90dbb330d1fb62 to your computer and use it in GitHub Desktop.
(comment
user> (stabilize-by identity 3 [1 2 2 3 3 3 4 5 6 7])
3
user> (stabilize-by identity 3 [1 2 2 3 3 4 5 6 7])
nil
user> (stabilize-by #(* % %) 3 [1 -1 2 -2 2 3 3 4 5 6 7])
2)
(defn stabilize-by [f n s]
(when (seq s)
(let [fs (map f s)
[part rest] (split-with (comp #{(first fs)} f) s)]
(if (= n (count (take n part)))
(first part)
(recur f n rest)))))
(defn stabilize-by [f n s]
(->> s
(partition-by f)
(filter #(>= (count %) n))
first first))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment