Instructor: 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
With the quick save, we'll remember that his name was Paulson, Robert.