What is the binary operator of Function Monoids in example of JavaScriptWhat is the most efficient way to deep clone an object in JavaScript?Which equals operator (== vs ===) should be used in JavaScript comparisons?What is the scope of variables in JavaScript?JavaScript closure inside loops – simple practical exampleWhat is the !! (not not) operator in JavaScript?Set a default parameter value for a JavaScript functionWhat is the JavaScript version of sleep()?What is (functional) reactive programming?What does “use strict” do in JavaScript, and what is the reasoning behind it?What is the 'new' keyword in JavaScript?

How dangerous is XSS?

Bullying boss launched a smear campaign and made me unemployable

Solving a recurrence relation (poker chips)

Why didn't Boeing produce its own regional jet?

What's the in-universe reasoning behind sorcerers needing material components?

Venezuelan girlfriend wants to travel the USA to be with me. What is the process?

Avoiding direct proof while writing proof by induction

Can I run a new neutral wire to repair a broken circuit?

Expand and Contract

What killed these X2 caps?

How did the Super Star Destroyer Executor get destroyed exactly?

How writing a dominant 7 sus4 chord in RNA ( Vsus7 chord in the 1st inversion)

How to Recreate this in LaTeX? (Unsure What the Notation is Called)

Intersection Puzzle

Why is consensus so controversial in Britain?

One verb to replace 'be a member of' a club

Why no variance term in Bayesian logistic regression?

What about the virus in 12 Monkeys?

Is it acceptable for a professor to tell male students to not think that they are smarter than female students?

What does the expression "A Mann!" means

Why would the Red Woman birth a shadow if she worshipped the Lord of the Light?

What method can I use to design a dungeon difficult enough that the PCs can't make it through without killing them?

Forgetting the musical notes while performing in concert

Avoiding the "not like other girls" trope?



What is the binary operator of Function Monoids in example of JavaScript


What is the most efficient way to deep clone an object in JavaScript?Which equals operator (== vs ===) should be used in JavaScript comparisons?What is the scope of variables in JavaScript?JavaScript closure inside loops – simple practical exampleWhat is the !! (not not) operator in JavaScript?Set a default parameter value for a JavaScript functionWhat is the JavaScript version of sleep()?What is (functional) reactive programming?What does “use strict” do in JavaScript, and what is the reasoning behind it?What is the 'new' keyword in JavaScript?













1















In this article Function Monoids are introduced with C# code and Haskell Type definition.




A function a -> b is a monoid if b is a monoid. This means that
you can combine two functions with the same type. In an
object-oriented context, it means that you can combine two methods
with the same signature into one method as long as the return type
forms a monoid.



Generalisation



While the above C# code is only an example, the general rule is that
any function that returns a monoid is itself a monoid. In Haskell,
this rule is articulated in the standard library:



instance Monoid b => Monoid (a -> b)


This means that for any monoid b, a function a -> b is also
(automatically) a monoid.




The problem is the example in C# is too specific about "GUID" and I have no idea what the author trying to do in the code, and the Haskell Type Definition is merely the type definition.



What is an example code in JavaScript to implement this Function Monoids?



cf)
Why is instance Monoid b => Monoid (a -> b) in base?










share|improve this question



















  • 3





    Given that javascript doesn't have a sophisticated enough type system, it's rather hard to translate. "Being a monoid" is not something that can be expressed as a runtime value. I would recommend to study Haskell to understand what the article means if you are interested in this.

    – Bergi
    Mar 21 at 21:27






  • 1





    I don't agree. To implement Monoids in JavaScript, type system is not required at all. The same goes to Mondas.

    – user11239932
    Mar 21 at 21:30











  • The C# example isn't specific on GUIDs at all. The meat of the article is the Combine function, which you should be able to translate into javascript even without any C# knowledge. CountPrimes and CountLetters are just used as examples of functions to be used as arguments to Combine.

    – Bergi
    Mar 21 at 21:33






  • 2





    @tbookq I'm writing comments because I haven't yet answered your question "What is an example code in JavaScript to implement this Function Monoids?". I'm writing comments to suggest ways to improve your question so that I can properly answer it with a helpful and understandable post.

    – Bergi
    Mar 21 at 21:39







  • 1





    @tbookq Please edit your question to include an arbitrary implementation of a monoid as an example (and to demonstrate your level of knowledge), and I will write an appropriate answer. I don't want to write an answer that is useless to you, but you will need to provide more information. It's you who wants other to help him.

    – Bergi
    Mar 21 at 22:11















1















In this article Function Monoids are introduced with C# code and Haskell Type definition.




A function a -> b is a monoid if b is a monoid. This means that
you can combine two functions with the same type. In an
object-oriented context, it means that you can combine two methods
with the same signature into one method as long as the return type
forms a monoid.



Generalisation



While the above C# code is only an example, the general rule is that
any function that returns a monoid is itself a monoid. In Haskell,
this rule is articulated in the standard library:



instance Monoid b => Monoid (a -> b)


This means that for any monoid b, a function a -> b is also
(automatically) a monoid.




The problem is the example in C# is too specific about "GUID" and I have no idea what the author trying to do in the code, and the Haskell Type Definition is merely the type definition.



What is an example code in JavaScript to implement this Function Monoids?



cf)
Why is instance Monoid b => Monoid (a -> b) in base?










share|improve this question



















  • 3





    Given that javascript doesn't have a sophisticated enough type system, it's rather hard to translate. "Being a monoid" is not something that can be expressed as a runtime value. I would recommend to study Haskell to understand what the article means if you are interested in this.

    – Bergi
    Mar 21 at 21:27






  • 1





    I don't agree. To implement Monoids in JavaScript, type system is not required at all. The same goes to Mondas.

    – user11239932
    Mar 21 at 21:30











  • The C# example isn't specific on GUIDs at all. The meat of the article is the Combine function, which you should be able to translate into javascript even without any C# knowledge. CountPrimes and CountLetters are just used as examples of functions to be used as arguments to Combine.

    – Bergi
    Mar 21 at 21:33






  • 2





    @tbookq I'm writing comments because I haven't yet answered your question "What is an example code in JavaScript to implement this Function Monoids?". I'm writing comments to suggest ways to improve your question so that I can properly answer it with a helpful and understandable post.

    – Bergi
    Mar 21 at 21:39







  • 1





    @tbookq Please edit your question to include an arbitrary implementation of a monoid as an example (and to demonstrate your level of knowledge), and I will write an appropriate answer. I don't want to write an answer that is useless to you, but you will need to provide more information. It's you who wants other to help him.

    – Bergi
    Mar 21 at 22:11













1












1








1


0






In this article Function Monoids are introduced with C# code and Haskell Type definition.




A function a -> b is a monoid if b is a monoid. This means that
you can combine two functions with the same type. In an
object-oriented context, it means that you can combine two methods
with the same signature into one method as long as the return type
forms a monoid.



Generalisation



While the above C# code is only an example, the general rule is that
any function that returns a monoid is itself a monoid. In Haskell,
this rule is articulated in the standard library:



instance Monoid b => Monoid (a -> b)


This means that for any monoid b, a function a -> b is also
(automatically) a monoid.




The problem is the example in C# is too specific about "GUID" and I have no idea what the author trying to do in the code, and the Haskell Type Definition is merely the type definition.



What is an example code in JavaScript to implement this Function Monoids?



cf)
Why is instance Monoid b => Monoid (a -> b) in base?










share|improve this question
















In this article Function Monoids are introduced with C# code and Haskell Type definition.




A function a -> b is a monoid if b is a monoid. This means that
you can combine two functions with the same type. In an
object-oriented context, it means that you can combine two methods
with the same signature into one method as long as the return type
forms a monoid.



Generalisation



While the above C# code is only an example, the general rule is that
any function that returns a monoid is itself a monoid. In Haskell,
this rule is articulated in the standard library:



instance Monoid b => Monoid (a -> b)


This means that for any monoid b, a function a -> b is also
(automatically) a monoid.




The problem is the example in C# is too specific about "GUID" and I have no idea what the author trying to do in the code, and the Haskell Type Definition is merely the type definition.



What is an example code in JavaScript to implement this Function Monoids?



cf)
Why is instance Monoid b => Monoid (a -> b) in base?







javascript functional-programming monoids






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 21 at 21:30









Bergi

380k64582916




380k64582916










asked Mar 21 at 21:15







user11239932














  • 3





    Given that javascript doesn't have a sophisticated enough type system, it's rather hard to translate. "Being a monoid" is not something that can be expressed as a runtime value. I would recommend to study Haskell to understand what the article means if you are interested in this.

    – Bergi
    Mar 21 at 21:27






  • 1





    I don't agree. To implement Monoids in JavaScript, type system is not required at all. The same goes to Mondas.

    – user11239932
    Mar 21 at 21:30











  • The C# example isn't specific on GUIDs at all. The meat of the article is the Combine function, which you should be able to translate into javascript even without any C# knowledge. CountPrimes and CountLetters are just used as examples of functions to be used as arguments to Combine.

    – Bergi
    Mar 21 at 21:33






  • 2





    @tbookq I'm writing comments because I haven't yet answered your question "What is an example code in JavaScript to implement this Function Monoids?". I'm writing comments to suggest ways to improve your question so that I can properly answer it with a helpful and understandable post.

    – Bergi
    Mar 21 at 21:39







  • 1





    @tbookq Please edit your question to include an arbitrary implementation of a monoid as an example (and to demonstrate your level of knowledge), and I will write an appropriate answer. I don't want to write an answer that is useless to you, but you will need to provide more information. It's you who wants other to help him.

    – Bergi
    Mar 21 at 22:11












  • 3





    Given that javascript doesn't have a sophisticated enough type system, it's rather hard to translate. "Being a monoid" is not something that can be expressed as a runtime value. I would recommend to study Haskell to understand what the article means if you are interested in this.

    – Bergi
    Mar 21 at 21:27






  • 1





    I don't agree. To implement Monoids in JavaScript, type system is not required at all. The same goes to Mondas.

    – user11239932
    Mar 21 at 21:30











  • The C# example isn't specific on GUIDs at all. The meat of the article is the Combine function, which you should be able to translate into javascript even without any C# knowledge. CountPrimes and CountLetters are just used as examples of functions to be used as arguments to Combine.

    – Bergi
    Mar 21 at 21:33






  • 2





    @tbookq I'm writing comments because I haven't yet answered your question "What is an example code in JavaScript to implement this Function Monoids?". I'm writing comments to suggest ways to improve your question so that I can properly answer it with a helpful and understandable post.

    – Bergi
    Mar 21 at 21:39







  • 1





    @tbookq Please edit your question to include an arbitrary implementation of a monoid as an example (and to demonstrate your level of knowledge), and I will write an appropriate answer. I don't want to write an answer that is useless to you, but you will need to provide more information. It's you who wants other to help him.

    – Bergi
    Mar 21 at 22:11







3




3





Given that javascript doesn't have a sophisticated enough type system, it's rather hard to translate. "Being a monoid" is not something that can be expressed as a runtime value. I would recommend to study Haskell to understand what the article means if you are interested in this.

– Bergi
Mar 21 at 21:27





Given that javascript doesn't have a sophisticated enough type system, it's rather hard to translate. "Being a monoid" is not something that can be expressed as a runtime value. I would recommend to study Haskell to understand what the article means if you are interested in this.

– Bergi
Mar 21 at 21:27




1




1





I don't agree. To implement Monoids in JavaScript, type system is not required at all. The same goes to Mondas.

– user11239932
Mar 21 at 21:30





I don't agree. To implement Monoids in JavaScript, type system is not required at all. The same goes to Mondas.

– user11239932
Mar 21 at 21:30













The C# example isn't specific on GUIDs at all. The meat of the article is the Combine function, which you should be able to translate into javascript even without any C# knowledge. CountPrimes and CountLetters are just used as examples of functions to be used as arguments to Combine.

– Bergi
Mar 21 at 21:33





The C# example isn't specific on GUIDs at all. The meat of the article is the Combine function, which you should be able to translate into javascript even without any C# knowledge. CountPrimes and CountLetters are just used as examples of functions to be used as arguments to Combine.

– Bergi
Mar 21 at 21:33




2




2





@tbookq I'm writing comments because I haven't yet answered your question "What is an example code in JavaScript to implement this Function Monoids?". I'm writing comments to suggest ways to improve your question so that I can properly answer it with a helpful and understandable post.

– Bergi
Mar 21 at 21:39






@tbookq I'm writing comments because I haven't yet answered your question "What is an example code in JavaScript to implement this Function Monoids?". I'm writing comments to suggest ways to improve your question so that I can properly answer it with a helpful and understandable post.

– Bergi
Mar 21 at 21:39





1




1





@tbookq Please edit your question to include an arbitrary implementation of a monoid as an example (and to demonstrate your level of knowledge), and I will write an appropriate answer. I don't want to write an answer that is useless to you, but you will need to provide more information. It's you who wants other to help him.

– Bergi
Mar 21 at 22:11





@tbookq Please edit your question to include an arbitrary implementation of a monoid as an example (and to demonstrate your level of knowledge), and I will write an appropriate answer. I don't want to write an answer that is useless to you, but you will need to provide more information. It's you who wants other to help him.

– Bergi
Mar 21 at 22:11












2 Answers
2






active

oldest

votes


















1














Monoid Laws




identity law:
combine (identity, a) == combine (a, identity)



associativity law:
combine (a, combine (b, c)) == combine (combine (a, b), c)




We can implement an identity element (identity) and binary operation (combine) for functions -






// identity element
const identity =
x => x

// binary operation
const combine = (a, b) =>
x => a (b (x))

// sample functions
const a =
x => x + 3

const b =
x => x - 1

const c =
x => x * x

// uphold laws
console.log
( combine (identity, a) (2) === combine (a, identity) (2)
, combine (a, combine (b, c)) (2) === combine (combine (a, b), c) (2)
)

// => true
// => true





The binary operation and identity element is different depending on your domain. See the table on Wikipedia for more insight on how to implement the identity element and binary operation for various sets.



