Functional Programming in Rust - Part 2 : Functional Combinators
Rust is a new system programming language developed at mozilla. It is a competitor to C and C++ with machine level access and no gc. But it’s not just better C.It brings many novel features of higher level languages like Java, Scala to system level programming.This combination of low level access, speed of C combined with flexibility and expressiveness of functional programming language like scala makes it very interesting language.
In this series of blog posts, I will discuss how to do functional programming in rust. Most of the examples are inspired from scala, as I use scala in my every day work. If you are new to rust, I highly recommend Rust Book as a starting point. Use the book to install rust on your machine and familiarise with basic syntax.
This is the second blog in the series which focuses on the functional combinators. You can access all the posts in series here.
TL;DR You can access complete code on github.
Functional Combinators
Functional combinators are anonymous functions which allows us to manipulate collection of objects in a elegant manner. In functional languages like Scala, we use combinators like map,flatMap extensively to manipulate collection API’s.
As rust supports functions as first class objects, we can manipulate collections like arrays, vectors using functional combinator rather than using typical loops structure.
Iterator Trait
All functional combinators in rust are defined on the Iterator trait. All the built in collections like arrays, vectors implement the iterator trait.
Laziness
One of the important feature of rust iterators are they are lazy in nature. So whenever we apply any combinator, it will not execute until we call a specific action. We will see more of this in our examples.
Defining a Vector
To use function combinators we need to have access to collection. So we are defining a vector in below code
Map Combinator
In above code, first we access iterator using iter() method. Then we call our map combinator and pass a closure to it.
As we discussed earlier, map is lazy. So we need to call collect in order to force the computation. Also when we call collect, we need to specify the type it returns.
This collect API is similar to Spark’s collect RDD API.
The following code snippets follow same patterns as map combinator to achieve different manipulations on vector.
Filter
Count
Zip with Index
Fold
Max
In max, rust returns an option value. We use unwrap method to get the value from option. It’s similar to get method on scala option.
For All
FlatMap
You can access complete code here.
Running code
You can run all the examples using the below command
So in this post, we looked at how to use functional combinators to manipulate collections in rust.