Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Learning Functional Data Structures and Algorithms

You're reading from   Learning Functional Data Structures and Algorithms Learn functional data structures and algorithms for your applications and bring their benefits to your work now

Arrow left icon
Product type Paperback
Published in Feb 2017
Publisher Packt
ISBN-13 9781785888731
Length 318 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
 S. Khot S. Khot
Author Profile Icon S. Khot
S. Khot
 Mishra Mishra
Author Profile Icon Mishra
Mishra
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Learning Functional Data Structures and Algorithms
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
1. Why Functional Programming? FREE CHAPTER 2. Building Blocks 3. Lists 4. Binary Trees 5. More List Algorithms 6. Graph Algorithms 7. Random Access Lists 8. Queues 9. Streams, Laziness, and Algorithms 10. Being Lazy - Queues and Deques 11. Red-Black Trees 12. Binomial Heaps 13. Sorting

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 see the complexity of this sliding window code. The time complexity is O(n). We need to visit each element of the original list twice: once when it is the first element, and second, when it is the second element. This is clearly...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images