KEMBAR78
Pure functions and usage in Angular | PPTX
Pure function: concepts &
cases
Mike MA 594mjf@gmail.com
What is pure function
Concept
A pure function is a function where the return value is only determined by its input values, without
observable side effects.
● When given the same argument, the result will always be the same
● Pure functions do not produce side effects.
● Foundation of functional programming
Reproducible Result
function half(x) {
return x / 2;
}i
Pure functions only depend on what's passed to them
When invoked, a pure function can be replaced by its result.
This is called referential transparency.
No Side Effect
function half(x) {
return x / 2;
}i
When a function or expression modifies state outside its own context, the result is a side effect.
Making a call to an API, manipulating the DOM, raising an alert dialog, writing to a database, etc.
Benefits: why use pure function?
Portable/Reusable
Only depends on input, easy to copy to somewhere else
The problem of OOP:
You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.
Easy to refactor and test
Pure functions are also extremely independent — easy to move around, refactor, and reorganize in
your code, making your programs more flexible and adaptable to future changes.
Given predictable result
Easy to combine/parallelize them
An Example of Functional Programming (FP):
Memoization (cacheable)
You may cache the output for a given input.
In real practice:
pure pipe in
Angular
Pipe API
Angular executes a pure pipe only when it detects a pure change to the input value. A
pure change is either a change to a primitive input value (String, Number, Boolean,
Symbol) or a changed object reference (Date, Array, Function, Object).
A pure pipe uses pure functions. Pure functions process inputs and return values
without detectable side effects. Given the same input, they should always return the
same output.
Why pure pipe is recommended?
Let’s see some real code …
https://codepen.io/simars/pe
n/wxRpjN?editors=0010#0
Thanks!

Pure functions and usage in Angular

  • 1.
    Pure function: concepts& cases Mike MA 594mjf@gmail.com
  • 2.
    What is purefunction
  • 3.
    Concept A pure functionis a function where the return value is only determined by its input values, without observable side effects. ● When given the same argument, the result will always be the same ● Pure functions do not produce side effects. ● Foundation of functional programming
  • 4.
    Reproducible Result function half(x){ return x / 2; }i Pure functions only depend on what's passed to them When invoked, a pure function can be replaced by its result. This is called referential transparency.
  • 5.
    No Side Effect functionhalf(x) { return x / 2; }i When a function or expression modifies state outside its own context, the result is a side effect. Making a call to an API, manipulating the DOM, raising an alert dialog, writing to a database, etc.
  • 6.
    Benefits: why usepure function?
  • 7.
    Portable/Reusable Only depends oninput, easy to copy to somewhere else The problem of OOP: You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.
  • 8.
    Easy to refactorand test Pure functions are also extremely independent — easy to move around, refactor, and reorganize in your code, making your programs more flexible and adaptable to future changes. Given predictable result
  • 9.
    Easy to combine/parallelizethem An Example of Functional Programming (FP):
  • 10.
    Memoization (cacheable) You maycache the output for a given input.
  • 11.
    In real practice: purepipe in Angular
  • 12.
    Pipe API Angular executesa pure pipe only when it detects a pure change to the input value. A pure change is either a change to a primitive input value (String, Number, Boolean, Symbol) or a changed object reference (Date, Array, Function, Object). A pure pipe uses pure functions. Pure functions process inputs and return values without detectable side effects. Given the same input, they should always return the same output.
  • 15.
    Why pure pipeis recommended? Let’s see some real code … https://codepen.io/simars/pe n/wxRpjN?editors=0010#0
  • 16.