public
Created

my-nth

  • Download Gist
my-nth.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
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))
))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.