Clojure Tutorial on Clojure Sequences

sequences are created with the help of the ‘seq’ command. following is a simple example of a sequence creation.

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

;; this program displays hello world
(defn example []
   (println (seq [1 2 3])))
(example)

the above program produces the following output.

(1 2 3)

following are the various methods available for sequences.

sr.no. methods & description
1 cons

returns a new sequence where ‘x’ is the first element and ‘seq’ is the rest.

2 conj

returns a new sequence where ‘x’ is the element that is added to the end of the sequence.

3 concat

this is used to concat two sequences together.

4 distinct

used to only ensure that distinct elements are added to the sequence.

5 reverse

reverses the elements in the sequence.

6 first

returns the first element of the sequence.

7 last

returns the last element of the sequence.

8 rest

returns the entire sequence except for the first element.

9 sort

returns a sorted sequence of elements.

10 drop

drops elements from a sequence based on the number of elements, which needs to be removed.

11 take-last

takes the last list of elements from the sequence.

12 take

takes the first list of elements from the sequence.

13 split-at

splits the sequence of items into two parts. a location is specified at which the split should happen.