O'Reilly logo

Learning Functional Data Structures and Algorithms by Raju Kumar Mishra, Atul Khot

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Complexities and collections

Let's look at collections and see how complexity helps us to see how they will perform in different situations. We will look at commonly used collections and idioms.

The sliding window

The sliding method allows us to create a sliding window. Here's an example:

scala> val list = List(1,2,3,4,5,6) 
list: List[Int] = List(1, 2, 3, 4, 5, 6) 
 
scala> val list1 = list.sliding(2,1).toList 
list1: List[List[Int]] = List(List(1, 2), List(2, 3), List(3, 4), List(4, 5), List(5, 6)) 

This creates List[Int]. Each element of List contains the current element and its successor in the original list.

Here is the equivalent code in Clojure:

user=> (partition 2 1 '(1 2 3 4 5 6)) 
((1 2) (2 3) (3 4) (4 5) (5 6)) 

It gives us similar results:

Let's ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required