Skip to content

Instantly share code, notes, and snippets.

@ocanbascil
Created November 26, 2014 14:46
Show Gist options
  • Save ocanbascil/c04d108d1df1c1875d46 to your computer and use it in GitHub Desktop.
Save ocanbascil/c04d108d1df1c1875d46 to your computer and use it in GitHub Desktop.
Clojure Dojo London, 24th Nov 2014, 4clojure
;;http://www.4clojure.com/problem/144
;;Write an oscillating iterate: a function that takes an initial value and
;;a variable number of functions. It should return a lazy sequence of the
;;functions applied to the value in order, restarting from the first function after it hits the end.
(fn [n & funcs] (reductions #(%2 %1) n (cycle funcs)))
;;http://www.4clojure.com/problem/110
;;Write a function that returns a lazy sequence of "pronunciations" of a sequence of numbers.
;;A pronunciation of each element in the sequence consists of the number of repeating identical
;;numbers and the number itself. For example, [1 1] is pronounced as [2 1] ("two ones")
;;which in turn is pronounced as [1 2 1 1] ("one two, one one").
;;Your function should accept an initial sequence of numbers, and return an infinite lazy
;;sequence of pronunciations, each element being a pronunciation of the previous element.
(fn [n]
(rest (iterate
(fn [n] (mapcat #(vector (count %) (first %))(partition-by identity n))) n)))
;;http://www.4clojure.com/problem/78
;;The trampoline function takes a function f and a variable number of parameters.
;;Trampoline calls f with any parameters that were supplied. If f returns a function,
;;trampoline calls that function with no arguments. This is repeated, until the return
;;value is not a function, and then trampoline returns that non-function value. This is useful for
;;implementing mutually recursive algorithms in a way that won't consume the stack.
(fn tr [f & args]
(if (ifn? f)
(tr (apply f args)) f)) ;;TODO: Use recur
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment