Clojure – Sequences

Clojure sequences

In this guide, we will discuss Sequences in Clojure. 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
1cons
Returns a new sequence where ‘x’ is the first element and ‘seq’ is the rest.
2conj
Returns a new sequence where ‘x’ is the element that is added to the end of the sequence.
3concat
This is used to concat two sequences together.
4distinct
Used to only ensure that distinct elements are added to the sequence.
5reverse
Reverses the elements in the sequence.
6first
Returns the first element of the sequence.
7last
Returns the last element of the sequence.
8rest
Returns the entire sequence except for the first element.
9sort
Returns a sorted sequence of elements.
10drop
Drops elements from a sequence based on the number of elements, which needs to be removed.
11take-last
Takes the last list of elements from the sequence.
12take
Takes the first list of elements from the sequence.
13split-at
Splits the sequence of items into two parts. A location is specified at which the split should happen.

Next Topic : Click Here

This Post Has One Comment

Leave a Reply