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.

Why is the class/constructor required as a parameter for `traverse` (or sequence)?

See original GitHub issue

A Fantasy-land compliant version of traverse might go something like this:

S.traverse (Array) (S.words) (S.Just ('foo bar baz'))

But why are we asking for the first (Array) parameter? If the words function must return an Array, could we not access the constructor of the Array as such:

const foo  = [1,2,3]
const foo2 = foo.constructor.of( 4,5,6 )

So why do we need Array at all? Here S.words returns an Array Monad contained within the Maybe. We can then derive the Array constructor as mentioned above to sequence an Array of Maybes.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
evilsoftcommented, Jul 13, 2018

Pardon the silly pattern, but this is one way that I have implemented traverse on a Maybe type: https://github.com/evilsoft/crocks/blob/master/src/core/Maybe.js#L210

The left side of that either function is for the Nothing case, so it composes a Nothing constructor before the af or Applicative function resulting in the Applicative m => m Maybe a

So in short: the traverse is “run”, but the “lifing” function is not. That af needs to come from somewhere, and that is what the TypeRep or a -> m a in the case of crocks (we accept both the Applicative TypeRep or an Apply returning function, which is why this pattern looks so silly.)

EDIT: notice that af is based on the f that is passed in and it only appears in the left or Nothing case.

EDIT AGAIN: Oh looks like the comment I was replying to was deleted. So sorry about that!

1reaction
babaknesscommented, Jul 12, 2018

Ah, right ok, you get Nothing not [ Nothing ]

Read more comments on GitHub >

github_iconTop Results From Across the Web

Constructors in Java - GeeksforGeeks
A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with our own values,...
Read more >
control initialize order when Python dataclass inheriting a class
There are two reasons for calling a parent's constructor, 1) to instantiate arguments that are to be handled by the parent's constructor, ...
Read more >
Untitled
C.3 — A parameter is a value that is passed into a constructor. These are often referred to as actual parameters. Introduction to...
Read more >
Abstract Data Types in C++
An array of class-type objects is default initialized by calling the default constructor on each element. Thus, the element type must have an...
Read more >
Traverse - Typelevel
In order to call traverse Id needs to be Applicative which is straightforward - note that while Id just wraps an A ,...
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