What is different in pure and impure function in scala
#pure and impure function #scala #diference #pure function #impure function
A pure function is a function that depends only on its declared inputs and its internal algorithm to produce its output .
def double(i: Int): Int = i * 2
"A pure function is a function that depends only on its declared inputs and its internal algorithm to produce its output. It does not read any other values from “the outside world” — the world outside of the function’s scope — and it does not modify any values in the outside world."
A impure function is a function that depends on referrential variable or data and also its declared inputs .it is produce its output .
"Write the core of your application using pure functions, and then write an impure “wrapper” around that core to interact with the outside world. If you like food analogies, this is like putting a layer of impure icing on top of a pure cake".
def sum(list: List[Int]): Int = list match {
case Nil => 0 case head :: tail => head + sum(tail)
}
Impure functions can add complexity to the code, making it more challenging to test, maintain, and reason about. Due to their possible side effects, they might also cause problems with concurrency and parallelism.
The fact that pure and impure functions are combined in real-world applications is a second important issue.
It is frequently advised to create the application's core in pure functions before employing impure functions to interact with the outside world.
Therefore ,
impure functions may be necessary in certain scenarios, particularly when dealing with I/O operations or stateful applications,