

It takes in two parameters iter and f, and returns something that is of types FlatMap. Here we're defining a function called flat_map. pub fn flat_map (iter : I, f : F ) -> FlatMap To begin with, let's write down the function definition for flat_map. The function should continue to iterate over the rest of the list of chars, even if it encounters an empty string.

The second one augments it by add empty strings in between. Using collect() and type coercion, this iterator is collected into a String and asserted with the expected output. Then it should flatten each iterator in the list resulting in an iterator yielding chars. The expected behavior of running flat_map using the specified closure is that it should run chars() on each input element, resulting in a list of iterators over the characters in each str. The first one is straight from the standard library documentation, but adapted to our api. The last two tests are the most interesting ones. Each one executes flat_map and checks if it returns the current number of elements.
RUST THE METHOD MAP EXISTS BUT CODE
This ensures that tests don't get compiled along with non-test code when running cargo build. The specific types of these parameters will be discussed later It takes in an iterator and a closure, and returns something that can be turned into an iterator.

Monads themselves are a very interesting concept and are very heavily used by the functional programming community to handle "impurity". The concept of flat_map gets used very often when attempting to explain the mystical concept of monads. To compute a flat_map, first map the collection, and then flatten it. Now that we've understood what flat and map mean separately, understanding flat_map is a piece of cake. In Rust, the standard library function flatten will remove only one level of nesting. In Javascript's, you can specify how deep a nested structure should be flattened by passing in a depth parameter. The level of nesting removed depends on the implementation of the function. To put it simply, flat removes nesting from collections. Let's say I have a list of elements $A =, ]$. To put it more formally, given a collection $$ and a function $f: a \rightarrow b$, $map() = $, where $a$ and $b$ are types.Īlright, now what's the deal with flat? Let's try to understand it through an example. For every element in a collection, apply a function that transforms the element, collect the transformed values, and return the transformed collection. Most of us are familiar with the concept of mapping. I'm assuming that you've coded in Rust before and are familiar with iterators and traits. Then I'll show you how I implemented it, explaining my thought process along the way. So, here we are!įirst let's try to understand what flat_map is and why it's useful. In the stream, he recommends implementing flat_map as a way to better understand traits. He implemented the standard library function flatten and along the way explained bits and pieces of Rust's trait system.

Not too long ago I happened to watch Crust of Rust stream on iterators.