Of course you are not limited to those domains. Your custom type may have various binary operations and identity elements that satisfy the monoid laws. If the laws are upheld, your type belongs to the monoid category.






share|improve this answer























  • Thanks, but x => a (b (x)) is composition of function, that is "endomorphism-monoid" which is not what this topic is about. blog.ploeh.dk/2017/11/13/endomorphism-monoid In Haskell, it's callled Endo Monoid.

    – user11239932
    Mar 21 at 23:50











  • The question is "What is the binary operator of Function Monoids in example of JavaScript?". This answer explains that any identity element and binary operation that satisfies the monoid laws forms a monoid. The answer also shows an example of a function monoid in JavaScript. The linked Wikipedia table shows other pairings that form monoids as well. I guess your question title is wrong or maybe you don't understand what you're asking.

    – user633183
    Mar 22 at 0:07











  • Well, I wrote a book about FunctionalProgramming published by one of the major publishing company in Japan. So, I know what I'm talking about. The question refers a specific Monoid with example and the title corresponds to it. I know Monoid well, and not asking here Monoid Law etc. that I also know well. Your answer is irreverent since it does not correspond to the question. Sorry.

    – user11239932
    Mar 22 at 0:18






  • 1





    You've been very quick to dismiss others and assert yourself. I can see you're very proud of your credentials, possibly to a fault. Did you bother to look at the credentials of the people offering their help? Bergi is one of the most knowledgeable contributors on this topic here on SO; also patient and respectful. You seem to know exactly what you want, so I'll leave you to find it.

    – user633183
    Mar 22 at 0:27











  • The point is my question is resolved. This is Q&A site. To answer own question is not prohibited so did I. Perhaps it's a good idea you also you investigate your behavior saying "your question title is wrong", the fact is nothing wrong presenting the concrete example and definition. It's simple and clear "endomorphism-monoid" in your answer or identiy funciton tihng is out of topic here.

    – user11239932
    Mar 22 at 0:33


















1














It's important to know that being able to combine things doesn't necessarily mean they are a monoid. For something to be a Monoid, it must have associativity, left identity, and right identity.



C#:



public static Func<Guid, int> Combine(
Func<Guid, int> f,
Func<Guid, int> g)

return x => f(x) + g(x);



JS:




function combine(f,g) 
return x => f(x) + g(x)


function addOne(x)
return x + 1;


function addTwo(x)
return x + 2;


let newFunction = combine(addOne, addTwo);
let result = newFunction(0);
console.log(result)








share|improve this answer

























  • Thanks, I probably should ask not just combine operation since this does not make sense at least, and yes it would generate a error. Small working code please in S.O.Snippet.

    – user11239932
    Mar 21 at 21:55











  • combine is a function that accepts two arguments, these arguments are assumed to be functions in this case. combine will actually return a new function that takes one argument. Inside this new function, each argument, f and g will be applied to x and their results will be added together. I hope this make it easier to understand.

    – Nick Acosta
    Mar 21 at 23:21












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55289369%2fwhat-is-the-binary-operator-of-function-monoids-in-example-of-javascript%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown
























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Monoid Laws




identity law:
combine (identity, a) == combine (a, identity)



associativity law:
combine (a, combine (b, c)) == combine (combine (a, b), c)




We can implement an identity element (identity) and binary operation (combine) for functions -






// identity element
const identity =
x => x

// binary operation
const combine = (a, b) =>
x => a (b (x))

// sample functions
const a =
x => x + 3

const b =
x => x - 1

const c =
x => x * x

// uphold laws
console.log
( combine (identity, a) (2) === combine (a, identity) (2)
, combine (a, combine (b, c)) (2) === combine (combine (a, b), c) (2)
)

// => true
// => true





The binary operation and identity element is different depending on your domain. See the table on Wikipedia for more insight on how to implement the identity element and binary operation for various sets.



Of course you are not limited to those domains. Your custom type may have various binary operations and identity elements that satisfy the monoid laws. If the laws are upheld, your type belongs to the monoid category.






share|improve this answer























  • Thanks, but x => a (b (x)) is composition of function, that is "endomorphism-monoid" which is not what this topic is about. blog.ploeh.dk/2017/11/13/endomorphism-monoid In Haskell, it's callled Endo Monoid.

    – user11239932
    Mar 21 at 23:50











  • The question is "What is the binary operator of Function Monoids in example of JavaScript?". This answer explains that any identity element and binary operation that satisfies the monoid laws forms a monoid. The answer also shows an example of a function monoid in JavaScript. The linked Wikipedia table shows other pairings that form monoids as well. I guess your question title is wrong or maybe you don't understand what you're asking.

    – user633183
    Mar 22 at 0:07











  • Well, I wrote a book about FunctionalProgramming published by one of the major publishing company in Japan. So, I know what I'm talking about. The question refers a specific Monoid with example and the title corresponds to it. I know Monoid well, and not asking here Monoid Law etc. that I also know well. Your answer is irreverent since it does not correspond to the question. Sorry.

    – user11239932
    Mar 22 at 0:18






  • 1





    You've been very quick to dismiss others and assert yourself. I can see you're very proud of your credentials, possibly to a fault. Did you bother to look at the credentials of the people offering their help? Bergi is one of the most knowledgeable contributors on this topic here on SO; also patient and respectful. You seem to know exactly what you want, so I'll leave you to find it.

    – user633183
    Mar 22 at 0:27











  • The point is my question is resolved. This is Q&A site. To answer own question is not prohibited so did I. Perhaps it's a good idea you also you investigate your behavior saying "your question title is wrong", the fact is nothing wrong presenting the concrete example and definition. It's simple and clear "endomorphism-monoid" in your answer or identiy funciton tihng is out of topic here.

    – user11239932
    Mar 22 at 0:33















1














Monoid Laws




identity law:
combine (identity, a) == combine (a, identity)



associativity law:
combine (a, combine (b, c)) == combine (combine (a, b), c)




We can implement an identity element (identity) and binary operation (combine) for functions -






// identity element
const identity =
x => x

// binary operation
const combine = (a, b) =>
x => a (b (x))

// sample functions
const a =
x => x + 3

const b =
x => x - 1

const c =
x => x * x

// uphold laws
console.log
( combine (identity, a) (2) === combine (a, identity) (2)
, combine (a, combine (b, c)) (2) === combine (combine (a, b), c) (2)
)

// => true
// => true





The binary operation and identity element is different depending on your domain. See the table on Wikipedia for more insight on how to implement the identity element and binary operation for various sets.



Of course you are not limited to those domains. Your custom type may have various binary operations and identity elements that satisfy the monoid laws. If the laws are upheld, your type belongs to the monoid category.






share|improve this answer























  • Thanks, but x => a (b (x)) is composition of function, that is "endomorphism-monoid" which is not what this topic is about. blog.ploeh.dk/2017/11/13/endomorphism-monoid In Haskell, it's callled Endo Monoid.

    – user11239932
    Mar 21 at 23:50











  • The question is "What is the binary operator of Function Monoids in example of JavaScript?". This answer explains that any identity element and binary operation that satisfies the monoid laws forms a monoid. The answer also shows an example of a function monoid in JavaScript. The linked Wikipedia table shows other pairings that form monoids as well. I guess your question title is wrong or maybe you don't understand what you're asking.

    – user633183
    Mar 22 at 0:07











  • Well, I wrote a book about FunctionalProgramming published by one of the major publishing company in Japan. So, I know what I'm talking about. The question refers a specific Monoid with example and the title corresponds to it. I know Monoid well, and not asking here Monoid Law etc. that I also know well. Your answer is irreverent since it does not correspond to the question. Sorry.

    – user11239932
    Mar 22 at 0:18






  • 1





    You've been very quick to dismiss others and assert yourself. I can see you're very proud of your credentials, possibly to a fault. Did you bother to look at the credentials of the people offering their help? Bergi is one of the most knowledgeable contributors on this topic here on SO; also patient and respectful. You seem to know exactly what you want, so I'll leave you to find it.

    – user633183
    Mar 22 at 0:27











  • The point is my question is resolved. This is Q&A site. To answer own question is not prohibited so did I. Perhaps it's a good idea you also you investigate your behavior saying "your question title is wrong", the fact is nothing wrong presenting the concrete example and definition. It's simple and clear "endomorphism-monoid" in your answer or identiy funciton tihng is out of topic here.

    – user11239932
    Mar 22 at 0:33













1












1








1







Monoid Laws




identity law:
combine (identity, a) == combine (a, identity)



associativity law:
combine (a, combine (b, c)) == combine (combine (a, b), c)




We can implement an identity element (identity) and binary operation (combine) for functions -






// identity element
const identity =
x => x

// binary operation
const combine = (a, b) =>
x => a (b (x))

// sample functions
const a =
x => x + 3

const b =
x => x - 1

const c =
x => x * x

// uphold laws
console.log
( combine (identity, a) (2) === combine (a, identity) (2)
, combine (a, combine (b, c)) (2) === combine (combine (a, b), c) (2)
)

// => true
// => true





The binary operation and identity element is different depending on your domain. See the table on Wikipedia for more insight on how to implement the identity element and binary operation for various sets.



Of course you are not limited to those domains. Your custom type may have various binary operations and identity elements that satisfy the monoid laws. If the laws are upheld, your type belongs to the monoid category.






share|improve this answer













Monoid Laws




identity law:
combine (identity, a) == combine (a, identity)



associativity law:
combine (a, combine (b, c)) == combine (combine (a, b), c)




We can implement an identity element (identity) and binary operation (combine) for functions -






// identity element
const identity =
x => x

// binary operation
const combine = (a, b) =>
x => a (b (x))

// sample functions
const a =
x => x + 3

const b =
x => x - 1

const c =
x => x * x

// uphold laws
console.log
( combine (identity, a) (2) === combine (a, identity) (2)
, combine (a, combine (b, c)) (2) === combine (combine (a, b), c) (2)
)

// => true
// => true





The binary operation and identity element is different depending on your domain. See the table on Wikipedia for more insight on how to implement the identity element and binary operation for various sets.



Of course you are not limited to those domains. Your custom type may have various binary operations and identity elements that satisfy the monoid laws. If the laws are upheld, your type belongs to the monoid category.






// identity element
const identity =
x => x

// binary operation
const combine = (a, b) =>
x => a (b (x))

// sample functions
const a =
x => x + 3

const b =
x => x - 1

const c =
x => x * x

// uphold laws
console.log
( combine (identity, a) (2) === combine (a, identity) (2)
, combine (a, combine (b, c)) (2) === combine (combine (a, b), c) (2)
)

// => true
// => true





// identity element
const identity =
x => x

// binary operation
const combine = (a, b) =>
x => a (b (x))

// sample functions
const a =
x => x + 3

const b =
x => x - 1

const c =
x => x * x

// uphold laws
console.log
( combine (identity, a) (2) === combine (a, identity) (2)
, combine (a, combine (b, c)) (2) === combine (combine (a, b), c) (2)
)

// => true
// => true






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 21 at 23:00









user633183user633183

71.9k21143184




71.9k21143184












  • Thanks, but x => a (b (x)) is composition of function, that is "endomorphism-monoid" which is not what this topic is about. blog.ploeh.dk/2017/11/13/endomorphism-monoid In Haskell, it's callled Endo Monoid.

    – user11239932
    Mar 21 at 23:50











  • The question is "What is the binary operator of Function Monoids in example of JavaScript?". This answer explains that any identity element and binary operation that satisfies the monoid laws forms a monoid. The answer also shows an example of a function monoid in JavaScript. The linked Wikipedia table shows other pairings that form monoids as well. I guess your question title is wrong or maybe you don't understand what you're asking.

    – user633183
    Mar 22 at 0:07











  • Well, I wrote a book about FunctionalProgramming published by one of the major publishing company in Japan. So, I know what I'm talking about. The question refers a specific Monoid with example and the title corresponds to it. I know Monoid well, and not asking here Monoid Law etc. that I also know well. Your answer is irreverent since it does not correspond to the question. Sorry.

    – user11239932
    Mar 22 at 0:18






  • 1





    You've been very quick to dismiss others and assert yourself. I can see you're very proud of your credentials, possibly to a fault. Did you bother to look at the credentials of the people offering their help? Bergi is one of the most knowledgeable contributors on this topic here on SO; also patient and respectful. You seem to know exactly what you want, so I'll leave you to find it.

    – user633183
    Mar 22 at 0:27











  • The point is my question is resolved. This is Q&A site. To answer own question is not prohibited so did I. Perhaps it's a good idea you also you investigate your behavior saying "your question title is wrong", the fact is nothing wrong presenting the concrete example and definition. It's simple and clear "endomorphism-monoid" in your answer or identiy funciton tihng is out of topic here.

    – user11239932
    Mar 22 at 0:33

















  • Thanks, but x => a (b (x)) is composition of function, that is "endomorphism-monoid" which is not what this topic is about. blog.ploeh.dk/2017/11/13/endomorphism-monoid In Haskell, it's callled Endo Monoid.

    – user11239932
    Mar 21 at 23:50











  • The question is "What is the binary operator of Function Monoids in example of JavaScript?". This answer explains that any identity element and binary operation that satisfies the monoid laws forms a monoid. The answer also shows an example of a function monoid in JavaScript. The linked Wikipedia table shows other pairings that form monoids as well. I guess your question title is wrong or maybe you don't understand what you're asking.

    – user633183
    Mar 22 at 0:07











  • Well, I wrote a book about FunctionalProgramming published by one of the major publishing company in Japan. So, I know what I'm talking about. The question refers a specific Monoid with example and the title corresponds to it. I know Monoid well, and not asking here Monoid Law etc. that I also know well. Your answer is irreverent since it does not correspond to the question. Sorry.

    – user11239932
    Mar 22 at 0:18






  • 1





    You've been very quick to dismiss others and assert yourself. I can see you're very proud of your credentials, possibly to a fault. Did you bother to look at the credentials of the people offering their help? Bergi is one of the most knowledgeable contributors on this topic here on SO; also patient and respectful. You seem to know exactly what you want, so I'll leave you to find it.

    – user633183
    Mar 22 at 0:27











  • The point is my question is resolved. This is Q&A site. To answer own question is not prohibited so did I. Perhaps it's a good idea you also you investigate your behavior saying "your question title is wrong", the fact is nothing wrong presenting the concrete example and definition. It's simple and clear "endomorphism-monoid" in your answer or identiy funciton tihng is out of topic here.

    – user11239932
    Mar 22 at 0:33
















Thanks, but x => a (b (x)) is composition of function, that is "endomorphism-monoid" which is not what this topic is about. blog.ploeh.dk/2017/11/13/endomorphism-monoid In Haskell, it's callled Endo Monoid.

– user11239932
Mar 21 at 23:50





Thanks, but x => a (b (x)) is composition of function, that is "endomorphism-monoid" which is not what this topic is about. blog.ploeh.dk/2017/11/13/endomorphism-monoid In Haskell, it's callled Endo Monoid.

– user11239932
Mar 21 at 23:50













The question is "What is the binary operator of Function Monoids in example of JavaScript?". This answer explains that any identity element and binary operation that satisfies the monoid laws forms a monoid. The answer also shows an example of a function monoid in JavaScript. The linked Wikipedia table shows other pairings that form monoids as well. I guess your question title is wrong or maybe you don't understand what you're asking.

– user633183
Mar 22 at 0:07





The question is "What is the binary operator of Function Monoids in example of JavaScript?". This answer explains that any identity element and binary operation that satisfies the monoid laws forms a monoid. The answer also shows an example of a function monoid in JavaScript. The linked Wikipedia table shows other pairings that form monoids as well. I guess your question title is wrong or maybe you don't understand what you're asking.

– user633183
Mar 22 at 0:07













Well, I wrote a book about FunctionalProgramming published by one of the major publishing company in Japan. So, I know what I'm talking about. The question refers a specific Monoid with example and the title corresponds to it. I know Monoid well, and not asking here Monoid Law etc. that I also know well. Your answer is irreverent since it does not correspond to the question. Sorry.

– user11239932
Mar 22 at 0:18





Well, I wrote a book about FunctionalProgramming published by one of the major publishing company in Japan. So, I know what I'm talking about. The question refers a specific Monoid with example and the title corresponds to it. I know Monoid well, and not asking here Monoid Law etc. that I also know well. Your answer is irreverent since it does not correspond to the question. Sorry.

– user11239932
Mar 22 at 0:18




1




1





You've been very quick to dismiss others and assert yourself. I can see you're very proud of your credentials, possibly to a fault. Did you bother to look at the credentials of the people offering their help? Bergi is one of the most knowledgeable contributors on this topic here on SO; also patient and respectful. You seem to know exactly what you want, so I'll leave you to find it.

– user633183
Mar 22 at 0:27





You've been very quick to dismiss others and assert yourself. I can see you're very proud of your credentials, possibly to a fault. Did you bother to look at the credentials of the people offering their help? Bergi is one of the most knowledgeable contributors on this topic here on SO; also patient and respectful. You seem to know exactly what you want, so I'll leave you to find it.

– user633183
Mar 22 at 0:27













The point is my question is resolved. This is Q&A site. To answer own question is not prohibited so did I. Perhaps it's a good idea you also you investigate your behavior saying "your question title is wrong", the fact is nothing wrong presenting the concrete example and definition. It's simple and clear "endomorphism-monoid" in your answer or identiy funciton tihng is out of topic here.

– user11239932
Mar 22 at 0:33





The point is my question is resolved. This is Q&A site. To answer own question is not prohibited so did I. Perhaps it's a good idea you also you investigate your behavior saying "your question title is wrong", the fact is nothing wrong presenting the concrete example and definition. It's simple and clear "endomorphism-monoid" in your answer or identiy funciton tihng is out of topic here.

– user11239932
Mar 22 at 0:33













1














It's important to know that being able to combine things doesn't necessarily mean they are a monoid. For something to be a Monoid, it must have associativity, left identity, and right identity.



C#:



public static Func<Guid, int> Combine(
Func<Guid, int> f,
Func<Guid, int> g)

return x => f(x) + g(x);



JS:




function combine(f,g) 
return x => f(x) + g(x)


function addOne(x)
return x + 1;


function addTwo(x)
return x + 2;


let newFunction = combine(addOne, addTwo);
let result = newFunction(0);
console.log(result)








share|improve this answer

























  • Thanks, I probably should ask not just combine operation since this does not make sense at least, and yes it would generate a error. Small working code please in S.O.Snippet.

    – user11239932
    Mar 21 at 21:55











  • combine is a function that accepts two arguments, these arguments are assumed to be functions in this case. combine will actually return a new function that takes one argument. Inside this new function, each argument, f and g will be applied to x and their results will be added together. I hope this make it easier to understand.

    – Nick Acosta
    Mar 21 at 23:21
















1














It's important to know that being able to combine things doesn't necessarily mean they are a monoid. For something to be a Monoid, it must have associativity, left identity, and right identity.



C#:



public static Func<Guid, int> Combine(
Func<Guid, int> f,
Func<Guid, int> g)

return x => f(x) + g(x);



JS:




function combine(f,g) 
return x => f(x) + g(x)


function addOne(x)
return x + 1;


function addTwo(x)
return x + 2;


let newFunction = combine(addOne, addTwo);
let result = newFunction(0);
console.log(result)








share|improve this answer

























  • Thanks, I probably should ask not just combine operation since this does not make sense at least, and yes it would generate a error. Small working code please in S.O.Snippet.

    – user11239932
    Mar 21 at 21:55











  • combine is a function that accepts two arguments, these arguments are assumed to be functions in this case. combine will actually return a new function that takes one argument. Inside this new function, each argument, f and g will be applied to x and their results will be added together. I hope this make it easier to understand.

    – Nick Acosta
    Mar 21 at 23:21














1












1








1







It's important to know that being able to combine things doesn't necessarily mean they are a monoid. For something to be a Monoid, it must have associativity, left identity, and right identity.



C#:



public static Func<Guid, int> Combine(
Func<Guid, int> f,
Func<Guid, int> g)

return x => f(x) + g(x);



JS:




function combine(f,g) 
return x => f(x) + g(x)


function addOne(x)
return x + 1;


function addTwo(x)
return x + 2;


let newFunction = combine(addOne, addTwo);
let result = newFunction(0);
console.log(result)








share|improve this answer















It's important to know that being able to combine things doesn't necessarily mean they are a monoid. For something to be a Monoid, it must have associativity, left identity, and right identity.



C#:



public static Func<Guid, int> Combine(
Func<Guid, int> f,
Func<Guid, int> g)

return x => f(x) + g(x);



JS:




function combine(f,g) 
return x => f(x) + g(x)


function addOne(x)
return x + 1;


function addTwo(x)
return x + 2;


let newFunction = combine(addOne, addTwo);
let result = newFunction(0);
console.log(result)








function combine(f,g) 
return x => f(x) + g(x)


function addOne(x)
return x + 1;


function addTwo(x)
return x + 2;


let newFunction = combine(addOne, addTwo);
let result = newFunction(0);
console.log(result)





function combine(f,g) 
return x => f(x) + g(x)


function addOne(x)
return x + 1;


function addTwo(x)
return x + 2;


let newFunction = combine(addOne, addTwo);
let result = newFunction(0);
console.log(result)






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 21 at 23:28

























answered Mar 21 at 21:50









Nick AcostaNick Acosta

1,6361019




1,6361019












  • Thanks, I probably should ask not just combine operation since this does not make sense at least, and yes it would generate a error. Small working code please in S.O.Snippet.

    – user11239932
    Mar 21 at 21:55











  • combine is a function that accepts two arguments, these arguments are assumed to be functions in this case. combine will actually return a new function that takes one argument. Inside this new function, each argument, f and g will be applied to x and their results will be added together. I hope this make it easier to understand.

    – Nick Acosta
    Mar 21 at 23:21


















  • Thanks, I probably should ask not just combine operation since this does not make sense at least, and yes it would generate a error. Small working code please in S.O.Snippet.

    – user11239932
    Mar 21 at 21:55











  • combine is a function that accepts two arguments, these arguments are assumed to be functions in this case. combine will actually return a new function that takes one argument. Inside this new function, each argument, f and g will be applied to x and their results will be added together. I hope this make it easier to understand.

    – Nick Acosta
    Mar 21 at 23:21

















Thanks, I probably should ask not just combine operation since this does not make sense at least, and yes it would generate a error. Small working code please in S.O.Snippet.

– user11239932
Mar 21 at 21:55





Thanks, I probably should ask not just combine operation since this does not make sense at least, and yes it would generate a error. Small working code please in S.O.Snippet.

– user11239932
Mar 21 at 21:55













combine is a function that accepts two arguments, these arguments are assumed to be functions in this case. combine will actually return a new function that takes one argument. Inside this new function, each argument, f and g will be applied to x and their results will be added together. I hope this make it easier to understand.

– Nick Acosta
Mar 21 at 23:21






combine is a function that accepts two arguments, these arguments are assumed to be functions in this case. combine will actually return a new function that takes one argument. Inside this new function, each argument, f and g will be applied to x and their results will be added together. I hope this make it easier to understand.

– Nick Acosta
Mar 21 at 23:21


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55289369%2fwhat-is-the-binary-operator-of-function-monoids-in-example-of-javascript%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해