Skip to content

Instantly share code, notes, and snippets.

@rubysolo
Created April 28, 2011 16:52
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 rubysolo/946740 to your computer and use it in GitHub Desktop.
Save rubysolo/946740 to your computer and use it in GitHub Desktop.
my-nth
user=> (defn my-nth [s n] (first ((apply comp (repeat n rest)) s)))
#'user/my-nth
user=> (my-nth '(4 5 6 7) 2)
6
---
On 4clojure:
#21:
(= (__ '(4 5 6 7) 2) 6)
My entry:
(fn my-nth [s n] (first ((apply comp (repeat n rest)) s)))
java.security.PrivilegedActionException: java.lang.IllegalArgumentException: Wrong number of args (0) passed to: core$comp (NO_SOURCE_FILE:0)
---
In the REPL:
user=> (= ((fn my-nth [s n] (first ((apply comp (repeat n rest)) s))) '(4 5 6 7) 2) 6)
true
---
comp requires at least one argument (thx amalloy)
which might be considered a bug:
http://blog.fogus.me/2010/08/18/monkeying-with-clojures-comp-function/
fixed version:
(defn my-nth [s n]
(if (= 0 n)
(first s)
(first ((apply comp (repeat n rest)) s))
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment