Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clojure find last element without using last function

Tags:

clojure

I'm learning clojure and have been using 4clojure.com to get better. I just completed #19 but it seems like maybe I haven't done it quite as the author's have anticipated - like I've perhaps missed the point somehow.

4 clojure problem 19

Given the constraint that you cannot use the last function does this seem like a reasonable solution?

#(.get %(- (count %) 1)) 
like image 457
javamonkey79 Avatar asked Nov 25 '11 04:11

javamonkey79


1 Answers

If you're going to use:

#(first (reverse %)) 

You might as well compose the function with "comp":

(comp first reverse) 

This is probably a little more idiomatic and easier to read. The same caveat about "reverse" not being lazy applies here.

like image 142
mudphone Avatar answered Sep 29 '22 18:09

mudphone