Stateful computations require the ability for their state to change overtime. We take a look on one way to replace the state portion of our stateful datatype with a value. We explore the mechanics of how this can be accomplished and introduce the put
construction helper. We also see how we can run our instances with execWith
to extract the state portion and discard the resultant.
Instructor: [00:00] When working with stateful computations, it's often handy to be able to swap out our State with a new value. To see what a construction like this would look like, let's pull in both the State and Pair constructors from crocks. Using these constructors, we'll create a function called putState that puts a given value into our State.
[00:19] PutState is defined as a function that takes a given State s and returns a State s of unit. Since unit is in our resultant, let's jump to the top and bring in its constructor from crocks as well.
[00:32] With all the pieces on the table, we can implement putState by building a function that accepts a value of type s and returns an instance that ignores the current State, and returns a Pair with the unit in the resultant and our given state in the State position, effectively replacing the current State with our value.
[00:52] Now let's check out our handiwork and log out the result of calling our funky fresh function with Grand Canyon as the value and then running it with a State of evergreen by passing the string evergreen to the runWith function. With a quick save, we see we get back a pair unit(Grand Canyon). Using snd to pull the State, we get back Grand Canyon.
[01:13] By replacing runWith with evalWith, we see our unit is in the resultant. Like evalWith, State provides execWith that will unwrap the State from the resulting Pair, throwing out the resultant.
[01:26] A common use case for this construction is to allow for the State to be reset to an initial value. Let's define our reset to be a function that goes from unit to a State s of unit where s, in our case, is fixed to the type String. To implement, we create a function that takes Nothing and returns the result of calling putState with our pointed value of Grand Canyon.
[01:52] We can now replace our call to putState with a call to our reset function. Give it a save, and we see that we still are in the Grand Canyon state even though we started off in the evergreen state. Replacing State with a new value is such a common construction that State provides a construction helper named put that will handle all the plumbing for us.
[02:11] We just destructure it off of the State constructor like so and get rid of all this cruft that is no longer needed. Now we simply replace our old putState function with put and save it down. Seeing that we still have Grand Canyon as our State and by calling evalWith to pull the resultant, we see our unit is still there, resulting in an overall pair of unit(Grand Canyon).
@Andrew
Thank you so much for taking the time to provide feedback. I :heart: being able to know what I can do better to create content that others will value.
I apologize for coming of as condescending with what my coworkers call "Presentation Ian". I will do what I can to remove this tone from my future videos, as it is not my intent.
To address the difference between the Livecode vids and the content for egghead it really is a matter of context and function. It can be very challenging to demonstrate a complex thought in a span of 3-7 mins and even harder to do it on the fly. So the way I approach creation of this content is with a scripted approach that can be reviewed and edited before the costly act of recording. Just to make sure the point is driven home, without too much fluff. Even the placement of puns is strategic to drive home points or provide a demarcation point on a chain of thoughts.
When I riff and speak naturally on those livecodes, it can take hours over several videos to explain something and many times, due to the riff nature of it all, I make mistakes on the intuition I am trying to communicate.
So with these vids to allow for natural "punch-in"s during the production process, I can use that inflection to my advantage. If I rise at the end of a statement, I can punch in any edits or corrections easily keeping me from re-recording 2-3 mins to get it to sound natural.
But it sounds like, I may need to alter the inflections a bit so I get what I am looking for and not come off as a salesman or something less desirable. I have started to put some thought in this, and will try some other approaching in my videos going forward.
Thanks again for taking the time, feedback like this is very valuable to me in my learning process of delivering content!
I find your content very interesting. So much so, that I looked up your youtube channel and watched your series on ADTs. Very engaging! You're an excellent teacher and very knowledgeable. However, I find your manner of speaking in these egghead videos somewhat off-putting. It feels heavily affected and condescending. It makes it difficult to pay attention to the content. In your youtube series you talk very naturally and conversationally, and as such I find the material presented there much more accessible and enjoyable. I look forward to future content from you, but for your next series on egghead I really hope you drop the affectation and talk naturally. You have excellent material. It doesn't need any embellishment :)