Clojure - Sequences take-last



Takes the last list of elements from the sequence.

Syntax

Following is the syntax.

(take-last num seq1)

Parameters − ‘seq1’ is the sequence list of elements. ‘num’ is the number of elements which needs to be included in the sequence from the end.

Return Value − A new sequence of elements with only the end number of elements included.

Example

Following is an example of take-last in Clojure.

(ns clojure.examples.example
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def seq1 (seq [5 4 3 2 1]))
   (def seq2 (take-last 2 seq1))
   (println seq2))
(Example)

Output

The above program produces the following output.

(2 1)
clojure_sequences.htm
Advertisements