Created
October 24, 2017 07:33
-
-
Save abiodun0/f95fcecca8acf2d68f83ac9ebcf4fffd to your computer and use it in GitHub Desktop.
This is a simple monoid structure for basic operations of average, addition, product and max
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Definining a simple monoid structure for addtions, multiply and Max utility | |
var number = [1, 2, 3]; | |
const Sum = (x) => ({ | |
append: (y) => Sum(x + y.val), | |
val: x | |
}) | |
Sum.Identity = Sum(0); | |
const Multiply = (x) => ({ | |
append: (y) => Multiply(x * y.val), | |
val: x | |
}); | |
Multiply.Identity = Multiply(1) | |
const Max = (x) => ({ | |
append: y => Max(x > y.val ? x : y.val), | |
val: x | |
}); | |
Max.Identity = Max(-Infinity); | |
// This was a bit tricky. first you would have to get the sum and the lenght | |
// each addiont you are making in are just one more length to the length | |
// except for the initial which is 0, 0 | |
const Average = (sum, length= 1) => ({ | |
append: y => Average(sum + y.sum, length + y.length), | |
length: length, | |
sum: sum, | |
val: length && sum / length | |
}) | |
Average.Identity = Average(0, 0) | |
// We only have one function!!!.. how cool is this!!! | |
const fold = (M, xs) => xs.map(M).reduce((acc, val) => acc.append(val), M.Identity); | |
// fold(Sum, number).val; | |
fold(Average, number).val; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yep basically. the map is creating a Monoid instance which applies to append to every reduce function.
The good part of it is. we can easily have for Max Agerage multiply by changing the 38 as follows
A use case, combing react elements
Now we can append like a boss
Not that this also contains contramap and functors, but the main focus is a monoid in this case
@kiggundu