Functional programming
Page content
- Pure functions, no side effects like
- reassigning a variable
- modifying a data structure in place
- Setting a field on an object
- throwing an exception or halting with an error
- printing to the console or reading user input
- reading from or writing to a file
- drawing on the screen eg: addition + on 2 integers is a pure function
Referential Transparency:
- An expression
e
is referential transparent if for all programsp
, all occurneces ofe
inp
can be replaced by the result of evaluatinge
, without affecting the observable behaviour ofp
.
Annotations:
- In java and scala Annotation are metadata that the compiler and runtime can use to infer how a method or class should behave.In a @tailrec annotation method, the Scala compiler will throw an error if our method was programmed to not be tail recursive, and thus make our code safer.
- throwing an exception breaks referential transparency. *