Apply Stateful Computations To Functions

Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 5 years ago

When building our stateful computations, there will come a time when we’ll need to combine two or more state transactions at the same time to come up with a new result. Usually this occurs when want to use plain ol’ JavaScript functions with two or more a arguments as part of our stateful computations.

We first look at how this can be accomplished by using chain and closure to get access to both functions. Then we will explore how we can leverage the of construction helper and the ap State instance method to clean this type of interaction up a bit. We then conclude with an even cleaner approach that takes full advantage of a crocks helper function named liftA2.

Instructor: [00:00] I need that often arises when combining stateful computations requires the ability to combine multiple state transactions concurrently. To explore some techniques for handling these cases as they come up, let's have a quick tour of the bits on our work bench.

[00:14] First, we plug the getConstruction helper of our state constructor and bringing these two helper functions. Then, we define two state accessors. The first being get first, which is the state string of string that extracts the first name from our state by using getWord to grab the first word of a given string.

[00:32] We also have getLast which is also a state string of string. It uses the same means of extraction as getFirst. Using these accesors, we'd like to take both of the names and format them into a new string that puts the last name first and separates them with the comma. We'll enlist this namify function to take care of the formatting for us. All things have to start somewhere, let's kick this off by creating a state instance that will simply named format.

[00:59] We define format to be a state string of string. As namify needs the first name first, we'll start with getFrist. Next, we'll use our log function to see what that gets us by passing and format, and saving it down to see we get back a lazy old state instance, which will run with an initial state of Robert Paulson, which in turn gives us back a pair Robert Robert Paulson.

[01:22] We're really only concerned with the resultant right now. We'll use the WOW with to run our instance, seeing our expected Robert. With the first name got, we now need to access the last name for namify.

[01:34] One way to accomplish having both first and last and scope is by using chain. We chain in a function that unwraps our first name F and returns our getLast accessor, resulting in the last name Paulson for our resultant.

[01:49] To apply both resultants to namify, we just need a map over getLast with namify partially applied with F getting back our Paulson, Robert. While we super duper got this working, it's pretty horrible to look at and will properly be hard to grock, if we ever need to get back in here again.

[02:07] I think we may be able to do a much nicer job by leaning on a state instance method name ap. AP is short for apply and it's defined on a state S with the function A to B in the resultant. We pass it as state S of A, which it'll apply the A to our function and wrap the result in a new state S of B.

[02:27] We'll be taking a much different approach using ap. We'll first clean a little house and start fresh for formats body. We're immediately presented with our first challenge. Looks like we need to start with the function and our resultant.

[02:42] One way we can do that is to use the arg construction helper, which is defined on the state constructor. Arg takes any given type A as input and gives us back a state S of A. As A can be any type, that means it can also be a function. Let's use arg by calling it with our curried namify function.

[03:02] Seeing that our resultant now contains our function. We now need to apply the first name to our REPT function, which will get us back another function ready for the last name. We just call ap on our instance passing our getFirst accerssor, resulting in a new instance with the function in our resultant.

[03:19] To apply the last name, we just use ap to apply the getLast accessor. With all arguments applied to namify, we get back our expected Paulson, Robert. With the little rearrangement, we can relieve arg of it's duty, so they can get back to more important things.

[03:36] We'll replace this ap call with map, supplying namify for its expected function, and use it to map over our getFrist applying our first name to the namify function. This leads us with our partially applied function and the resultant ready for our application of getLast.

[03:53] One thing to note is that this interface allows us to lift any curried function into any AVT that implements it, not just our beloved state. Crocks provides a helper function called lift A to that will lift any binary function and applied to it two instances of a given date type that implements ap.

[04:12] Giving us back the result wrapped up in the given data type. We'll replace our format body one last time calling lift A to for this go around, passing it our binary namify function followed by our two state instances, getFirst and getLast.

[04:30] With the quick save, we'll remember that his name was Paulson, Robert.

egghead
egghead
~ 4 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