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 thesequencecontaining 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
dropmethod to remove3elements from the sequence because according to thesyntax, we pass the number of elements we want to remove, and in the above example we passed3. So, elements1,2, and8will be removed, as we would see in the output. - Line 6: We call the
droppfunction, and we observe that the output returned all the elements of the sequence except for the first three elements, which are1,2, and8.