Created
April 28, 2011 16:52
-
-
Save rubysolo/946740 to your computer and use it in GitHub Desktop.
my-nth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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