Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 960 of the free egghead.io lessons, plus get JavaScript content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Lift into a Pointed Functor with of

1:21 JavaScript lesson by

We examine the of function we've seen on a few types and discover it's the Pointed interface.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

We examine the of function we've seen on a few types and discover it's the Pointed interface.

Avatar
Nik

Would anyone be kind enough to explain to me the difference between making a Box with Box(3) in the first episode and with of as in Box.of(3) in this ep?

In reply to egghead.io
Avatar
Brian Lonsdorf

Hi Nik!

The idea is that of is a generic way to place a value into a Functor. So those two are exactly equivalent when it comes to Box, but for more complex constructors like new Task((rej, res)... it gives us an interface to program against that's "constructor agnostic".

In reply to Nik

You may have seen this function of popup here now. It's really a generic interface to be able to place a value into our type, or we call it lifting a value to our type. Here, we have Task.of. If we say ('hello'), we end up with a //Task('hello'). If we have an Either.of('hello'), we end up with a //Right('hello').

of.js

Task.of('hello') // Task('hello')
Either.of('hello') // Right('hello')

Similarly, if we have a Box.of say (100), we're just picking arbitrary values here. We're just putting those values inside our types, and this is regardless of any constructor complexities here.

If you recall Task as the constructor where we take a rej function, a res function, and resolve this.

new Task((rej, res) =>
    res('hello'))

This would be much more complex and not to the generic interface that I could use to program functions that just pop a value into a type, not worrying about any constructors or specifics.

"Why did we choose Right instead of Left?"

That's a terrific question. The reason for this is that one of the contracts or intuitions of of here, is that as soon as I pop a value into my of, I want to be able to start mapping and chaining and using all of those generic interface functions on this value. If this was to return a Left, we wouldn't be able to actually map over it.

Either.of('hello').map(x => x + '!')

It would just ignore maps and chains. That's kind of ignoring the contract of of, which would just lift a value into a type and start working with it as if it's a total success and things work the way they should.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?