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 835 of the free egghead.io lessons, plus get JavaScript content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.



A collection of Either examples compared to imperative code

2:05 JavaScript lesson by

A tour of simple functions rewritten as composed expressions using Either set to 8 byte music and colorful flying blocks


egghead.io comment guidelines

Avatar
egghead.io

A tour of simple functions rewritten as composed expressions using Either set to 8 byte music and colorful flying blocks

Avatar
David

Holy crap, the one at 1:30, with the wrap and fs.readfilesync is still just blowing my mind. I don't know if it's better, but it's definitely harder for me to reason about that the moment. I think have all the references to what tryCatch is, etc, may help with context. Or just practice, doing, and refactoring real code to get the hang of it.

Regardless, loving this course so far!

In reply to egghead.io
Avatar
Nick Ostrovsky

Wrap has an error. Should be something like preview => { ...example, preview }, not ex => ....

Avatar
Alexander

Yes, wondered about that for a bit too. Although I'd describe it like this: the fold on unpacking the read 'preview' from the file should clone example and then assign the new preview prop.

.fold(() => example,
        preview => Object.assign({ preview }, example))

I would maybe even Object.assign({}, example, { preview }) to be nice and obvious.

In reply to Nick Ostrovsky
Avatar
Brian Lonsdorf

Yep! Sorry about the mistake there. This is what I should have had

In reply to Alexander
Avatar
Egghead

Hi,

In second example (getPrefs method), shoul we also check for existance of 'preferences' property of user object?

Avatar
Brian Lonsdorf

You totally can! We can check everything we feel like.

In reply to Egghead
Avatar
Egghead

Hi Brian,
Thanks for reponse, one question regarding 'checking' if something exist or if parameters are 'right' type or not empty, what is your approach or best practice, do you check everything for existance(or if parameters are right type or not empty)?

In reply to Brian Lonsdorf
Avatar
Brian Lonsdorf

I think the best practice is to return Either's from low level "partial" functions that might return null. If they do that, then when you're at an application level, you've ensured existence at the edges so you don't end up with a bunch of unnecessary (paranoia-driven-development) checks in your main logic.

In reply to Egghead
Avatar
Luis Silva

In parseDbUrl example, there is a small difference between the imperative code and the functional, if the c.url is falsy the imperative will return undefined and the functional will return null.

All right, class. It's movie time. Hit the lights, please. We're going to see various examples of imperative code, compared to the equivalent composed expressions using Either. Enjoy.

[music]

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