What is an effect?
In software, a program is categorized as “effectful” if it needs to interact with the external environment for it to complete its purpose. For example
console.log in JS, actually paints pixels on the screen and is thus an effectful function. Similarly using
fetch to make an http call in the browser is a side-effect because for it to run it needs access to network. Simpler code pieces such as
new Date() which returns the current date/time is also effectful because it relies on the system’s clock.
Concurrency and Effects.
Effects in Functional Programming?
In functional programming you work with pure functions. A pure function is —
- Total: They return an output for every input. They shouldn’t be throwing exceptions or just exit without completing.
- Deterministic: For the same input they return the same output.
- Pure: Their only effect is computing the output. They don’t depend on external environments.
// impure function const putStrLn = msg => console.log(msg)