What is the drop 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 build a sequence.

What is the drop method in Clojure Sequence?

The drop sequence method will return a sequence with the remaining element after removing a specified number of elements from the beginning of the sequence.

Use case

This method can be used to remove elements in a sequence.

Syntax

(drop number seqq)
Syntax of drop sequence

Parameters

This method takes the following parameters:

  • seqq: This represents the sequence containing element.
  • number: This is the number of elements you wish to remove from the sequence, starting from the first element.

Return value

This method returns a sequence containing the elements left after the removal.

Example

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

Explanation

  • Line 3: We define a function dropp.
  • Line 4: We define our sequence seq1.
  • Line 5: We use the drop method to remove 3 elements from the sequence because according to the syntax, we pass the number of elements we want to remove, and in the above example we passed 3 . So, elements 1, 2, and 8 will be removed, as we would see in the output.
  • Line 6: We call the dropp function, and we observe that the output returned all the elements of the sequence except for the first three elements, which are 1,2, and 8.

Free Resources