Delay Evaluation with LazyBox

Brian Lonsdorf
InstructorBrian Lonsdorf
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 5 years ago

We rewrite the Box example using lazy evaulation

[00:01] With Box, we are able to map over each function, and pass our input to output, and work-like function composition here. If we run it, we get a lowercase A here, as it goes through and trims, turns things into numbers, and whatnot.

[00:13] Now we could also define a lazybox. A lazybox will take, instead of an X, will take a G here for a function. We want to convert our value that's right there, our concrete value, to a function that will eventually return our value.

[00:27] Sometimes this is referred to as a Church encoding. What we can do here is define a map. How would that work? It takes our F, and we want to return a lazybox that has a function inside. Since this is function composition, we could quite literally just run F of G here, and have our function composition.

[00:42] Fold works the same way, except fold will not bottle it back up. It'll just run it right away, so we don't need another layer of the lazybox here. What we get from this is that none of this will actually run, if I run my node there. It will just sit here.

[00:56] In fact, we could go ahead and console.log, there we go, and see that nothing runs. Now if we bring it back, and we run fold, that's like pulling the trigger. There we are. We have the same results.

[01:08] This gives us purity by virtue of laziness. Basically, nothing happens, so we don't have any impure side effects, until the very end, when we call fold. We're pushing it all the way down to the bottom. This is how a variety of types define map, where they have a function inside them instead of a concrete value, such as promises, observables, or streams, things like this.

egghead
egghead
~ 47 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today