Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

take-last method
clojure
clojure sequence
communitycreator

What is the take-last 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.

Answers Code

Overview

A sequence is a data storage structure in Clojure. It holds a collection of data objects and can be seen as a logical list. The seq method is used to build a sequence.

In Clojure, the take-last sequence method will return a sequence containing the number of selected elements. This method can select elements from right to left instead of left to right for the initial sequence.

Syntax

(take-last number seqq)
take-last sequence syntax

Parameters

This method takes the following parameters:

  • seq: This represents the sequence containing element.
  • number : This is the number of elements you wish to select from the sequence starting from the last element

Return value

This method returns a sequence containing the selected elements.

Example

(ns clojure.examples.example
(:gen-class))
(defn func []
(def seq1 (seq [1 2 8 9 1 0 6 8 2]))
(println (take-last 3 seq1)))
(func)
Example on take-last method in clojure sequence

Explanation

  • Line 3: We define a function func.
  • Line 4: We define our sequence seq1.
  • Line 5: We use the take-last method to select 3 last elements from the sequence. According to the syntax, we pass the number of elements we want to select and in the example above we passed 3 . So elements 6,8, and 2 will be selected as we would see in the output.
  • Line 6: We call the func function. Observe that the output returned all the elements of the sequence selected starting from the right side of the element to the left 6,8 and 2.

RELATED TAGS

take-last method
clojure
clojure sequence
communitycreator

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.

Answers Code
Keep Exploring