pure function

Looking at some differences between Scalaz Task and Scala Future

Table of Contents1 - The Scala Future example2 - The Scalaz Task example3 - What happens when you call the Future twice?4 - What happens when you call the Task twice?5 - Is one approach correct?6 - Source code

Some time ago I was searching for something and came across this Reddit thread about this tweet from Timothy Perrett, who leads Scala teams at Verizon:

“The fact that #scala Future is not lazy just blows my mind. After years of using Scalaz Task, Future is now totally unusable.”

The last part of that tweet is a bit of hyperbole to me, as I’ve been using the Scala Future for a long time myself, and I’ve had no problems using it. That being said, the examples at the top of the Reddit page were interesting, so I decided to try to understand the differences.

This is a page from my book, Functional Programming, Simplified

Pure Function Signatures Tell All

“In Haskell, a function’s type declaration tells you a whole lot about the function, due to the very strong type system.”

Learn You a Haskell for Great Good!

One thing you’ll find in FP is that the signatures of pure functions tell you a lot about what those functions do. In fact, it turns out that the signatures of functions in FP applications are much more important than they are in OOP applications. As you’ll see in this lesson:

This is a page from my book, Functional Programming, Simplified

The “Great FP Terminology Barrier”

“They say no ship can survive this.”

Hikaru Sulu, talking about “The Great Barrier”
(Star Trek V: The Final Frontier)

A short excursion to ... The Twilight Zone

Hello, Rod Serling of The Twilight Zone here. Al will be back shortly, but for now, let me take you to another place and time ... an alternate universe ...

This is a page from my book, Functional Programming, Simplified

Rules for Programming in This Book

“Learn the rules like a pro, so you can
break them like an artist.”

Pablo Picasso

“Learn the rules, and then forget them.”
Haiku Master Matsuo Basho

Alright, that’s enough of the “preface” material, let’s get on with the book!

As I wrote earlier, I want to spare you the route I took of, “You Have to Learn Haskell to Learn Scala/FP,” but, I need to say that I did learn a valuable lesson by taking that route:

This is a page from my book, Functional Programming, Simplified

Question Everything

“I have no special talent. I am only passionately curious.”

Albert Einstein

A Golden Rule of this book is to always ask, “Why?” By this I mean that you should question everything I present. Ask yourself, “Why is this FP approach better than what I do in my OOP code?” To help develop this spirit, let’s take a little look at what FP is, and then see what questions we might have about it.