What is "effect" or "effectful" mean in Functional Programming?

Photo by pawel szvmanski

A lot of the time, when we discuss the effect, we usually talk about side-effect. However, as I study more and more into functional programming and reading more and more functional programming books, I noticed many times “Effect” or “Effectful” had been widely said in the FP community when describing abstract things.

I dig a little deeper into what an “Effect” or “Effectful” means and put that in this blog post for a note to my future self.

It is not Side Effect

Usually, what they meant for “Effect” or “Effectful” is no side effect (sometimes it does). It is Main Effect.

It has something to do with Type Category

A type category is a Math Structure to abstract out representation for all the different fields in Math. When designing a program, we can think in the properties of that program before writing code instead of the other way around. For example, a function sum can be empty (identity law), has the property of combined operation and needs to be associative. (1+2 is equal to 2+1). We can characterize them as Monoid, and restrict input function to be a Monoid. This way, we can create a solution in a systematic approach that generates fewer bugs.

Within Type Category, there is Monad, which is a fancy word for a wrapper that produces an “effect” on a given type. I will quote the statement that Alvin Alexander mentioned in Functional and Reactive Domain Modeling:

  1. Option models the effects of optionality
  2. Future models latency as an effect
  3. Try abstract the consequences of failures

Those statements can be rewritten as:

  1. Option is a monad that models the effect of optionality (of being something optional)
  2. Future is a monad that models the impact of latency
  3. Try is a monad that models the impact of failures (manages exception as an effect)

Similarly:

  1. Reader is a monad that models the effect of composting operations based on some input.
  2. Writer is a monad that models the impact of logging
  3. State is a monad that models the impact of State
  4. Sync in Cats-effect is a monad that models the effects of synchronous lazy execution.

It is an F[A] instead of A

An effect can be said of what the monad handles.

Quoting from Rob Norris in Functional Programming with Effects - an effectual function returns F[A] rather than A.

For example:

def division(num1:Int, num2:Int): Either[Exception, Int] = if(num2 == 0) Left(new IllegalStatement("num1 cannot divide by zero")) else Right(num1/num2)

By looking at the code above, we know that without diving through the code, the function can have an “effect” of returning some Exception or integers based on the input type. By explicitly declaring what effect your function can return, other tasks that call division will need to supply a mechanism to handle those “effect” types, making your program deterministic.

"I don’t remember where I read it, but someone said that if you want to think about it philosophically, when a function returns an A, that A has already been fully evaluated; but if that function returns F[A] instead, that result has not already been fully evaluated, the A is still inside F[A] waiting to be evaluated. So, rather than writing a function that returns a raw type, an effectful function returns a raw type inside a useful wrapper — where that wrapper is a monad that lets the result be used in a monadic style (i.e., in a Scala for-expression)."

Alvin Alexander

Summary

  • Effect can also mean the “main-effect” of your program.
  • Instead of thinking about Side-effect, and effect is what the monad handles.
  • An “effectful” function returns a F[A] rather than A

Source

Effect or “Effectful” Type

Like this Article?

Sign up for my newsletter to get notified for new articles!


Related Posts

5 Anti Pattern for Writing Code in a Functional Programming Language

No 1. Nested Asynchronous Function

Why Do Functional Programmers Prefer For-Comprehension Over Imperative Code Block

Short Answer - Readability

How to Turn Domain Model into DynamoDB AttributeValue

A brief introduction about Dynosaur

Functional Programming has made My Job Easier as a Software Engineer. Here's Why.

Type level system able to let me sleep well at night

This is the Main Difference of Writing Applications in Functional Programming vs. Object-Oriented Programming

It is not immutability or inheritance, but more on the structure of the application if you use functional programming vs object-oriented