Instructor: We'll be doing three things to multiply two lists from our initial state. First, we pull our colors array, followed by pulling our shapes array. Finally, we multiply them over a combining function. Our initial state contains both arrays, a colors array and an array of shape names.
Let's kick this off by first pulling our colors from our state. We'll jump into our game model and export a new transaction that places our colors array in our resultant, calling it getColors, taking a unit as its input. We define the getColors transaction as a function that takes a unit to a state appState of array of string.
We implement getColors using our getState helper function, pointing at our colors array. getState will look up the colors attribute and place whatever resides there in the resultant of our state instance.
In our index file, we import getColors by plucking it off of data model game. Then we log out the result of calling it down below to get our expected state instance ready to be run with our initial state by using evalWith to peek at the resultant, although it's not what we expect.
We want an array of string but have a maybe of array of string due to getState returning a maybe. That's cool. We'll just option out the maybe with an empty array by mapping our state. Problem solved. That finishes up the first task and kind of solves the second because we need to do the same thing for getShapes.
With some copypasta love, we duplicate this flow and change the name and attribute we're pointing to. Now we can pull shapes like a boss. Nice. To demonstrate, we replace getColors with getShapes up top and down below, followed by a quick save to see just how cool we are.
Now we want to multiply these arrays over this function that takes a string to a function that takes a string to a card, called buildCard. buildCard is a curried function that accepts names for color and shape and returns an object that constructs an ID and adds the names to the object, thus giving us a card.
To multiply, we'll take advantage of the fact that array, just like state, is an applicative functor. To demonstrate, we'll export this function that we aptly name buildCards, plural, that takes an array of string, then another array of string and gives us back an array of card.
Implementation is as simple as applying liftA2 with buildCard ready to take two arrays of string. To prove that this does what we think it does, we import it into our index file and replace our getShapes mess with a call to buildCards, passing an array of one, two, three for the first argument and the strings A, B for the second.
Voilà. Array multiplication at it's finest. Not convinced this is multiplication? Checking the length gives us back 2*3, or 6. Adding a fourth gives us eight. If that's not multiplication, I don't know what is.
Moving right along, we need to get this into our state. Again, we rely on the applicative, this time for state, by exporting a function we cleverly call generateCards, which we define as a function that goes from unit to a state appState of array of card.
We just lean on the Crocks converge combinator and lift buildCards into our state flow by partially applying it to liftA2 for our combining function.
With getColors coming in on the left and getShapes coming in on the right, we're ready to show this off to the world, which we do by replacing buildCards with generateCards throughout our index and then promptly call it with unit to see our expected state instance. When peeking at the resultant, we see our expected results. A quick check of length is 3*4, or 12.