What is the first method in Clojure sequence?

What is a sequence?

A sequence is a data storage structure that holds a collection of data objects. In Clojure, it can be seen as a logical list. The seq method is used to create a sequence.

What is the firstmethod in Clojure sequence?

The first sequence method returns the first element of a sequence.

Syntax

(first seqq)
first sequence syntax

Parameters

seqq: This represents the sequence containing the element.

Return value

This method returns the first value of a sequence.

Example

(ns clojure.examples.example
(:gen-class))
(defn firstt []
(def seq1 (seq [1 2 8 9 1 0 6 8 2]))
(println (first seq1)))
(firstt)

Explanation

  • Line 3: We define a function firstt.
  • Line 4: We define our sequence seq1.
  • Line 5: We use the first method to get the first value of the seq1.
  • Line 6: We call the firstt function. Observe that the output returned "1" as it is the first value of the seq1 sequence.

Free Resources