Skip to content

Commit 3218069

Browse files
committed
refactor: Introduce function section
- dissolve totality section - make partial function a top-level section - reformulate content of partial function section
1 parent a7e26ce commit 3218069

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

readme.md

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ __Table of Contents__
6060
* [Sum type](#sum-type)
6161
* [Product type](#product-type)
6262
* [Option](#option)
63-
* [Totality](#totality)
63+
* [Function](#function)
64+
* [Partial function](#partial-function)
6465
* [Functional Programming Libraries in JavaScript](#functional-programming-libraries-in-javascript)
6566

6667

@@ -908,9 +909,12 @@ getNestedPrice({item: {price: 9.99}}) // Some(9.99)
908909

909910
`Option` is also known as `Maybe`. `Some` is sometimes called `Just`. `None` is sometimes called `Nothing`.
910911

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:
914918
```js
915919
// identity :: a -> a
916920
const identity = a => a
@@ -920,9 +924,24 @@ or `negate`:
920924
// negate :: Boolean -> Boolean
921925
const negate = value => !value
922926
```
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+
const add = (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+
const addCurried = 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:
926945
```js
927946
// example 1: sum of the list
928947
// sum :: [Number] -> Number
@@ -949,8 +968,10 @@ times(3)(console.log)
949968
times(-1)(console.log)
950969
// RangeError: Maximum call stack size exceeded
951970
```
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:
954975
```js
955976
// example 1: sum of the list
956977
// we can provide default value so it will always return result
@@ -963,7 +984,7 @@ sqrt([]) // 0
963984
// change result to Option
964985
// first :: [A] -> Option A
965986
const first = 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
967988
first([]).map(a => console.log(a)) // console.log won't execute at all
968989
//our previous worst case
969990
first([[42]]).map(a => console.log(a[0])) // 42
@@ -983,7 +1004,7 @@ times(3)(console.log)
9831004
times(-1)(console.log)
9841005
// won't execute anything
9851006
```
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.
9871008

9881009
## Functional Programming Libraries in JavaScript
9891010

0 commit comments

Comments
 (0)