Showing posts with label Monadm. Show all posts
Showing posts with label Monadm. Show all posts

Monday, 11 June 2012

Scala: Monoid

One of the simplest constructions in function programming is Monoid. Thanks to simple nature we are using it even without knowledge how it is named. Despite on this it's very interesting to review this in details. By the way this magic comes from Category Theory - which should be known by sophisticated developers;)

Comes from Semigroup:

In Function Programming Semigroup is set (presented by type T) and binary associative operation. Fast drop to both binary and associative:

Binary

Binary operation is any operation that requires two operands, for example
    2 * 2
    5 / 10
    list1 ::: list2

Associative

Formally, a binary operation on a set S is called associative if it satisfies the associative law:
(x * y) * z=x * (y * z)\qquad\mbox{for all }x,y,z\in S.
Still not easy to refresh basic algebra, here are examples for associative operations:
     (1 + 3) + 9 = 1 + (3 + 9)
     (2 * 5) * 10 = 2 * (5 * 10)
And non associative operations:
     (5 - 1) - 1 ≠ 5 - (1 - 1)
     (6 / 3) / 3 ≠ 6 / (3 / 3)

As we can feel associative is simple property, but if your are implementing new business types, you should determine on design phase is your operation in type is associative or not.