Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

scala

What is tabulate in Scala?

Samia Ishaque

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

The tabulate method in Scala creates a list of specific dimensions filled by the desired values. The general syntax for tabulating a list is as follows:

List.tabulate(dimensions)(elements)

The tabulated list can have multi-dimensions, and the elements can be calculated using mathematical equations.

For example, if we wanted to fill the list with values that are three times the index, elements would be input as n=>n*3.

Parameters

There are two compulsory input parameters for the tabulate method: 1) the dimensions of the list tabulated and 2) the elements you wish to fill the list with.

Return value

The tabulate method returns a tabulated list.

Example

Below is an example that will help us understand how to use the tabulate method. We want to generate two lists of size 5 with elements that are multiples of 3. To do this, we keep the dimensions as (2,5) and elements such that they multiply their indexes with 3.

object Main extends App {
val x = List.tabulate(2,5)(_*_*3)
print(x)
}

The following is a similar example for one-dimensional lists. It tabulates a list with five elements that are all multiples of 3.

object Main extends App {
val x = List.tabulate(5)(x=>x*3)
print(x)
}

RELATED TAGS

scala

CONTRIBUTOR

Samia Ishaque
Copyright ©2022 Educative, Inc. All rights reserved

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