![Enterprise Architecture Patterns](https://static.frontendmasters.com/assets/courses/2021-02-02-enterprise-patterns/thumb.webp)
Check out a free preview of the full Enterprise Architecture Patterns course
The "Mapping to Functions" Lesson is part of the full, Enterprise Architecture Patterns course featured in this preview video. Here's what you'd learn in this lesson:
Lukas explains in more detail how partially applied functions are passed into the stream and called at a later time. The mapTo method passes the property and value that should be applied to the position property once it enters the stream.
Transcript from the "Mapping to Functions" Lesson
[00:00:00]
>> So all we're doing is left, right, up, down. But in the code what is happening, Is, That, So I think the first part of this is pretty straightforward. We're listening for a keydown event, and then we're filtering out based on the arrow. So we're creating an up, down, left, and right stream.
[00:00:34]
And then from here, we're merging these together. And then I'm taking the current or the accumulated value, which is a position object. And then, the current value, it's a function that we're then calling and passing in the accumulated position objects. So for instance, if you take increment what this expects is an object with a property and a value.
[00:01:10]
And so we're saying we don't know what property you want to increment. You can define that. And we don't know how much you want to increment it by, you can also define that. And so in our MapToFunction, we're saying we want to return a partially applied function. That we definitely know we want to do X and we want to increment by 10.
[00:01:42]
We want to decrement by 10. Or we want to do y, an increment or decrement by 10. And when we supply this final position object, then you're able to calculate this, and return this to complete the calculation. And it goes into the stream which is how we're able to do this.
[00:02:07]
So partial application is super interesting. I spent an entire afternoon wrapping my mind around it, so if in part you're kind of looking at this and being like that was a little cuckoo, I don't know if I got it, don't worry. [LAUGH] It took me a while. Even after this, I realized I still didn't truly understand it.
[00:02:30]
So more importantly, what I want to convey is that you can send not only objects and values, like primitive values, but functions into your observable stream, so that they can be executed at some point in the future, which is extremely powerful. And so now we're just slightly starting to kind of flirt with some of this high level, distributed, enterprise-y stuff.
[00:03:01]
When I think of enterprise-y stuff, this is now kind of the realm that we start to get into kind of this dynamic functional programming at scale. Well, we're kind of starting to touch on that just a little bit.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops