A streaming library with a superpower: FS2 and functional programming by Daniel Sebban

Many who use Scala will know that it has an exceptional streaming library called FS2, this incredible library incorporates the advantages of functional programming. In this article written by Daniel Sebban, Daniel starts by reviewing what problems FS2 solves and follows onto more including how to use Stream with a DB or any other IO. Have a read and broaden your library horizons.

A streaming library with a superpower: FS2 and functional programming

Scala has a very special streaming library called FS2 (Functional Streams for Scala). This library embodies all the advantages of functional programming (FP). By understanding its design goals you will get exposure to the core ideas that make FP so appealing.

FS2 has one central type: 'Stream[Effect,Output]'

You might get from this type that it’s a 'Stream' and that it emits values of type 'Output'.

The obvious question here is what is 'Effect'? What is the link between 'Effect' and 'Output'? And what advantages does FS2 have over other streaming libraries?

Overview

I will start by reviewing what problems FS2 solves. Then I compare 'List' and 'Stream' with several code examples. After that, I will focus on how to use 'Stream' with a DB or any other IO. That is where FS2 shines and where the 'Effect' type is used. Once you will understand what 'Effect' is, the advantages of Functional Programming should be evident to you.

At the end of this post you will get the answers to the following questions:

What problems can I solve with FS2?

What can I do with 'Stream' that 'List' cannot?

How can I feed data from an API/File/DB to 'Stream'?

What is this 'Effect' type and how does it relate to functional programming?

Note: The code is in Scala and should be understandable even without prior knowledge of the syntax.

What problems can I solve with FS2?

Streaming I/O: Loading incrementally big data sets that would not fit in memory and operating on them without blowing your heap.

Control Flow (not covered): Moving data from one/several DBs/files/APIs to others in a nice declarative way.

Concurrency (not covered): Run different streams in parallel and make them communicate together. For example loading data from multiple files and processing them concurrently as opposed to sequentially. You can do some advanced stuff here. Streams can communicate together during the processing stage and not only at the end.

List vs Stream

'List' is the most well known and used data structure. To get a feel for how it differs from an FS2 'Stream', we will go through a few use cases. We will see how 'Stream' can solve problems that 'List' cannot.

Your data is too big and does not fit in memory

Let’s say you have a very big file (40GB) 'fahrenheit.txt.' The file has a temperature on each line and you want to convert it to 'celsius.txt'.

I still got back a 'Pure'! I gave the 'Stream' a function that affects the world and I still got a 'Pure', not cool ... It would have been neat if FS2 would have detected automatically that the 'loadUserIdByName' function has an effect on the world and returned me something that is NOT 'Pure' but it does not work like that. You have to use a special combinator instead of 'map': you have to use 'evalMap'.

It separated the 'Future' and isolated it! The left side that was the 'Effect' type parameter is now the concrete 'Future' type.

Neat trick, but how does it help me?

You just witnessed true separation of concerns. You can continue to operate on the stream with all the nice 'List' like combinators and you don't have to worry about if the DB is down, slow or all the stuff that is related to the network (effect) concerns.

It all works until I want to use 'toList' to get the values back

scala> userIdsFromDB.toList
<console>:18: error: value toList is not a member of fs2.Stream[scala.concurrent.Future,Long]
userIdsFromDB.toList
^

What???!!! I could swear that I used 'toList' before and it worked, how can it say that 'toList' is not a member of 'fs2.Stream[Future,String]' any more? It is as if this function was removed the moment I started using an effect-ful stream, that's impressive! But how do I get my values back?

First we use 'compile' to tell the 'Stream' to combine all the effects into one, effectively it folds all the calls to 'loadUserIdByName' into one big 'Future'. This is needed by the framework, and it will become apparent why this step is needed soon.

Yes it might be surprising but the major theme in FP is separating the

Description of your program: a good example is the program we just wrote, it’s a pure description of the problem “I give you names and a DB, give me back IDs”

And the

