question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What is it called when one of these methods gets applied to a Functor's value?

See original GitHub issue

The simplest example I can think of is here:

https://github.com/folktale/data.either/blob/master/lib/either.js#L252-L259

An Either doesn’t exactly have a concat, but if its an Either of a Semigroup, then you can concat the underlying Semigroup simply by calling concat. Is there a specific term for these kind of operations on the values of functors? They’re basically just a layer of indirection on top of map or chain.

I’m building a parser combinators library and I’m not sure if its Kosher to allow a concat method to join parser results together.

  concat(p) {
    return this.chain(vs => p.map(v => vs.concat([v])))
  }

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
joneshfcommented, Jan 17, 2017

@ccorcos I don’t have an answer to your title question. The examples you give can lead to some discussion though. Either a b has more than one implementation for Semigroup. For instance:

const Left = x => ({
  concat: other => other,
});

const Right = x => ({
  concat: _ => Right(x),
});

That implementation does not rely at all on the underlying value. I know you mentioned Either a b as an example, but it’s important to remember that we can sometimes do things differently.

Your parser combinator library could function in a similar way. Instead of trying to concat the underlying value, concat the structure. This is what naturally falls out of parser combinators anyway in a language with support for higher kinded polymorphism. The Alt, Plus, and Alternative algebras follow naturally from parser combinators. The reason we have alt as f a ~> f a -> f a instead of a ~> a -> a is precisely because we want to talk about concating the structure and not the underlying value.

The point I’m trying to make is that you should explore using Alt, Plus, and Alternative for your parser combinator library.

2reactions
joneshfcommented, Jan 18, 2017

the example you give for Either isnt exactly useful how the other one is…

I find it pretty useful.

const gt10 = x => x > 10 ? Right(x) : Left("too small");
[1,5,124,41,31,2,3,13].reduce((acc, x) => acc.concat(gt10(x)), Left("empty"))
//=> Right(124)
[1,5,1,2,4,4,1,3,1,2,3,1,3].reduce((acc, x) => acc.concat(gt10(x)), Left("empty"))
//=> Left("too small")
[].reduce((acc, x) => acc.concat(gt10(x)), Left("empty"))
//=> Left("empty")

I can make a safe find that tells me what happened without trying too hard. It’s safe in the sense that I don’t have to deal with null or undefined.

It seems to me that the answer is that this shouldnt actually be allowed. You shouldn’t be able to concat two Eithers in a way that assumes its value to be a Semigroup.

That’s too restrictive and not really something the spec should care about. It’s way too low level of a thing to add. In the scope of Semigroup, we shouldn’t care if an Either a b data type even exists. We should definitely not special case the spec for data types. That leads to the craziness of promises.

It also severely limits the usefulness of Semigroup. You’d expect that something like this could be written once and for all:

const lift2 = f => x => y => y.ap(x.map(f));
const concat = a => b => a.concat(b);
const applyConcat = lift2(concat);

But saying that Either a b cannot do such a thing would make this function really awkward.

That is unless you specifically define that the value of an Either must be a Semigroup.

data.either does that in this case: https://github.com/folktale/data.either/blob/f9b20a582c7287528df328a5d13a662b8d454b52/lib/either.js#L244. Well, it requires a Monoid, but close enough.

I think FantasyLand would be infinitely more valuable to people if there was a section for examples of each of these types in practical situations.

Please contribute with some examples! You’re writing a perfect one for Alt, Plus and Alternative. There’s more discussion here: https://github.com/fantasyland/fantasy-land/issues/177.

Read more comments on GitHub >

github_iconTop Results From Across the Web

C++ Tutorial: Functors (Function Objects) - 2020
In other words, a functor is any object that can be used with () in the manner of a function. This includes normal...
Read more >
Functors in C++ - GeeksforGeeks
A functor (or function object) is a C++ class that acts like a function. Functors are called using the same old function call...
Read more >
What are these applicative functors you speak of? | Devlog
This method is usually called map , its only purpose is to give us access to the value so we can transform it...
Read more >
What is a functor? - Medium
A post in Functional JavaScript Blog states that a functor is a function that, “given a value and a function, unwraps the values...
Read more >
What are C++ functors and their uses? - Stack Overflow
A functor is a class or struct object which can be "called" like a function. This is made possible by overloading the ()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found