Skip to content

Instantly share code, notes, and snippets.

@afsalthaj
Last active April 23, 2019 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afsalthaj/44927e733fa430714bf25abe5411b79e to your computer and use it in GitHub Desktop.
Save afsalthaj/44927e733fa430714bf25abe5411b79e to your computer and use it in GitHub Desktop.

My note on scalaz.Task approaches. This is mainly for old code!!!!

I have found wrong usages of scalaz.Task in old programs.

Why this is important?

When you bring in FP ornaments into your code, it comes with certain cost. The first and formost is additional learning curve for new developers to come in and understand the concepts of total, side effect free, lazy descriptions of computations. This has its advantage of being able to ship robust code with equational reasoning. But what if the usage of FP constructs itself is wrong?

Most of the old scala-FP code has the usages of scalaz.Task which is one of the most popular FP datastructure then. It's good to use it even today although there are typelevel libraries and ZIO providing better choices - cats.IO, ZIO, Monix.Task

scalaz.Task tips

Tip 1

Just not to get things hairy, its good to make things work with Task.delay which is basically () => Try, and not Task.apply which push the computations to async boundaries.

Tip 2

Task.now disallows all that we could do with Task as it is strict evaluation and executes only once ! Probably that's not what we want either.

Tip 3

Task.delay will keep things simple by making computations within current thread. If we think we really need to push comp to async, wrap it in Task.fork, and if not it will wait for forked comp to join by default.

Maintaining an existing FP code written in scalaz.Task

Can I just ditch this old FP code ?

Yes:

If you find an entire set of compositions done using Task.now.

If there isn't even one property based test / laws hanging around

If there isn't an attempt made anywhere in the whole program ( Either, MonadError, try/catch etc etc)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment