Category Theory for Programmers Chapter 2: Types and Functions
 Implement
memoize
const memoize = (f) => { const cache = new Map(); return function() { const args = JSON.stringify(arguments); if (cache.has(args)) { return cache.get(args); } const result = f.apply(null, arguments); cache.set(args, result); return result; } }
 Memoizing
Math.random
just gives the same number forever.const random = memoize(Math.random)

Math.random
doesn’t take a seed, but the above function can give different values with different inputs. Not really sure what the intention here is, but this works. 
Only the first function is pure. It gives the same results with no sideeffects for the inputs. If memoized, nothing changes. (2) reads input, so can give different results. (3) has sideeffects by writing to stdout which can be differnt between calls. (4) has a value that persists between calls so successive calls have different values.
 There are 4 functions from
Bool
toBool
.const id = x => x const not = x => !x const alwaysTrue = x => true const alwaysFalse = x => false

The category with types
Void
,()
, andBool
has the following morphisms: Id_{Void}:
Void
→Void
 absurd:
Void
→a
 Id_{()}:
()
→()
 discard:
a
→()
 True_{()}:
()
→Bool
 False_{()}:
()
→Bool
 Id_{Bool}:
Bool
→Bool
 Not_{Bool}:
Bool
→Bool
 True_{Bool}:
Bool
→Bool
 False_{Bool}:
Bool
→Bool
The weird thing here is why there are no morphisms to
Void
. For this, we fall back to what a morphism is, and here it is a function from a set to a set. This also means, that a function takes every value of the domain to a unique value in the codomain. In the instance ofVoid
, we can define a function withVoid
as the domain because even thoughVoid
is the empty set, we can still define a function for all x in ∅ (even if there are none). However, for a function that does have values, likeBool
, for there to be a function toVoid
, we are saying for all x in{true, false}
there exists a unique value inVoid
such that the relationship holds. However, there is no value inVoid
so we cannot have a function.This differs with
()
which is a singleton set, eg{👾}
. For this we can define functions from()
that send👾
to some other value, and similar, send all values from another domain to just one value.  Id_{Void}: