Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

sequence method
clojure
community creator

What is the cons method in the Clojure sequence?

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

A sequence is simply a logical list and an immutable object. We use the seq method to create a sequence.

In Clojure, the cons method adds a data item to a sequence and returns a new list.

Syntax

(cons element seqq)

Parameter

We pass two parameters to the cons method:

  • seq: This represents the list of items.
  • element: This represents the data items to be added.

Return value

The cons method will return a list of data items.

Example

(ns clojure.examples.example
   (:gen-class))
(defn Conss []
   (println (cons 6 (seq [11, 54,9]))))
(Conss)
The cons method example

Explanation

  • Line 3: We define a function, Conss.
  • Line 4: We print the new sequence using println(). The cons method adds data items 6 to the list.
  • Line 5: We call the Conss function.

RELATED TAGS

sequence method
clojure
community creator

CONTRIBUTOR

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring