Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Understand Transformer Functions


    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.



    Become a Member to view code

    You must be a Pro 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
    orLog In




    Instructor: 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.

    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.

    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.

    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.

    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.

    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.

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