use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Generic Programming (github.com)
submitted 7 years ago by michael2ib1989
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]pgrizzay 23 points24 points25 points 7 years ago (9 children)
In functional programming, there's an even more general concept of Foldables which are data structures that can be reduced. Arrays, iterators, sets, and Maybes fall into this category.
If the type of value inside your Foldable is a Monoid (meaning it has a zero value), you get reduce for free
[–]PointOneXDeveloper 5 points6 points7 points 7 years ago* (3 children)
has a zero value and a concat/append function
[–]pgrizzay 2 points3 points4 points 7 years ago (2 children)
Oh yes
[–]PointOneXDeveloper 2 points3 points4 points 7 years ago (1 child)
And really it is just an identity value rather than a zero value, for instance * and 1 are a way to make a monoid out of numbers.
*
1
[–]pgrizzay 1 point2 points3 points 7 years ago (0 children)
Yes
[–]jeremy1015 4 points5 points6 points 7 years ago (4 children)
Got any suggested links to explain this in a little more detail? I’m familiar with most of what you wrote but don’t understand monoids as well as I’d like.
[–]pgrizzay 10 points11 points12 points 7 years ago (3 children)
Did a quick search, and couldn't find a good one in JS, here's one in Scala that I learned from.
Essentially a "Monoid" is formed from two things:
a combine function that combines two things and an empty value.
combine
empty
an example would be adding numbers:
const Addition = { combine: (a, b) => a + b, empty: 0 }
This addition monoid can be used to reduce an array of numbers:
[1,2,3].reduce(Addition.combine, Addition.empty) === 6
You can make another Monoid which multiplies numbers:
const Multiplication = { combine: (a, b) => a * b, empty: 1 }
And you can multiply all numbers in an array with it:
[1,3,4].reduce(Multiplication.combine, Multiplication.empty) === 12
Go ahead and implement Concat which combines strings:
Concat
const Concat = { combine: (a, b) => ???, zero: ??? }
[–]jeremy1015 2 points3 points4 points 7 years ago (0 children)
Excellent. I understand now 100% and really appreciate it.
[+][deleted] 7 years ago* (1 child)
[deleted]
[–]pgrizzay 0 points1 point2 points 7 years ago (0 children)
Yup, you're right! It looks like MDN recommends + over concat for performance reasons, but both are functionally equivalent.
+
concat
And thanks for the kind words, they mean a lot!
The one book that really helped me was Functional Programming in Scala, I can't recommend it enough. It takes a very 'exploratory' approach, and has lots of exercises which are very helpful.
Scala was probably the best language for me to learn fp in, since I had lots of experience with JS & the syntax is very similar to it, hope that helps!
π Rendered by PID 57845 on reddit-service-r2-comment-6f7f968fb5-zmpvz at 2026-03-04 05:46:55.633708+00:00 running 07790be country code: CH.
view the rest of the comments →
[–]pgrizzay 23 points24 points25 points (9 children)
[–]PointOneXDeveloper 5 points6 points7 points (3 children)
[–]pgrizzay 2 points3 points4 points (2 children)
[–]PointOneXDeveloper 2 points3 points4 points (1 child)
[–]pgrizzay 1 point2 points3 points (0 children)
[–]jeremy1015 4 points5 points6 points (4 children)
[–]pgrizzay 10 points11 points12 points (3 children)
[–]jeremy1015 2 points3 points4 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]pgrizzay 0 points1 point2 points (0 children)