This Lesson is for Members

Subscribe today and get access to all lessons! Plus direct HD download for offline use, enhances transcripts, member comment forums, and iTunes "podcast" RSS feed. Level up your skills now!

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Understand Transformer Functions

    Paul FrendPaul Frend

    The next transducer building block we need to explore is a transformer. A transformer is a function that takes a value and returns a transformed version of it.

    In this lesson, we cover some examples of transformer functions and use them with some basic composition. We'll follow the functional style of creating new values, avoiding mutations. Even though they're trivial functions, it's important to understand how they work, as soon we’ll be combining them with reducers to create transducers.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Instructor: 00:00 We've got a string, hello, saved into the variable mystring. Now, let's call toUpperCase on it. As expected, we get the uppercase version of hello. Calling toUpperCase won't actually change our string, but will return our transformed value.

    00:17 We can easily make this transformation into its own function, so let's call that toUpper. Then, we'll take a string, and we're going to return that string then after calling toUpperCase on it. We forgot our arrow. Then, we can call that with any string we want, and if we log out our original string, we can see that that still hasn't been modified.

    00:40 We've got a function here that, when given a value, returns a new transformed value. Now, let's see if we can compose this with some other operations. Let's create a new function called Shout which will also take a string. This is going to return the same string, but with some exclamation marks on it.

    01:01 Let's make sure we get back what we expect. As we can see, we get our string with our two exclamation marks. Now, let's try and combine our two string transformers. Let's create a new function called Scream. That's going to take a string like before, but it's going to call toUpper, but as its input, it's going to call Shout. Then, we pass the string in.

    01:26 Now, if we run that, we can see we get a new string representation that combines both toUpper and Shout. The execution of Shout gets passed as an argument into toUpper. As you can see, these transformers compose naturally as long as the input and output types don't change.

    01:45 Anything that expects a string, like toUpper, for instance, doesn't care if that string comes from a string literal, or a function that produces a string, or a function that produces a function that produces a string, as long as the outcome is a string. That's why we can compose these functions together.

    02:02 Our transformers are simple functions that take in some value and return a new representation of that value.

    Discuss

    Discuss