Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i get the second to last element of a Sequence? Clojure

i know how to get the last element with the function last but is it possible to get the second to last element of a Sequence?

(defn last
  [args]
   (last args))
(last [1 2 3 4]) ;;--> 4 but i want it to return 3

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use reverse or take-last:

(defn second-to-last [s]
  (second (reverse s)))

(second-to-last (range 100))
=> 98

(defn second-to-last [s]
  (first (take-last 2 s)))

(second-to-last (range 100))
=> 98
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading