You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`Option` is also known as `Maybe`. `Some` is sometimes called `Just`. `None` is sometimes called `Nothing`.
910
911
911
-
## Totality
912
-
### Total functions
913
-
The total function is just like a function in math - it will always return a result of the expected type for expected inputs and will always terminate. The easiest example is `identity` function:
912
+
## Function
913
+
Category Threory: A function is just an arrow between types. It has a direction. Replace arrow with mapping maybe?
914
+
915
+
A **function**`f: A -> B` maps every element from its domain (A) to an element in its co-domain (B); or in other words: takes exactly one argument and maps it to exactly one (and always the same) value. That value depends entirely on the argument. This is what makes functions so pleasant to work with: there is no room for side-effects; a function is always pure – by definition. Functions exhibit [referential transparency](#referential-transparency).
916
+
917
+
A very simple example is the `identity` function:
914
918
```js
915
919
// identity :: a -> a
916
920
constidentity=a=> a
@@ -920,9 +924,24 @@ or `negate`:
920
924
// negate :: Boolean -> Boolean
921
925
constnegate=value=>!value
922
926
```
923
-
Such functions always meet the requirements, you just can't provide such arguments, which satisfy inputs but break outputs.
924
-
### Partial functions
925
-
It's a function which violates the requirements to be total - it may return an unexpected result with some inputs or it may never terminate. Partial functions add cognitive overhead, they are harder to reason about and they can lead to runtime errors. Some examples:
927
+
928
+
When programmers talk about functions though they often mean **expressions**: methods, procedures, and the likes. This is where some of the misconceptions regarding e.g. [Purity](#purity) and [Side effects](#side-effects) as special to some subset of functions come from, so that they need to be adressed here. What we really mean to say is: An expression which does not produce any side-effects (or: is pure) is a function. Programmers also have a concept of *functions expecting more than one parameter*, although those are *convenience methods* for their [curried forms](#currying). And rightfully so! Convenience is an important aspect of programming.
929
+
930
+
```js
931
+
// not a function, but an expression or more specifically a method
932
+
// add :: (number, number) -> number
933
+
constadd= (a, b) => a + b
934
+
935
+
// add :: number => number => number
936
+
// curried form: a function - taking one argument and returning a value, in this case another function
937
+
constaddCurried=a=>b=> a + b
938
+
add(1, 2) // might be considered more convenient than addCurried(1)(2)
939
+
```
940
+
941
+
It's totally fine to use the term "function" in a more loose way – depending on context –, but keep in mind its original meaning as *side-effect-free* or *pure* is often implicit with resources like the documentation of your favourite [fp libraries](#functional-programming-libraries-in-javascript).
942
+
943
+
## Partial function
944
+
A partial function is a function which may not be defined for all inputs - it might return an unexpected result with some inputs or it may never terminate. Partial functions add cognitive overhead, they are harder to reason about and they can lead to runtime errors. Some examples:
926
945
```js
927
946
// example 1: sum of the list
928
947
// sum :: [Number] -> Number
@@ -949,8 +968,10 @@ times(3)(console.log)
949
968
times(-1)(console.log)
950
969
// RangeError: Maximum call stack size exceeded
951
970
```
952
-
### Avoiding partial functions
953
-
Partial functions are dangerous, you can sometimes get the expected result, sometimes the wrong result, and sometimes your function can't stop the calculations at all. The input of partial functions should be always checked, and it can be hard to track all edge cases through entire applications, the easiest way to deal with it it's just to convert all partial functions to the total. General advice can be the usage of `Option` type, providing default values for edge cases and checking function conditions to make them always terminate:
971
+
972
+
### Dealing with partial functions
973
+
Partial functions are dangerous as they need to be treated with great caution. You might get an unexpected (wrong) result or run into runtime errors. Sometimes a partial function might not return at all. Being aware of and treating all these edge cases accordingly can become very tedious.
974
+
Fortunately a partial function can be converted to a regular (or total) one. We can provide default values or use guards to deal with inputs for which the (previously) partial function is undefined. Utilizing the [`Option`](#Option) type, we can yield either `Some(value)` or `None` where we would otherwise have behaved unexpectedly:
954
975
```js
955
976
// example 1: sum of the list
956
977
// we can provide default value so it will always return result
@@ -963,7 +984,7 @@ sqrt([]) // 0
963
984
// change result to Option
964
985
// first :: [A] -> Option A
965
986
constfirst=a=>a.length?Some(a[0]) :None()
966
-
first([[42]]).map(a=>console.log(a)) // 42
987
+
first([42]).map(a=>console.log(a)) // 42
967
988
first([]).map(a=>console.log(a)) // console.log won't execute at all
968
989
//our previous worst case
969
990
first([[42]]).map(a=>console.log(a[0])) // 42
@@ -983,7 +1004,7 @@ times(3)(console.log)
983
1004
times(-1)(console.log)
984
1005
// won't execute anything
985
1006
```
986
-
If you will change all your functions from partial to total, it can prevent you from having runtime exceptions, will make code easier to reason about and easier to maintain.
1007
+
Making your partial functions total ones, these kinds of runtime errors can be prevented. Always returning a value will also make for code that is both easier to maintain as well as to reason about.
0 commit comments