Understand Transformer Functions

Paul Frend
InstructorPaul Frend
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 5 years ago

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.

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.

Ioan Lucut
Ioan Lucut
~ 6 years ago

Hi! Thank you for your lesson! May I ask what kind of plugin are you using in the Webstorm for compiling & run the lines on the fly?

Paul Frend
Paul Frendinstructor
~ 6 years ago

Thanks @Ioan! The plugin is called quokka, check it out https://quokkajs.com/

Tre' Codez
Tre' Codez
~ 4 years ago

Oh, not Wallaby! Checking it out now then!

Markdown supported.
Become a member to join the discussionEnroll Today