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 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 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.

#scala #programming #functional-programming #effect #side-effects

What is “effect” or “effectful” mean in Functional Programming?
1.55 GEEK