concept effect in category functional programming

appears as: effects, effect, effect, n effect, effects, An effect, The effect
Functional Programming in Java: How functional techniques improve your Java programs

This is an excerpt from Manning's book Functional Programming in Java: How functional techniques improve your Java programs.

This program is fully functional. It takes an argument, which is the pair of integers a and b, it returns a value, and it has absolutely no other observable effect. That it mutates variables doesn’t contradict the requirements, because arguments in Java are passed by value, so the mutations of the arguments aren’t visible from outside. You can then choose to apply an effect, such as displaying the result or using the result for another computation.

Listing 3.6. A Result that can handle Effects

13.1.1. What are effects?

I defined pure functions as functions without any observable side effects. An effect is anything that can be observed from outside the program. The role of a function is to return a value, and a side effect is anything, besides the returned value, that’s observable from the outside of the function. It’s called a side effect because it comes in addition to the value that’s returned. An effect (without “side”) is like a side effect, but it’s the main (and generally unique) role of a program. Functional programming is about writing programs with pure functions (with no side effects) and pure effects in a functional way.

The question is, what does it mean to handle effects in a functional way? The closest definition I can give at this stage is “handling effects in a way that doesn’t interfere with the principles of functional programming, the most important principle being referential transparency.” There are several ways to approach or reach this goal, and reaching this goal fully can be complex. Often, approaching it is sufficient. It’s up to you to decide which technique you want to use. Applying effects to contexts is the simplest (although not fully functional) way to make otherwise functional programs produce observable effects.

13.1.2. Implementing effects

As I just said, an effect is anything that’s observable from outside the program. Of course, to be valuable, this effect must generally reflect the result of the program, so you’ll generally need to take the result of the program and do something observable with it. Note that “observable” doesn’t always mean observable by a human operator. Often the result is observable by another program, which might then translate this effect into something observable by a human operator, either in synchronous or asynchronous form. Printing to the computer screen can be seen by the operator. Writing to a database, on the other hand, might not always be directly visible to a human user. Sometimes the result will be looked up by a human, but usually it will be read later by another program. In chapter 14, you’ll learn how such effects can be used by programs to communicate with other programs.

Functional Programming in Scala

This is an excerpt from Manning's book Functional Programming in Scala.

We saw in the case of buyCoffee how we could separate the creation of the Charge from the interpretation or processing of that Charge. In general, we’ll learn how this sort of transformation can be applied to any function with side effects to push these effects to the outer layers of the program. Functional programmers often speak of implementing programs with a pure core and a thin layer on the outside that handles effects.

But even so, surely at some point we must actually have an effect on the world and submit the Charge for processing by some external system. And aren’t there other useful programs that necessitate side effects or mutation? How do we write such programs? As we work through this book, we’ll discover how many programs that seem to necessitate side effects have some functional analogue. In other cases we’ll find ways to structure code so that effects occur but aren’t observable. (For example, we can mutate data that’s declared locally in the body of some function if we ensure that it can’t be referenced outside that function, or we can write to a file as long as no enclosing function can observe this occurring.)

We’ll make an important distinction in this chapter between effects and side effects. The IO monad provides a straightforward way of embedding imperative programming with I/O effects in a pure program while preserving referential transparency. It clearly separates effectful code—code that needs to have some effect on the outside world—from the rest of our program.

def doWhile[A](a: F[A])(cond: A => F[Boolean]): F[Unit] = for { #1 a1 <- a ok <- cond(a1) _ <- if (ok) doWhile(a)(cond) else unit(()) } yield () def forever[A,B](a: F[A]): F[B] = { #2 lazy val t: F[B] = forever(a) a flatMap (_ => t) } def foldM[A,B](l: Stream[A])(z: B)(f: (B,A) => F[B]): F[B] = #3 l match { case h #:: t => f(z,h) flatMap (z2 => foldM(t)(z2)(f)) case _ => unit(z) } def foldM_[A,B](l: Stream[A])(z: B)(f: (B,A) => F[B]): F[Unit] = #4 skip { foldM(l)(z)(f) } def foreachM[A](l: Stream[A])(f: A => F[Unit]): F[Unit] = #5 foldM_(l)(())((u,a) => skip(f(a))) #1: Repeats the effect of the first argument as long as the cond function yields true #2: Repeats the effect of its argument infinitely #3: Folds the stream with the function f, combining the effects and returning the result #4: The same as the foldM function above except ignores the result #5: Calls the function f for each element of the stream and combines the effects