Execution of your program: running the actual code and asking it to go to the DB

One more time our program has literally no effect on the world besides making your computer warm, exactly like our 'Pure' stream.

Code that does not have an effect is called pure and that’s what all Functional Programming is about: writing programs with functions that are pure. Bravo, you now know what FP is all about.

Why would you want write code this way? Simple: to achieve separation of concerns between the IO parts and the rest of our code.

Now let’s fix our program and take care of this 'Future' problem.

As we said 'Future' is a bad 'Effect' type, it goes against the separation of concerns principle. Indeed, 'Future' is eager in Scala: the moment you create one it starts to executes on some thread, you don't have control of the execution and thus it breaks. FS2 is well aware of that and does not let you compile. To fix this we have to use a type called 'IO' that wraps our bad 'Future'.

That brings us to the last part, what is this 'IO' type? and how do I finally get my list of 'usedIds' back?

When '???' is called you will get this exception, it means the function was executed (as opposed to before when we made the point that nothing was really happening). When we implement this function it will go to the DB and load the ids, and it will have an effect on the world (network/files system).

'IO[Long]' is a description of how to get a value of type 'Long' and it most certainly involves doing some I/O i.e going to the network, loading a file,...

It’s the How and not the What. It describes how to get the value from the network. If you want to execute this description, you can use 'unsafeRunSync'(or other functions prefixed 'unsafe'). You can guess why they are called this way: indeed a call to a DB is inherently unsafe as it could fail if, for example, your Internet connection is out.

Recap

Let’s take a last look at 'Stream[Effect,Output]'.

'Output' is the type that the stream emits (could be a stream of 'String', 'Long' or whatever type you defined).

'Effect' is the way (the recipe) to produce the 'Output' (i.e go to the DB and give me an 'id' of type 'Long').

It’s important to understand that if these types are separated to make things easier, breaking down a problem in subproblems allows you to reason about the subproblems independently. You can then solve them and combine their solutions.

The link between these 2 types is the following :

In order for the 'Stream' to emit an element of type

'Output'

It needs to evaluate a type

'Effect'

A special type that encodes an effective action as a value of type 'IO', this 'IO' value allows the separation of 2 concerns:

Description:'IO' is a simple immutable value, it’s a recipe to get a type 'A' by doing some kind of IO(network/filesystem/…)

Execution: in order for 'IO' to do something, you need to execute/run it using 'io.unsafeRunSync'

Putting it all together

'Stream[IO,Long]' says:

This is a 'Stream' that emits values of type 'Long' and in order to do so, it needs to run an effective function that produces 'IO[Long]' for each value.

That’s a lot of details packed in this very short type. The more details you get about how things happen the fewer errors you make.

Takeaways

'Stream' is a super charged version of 'List'

'Stream(1,2,3)' is of type 'Stream[Pure, Int]', the second type 'Int' is the type of all values that this stream will emit

'Pure' means no effect on the world. It just makes your CPU work and consumes some power, but besides that it does not affect the world around you.

Use 'evalMap' instead of 'map' when you want to apply a function that has an effect like 'loadUserIdByName' to a 'Stream'.

'Stream[IO, Long]' separates the concerns of What and How by letting you work only with the values and not worrying about how to get them (loading from the db).

Separating program description from evaluation is a key aspect of FP.

All the programs you write with 'Stream' will do nothing until you use 'unsafeRunSync'. Before that your code is effectively pure.

'IO[Long]' is an effect type that tells you: you will get 'Long' values from IO (could be a file, the network, the console ...). It's a description and not a wrapper!r

'Future' does not abide by this philosophy and thus is not compatible with FS2, you have to use 'IO' type instead.

Archive

Signify Technology is an innovative technology recruitment business with our head office based in the heart of London. We provide permanent and contract technology recruitment solutions to a wide range of the world's leading brands on a global basis including blue chip, technology companies and start-ups. Behind each piece of technology changing the world, is a person, it's our mission to introduce them to the companies that need them the most.