For those interested in functional programming, I’ll talk about monoids and why they’re very important to understand ahead of time.
Don’t get confused: This isn’t monad — it’s monoid. I’m pretty sure you already know of monoids and you use them almost every day — you just didn’t know the term for them.
This is a series on functional programming, so you might not understand what this article is going to talk about if you haven’t read the previous posts.
Let’s assume there’s a function named identity
that takes A
and returns A
.
const identity: <A>(a: A): A => a;
interface Student {
name: string;
age: number;
}
identity<number>(3) // 3
identity<string>('hello') // hello
identity<Student>({
name: 'Bincent',
age: 5
}); // { name: 'Bincent', age: 5 }
In functional programming, this useless function (seems useless) is an important factor for many other concepts (such as monoids) that we’re about to talk about.
Basically, a monoid is a set of elements that holds the rules of the semigroup and the identity-element rule.
If S
is a set of elements, a
is a member of S
, and ·
is a proper binary operation, a·e = e·a ∈ S
must be satisfied to be a monoid.
Identity: a ∈ S, a·e = e·a = a ∈ S
Some documentation calls this using the number 1 and the any alphabet in subscript — for example, 1x referring to the identity on the variable x. Or some documentation uses just a single alphabet letter, such as i or e.
That’s all there is to know about monoids, let’s practice with some simple examples.
#typescript #programming #functional-programming #javascript #coding #function