Use a Javascript Array to Construct a Maybe

Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 5 years ago

Much of our code in real life is full of if statements that check the validity of a given input to see if a given computation should be performed. Using the ever popular Maybe construction allows us capture this disjunction in one place, keeping our functions free of similar if statements that can pollute the intention of the function. You do not even need a fancy library to get this benefit, as Javascript ships with everything you need with it's build in Array.

[00:00] To start off, let's first introduce our starring cast. We have three functions to play with, starting with add10 which takes a number to a number. We pass it any number and it adds 10 returning the result.

[00:12] Next up is square which also takes a number to a number, but it will take the number it receives and square it instead. Finally, we have doMath which forms a composition of the two. Our data is piped through these functions right to left, and then returns the result.

[00:28] Let's take these for a spin and see how they drive. Using this log function, we'll just call doMath passing it 0and give it a save.

[00:37] Let's take a gander at how we arrived at 100. 0was passed into add 10, passing 10 onto square, which of course gets us our hundo. By passing in 1, we get the expected 121. While this works when we have numbers, by passing in this little kitty cat we find that our flow is not very purr-fect.

[00:57] This is because our functions work only with numbers, so we need to deal with that constraint. Luckily, we have isValid, which will only return true when passed a finite number. One way to solve our issue, is to use isValid directly in our function, passing in our input. If we're good, we perform the computation, or we just return our value if not. This is now required in every function dependent on this value, and future us will be super mad at current us if we actually did this.

[01:28] As it turns out, we can make everyone happy here by using a maybe. To start, we need a way to list a given value into a maybe based on some condition. To do this, we'll create a function called maybe. Maybe is defined as a function with two parameters.

[01:45] First, it takes a predicate that we'll use to decide how to construct the maybe. Then it takes a value of any type that could be valid, and finally, it returns us a maybe of A. To implement this, we first take our predicate and return a function that accepts a value X.

[02:01] Now we test our value by running it through pred. If it passes, we return an array housing our value. Otherwise, we return an empty array. Now we just expose this abomination to the world, and see what shenanigans we can get up to.

[02:16] We gain access to our newly created maybe function by plucking it off this conveniently available maybe object with some destructuring. Now we create a function named maybeValid that is specific to what we want to guard for.

[02:30] We will define maybeValid as a function that will take any type splat as its input, and return us a maybe of our desired number. To implement, all we need to do is apply the isValid predicate to maybe.

[02:43] Meow we're ready to test this out on our kitty. We just replace doMath with maybeValid, give it a save. We see we get back our nothing. But if we pass a 0we get an array of 0or just 0A 1 will get us just 1, and a valid 10 arrives at just 10. While this is great, our functions work with numbers not maybes of numbers.

[03:07] We need a way to lift our functions into our maybes so they can be applied to our numbers. To do this, we'll create another maybe function called mapMaybe. mapMaybe is defined as a function that takes a function from A to B, then expects a maybe of A and after applying our function, returns us a brand new maybe of B.

[03:29] To implement this, first we take a function FN and return a function ready to take our maybe M. Because we're using an array, we just apply our function to the array's built-in map, returning the result.

[03:43] Like our maybe function, we just expose this function by exporting it from our module. To put it to good use, we pop to the top of our index file and just pluck it off of our maybe namespace.

[03:55] Now it's time to add a little safety to our flow. To do this, we'll create a little function called safeMath which we'll use to lift our doMath function into our newfound maybe. We define safeMath as a function that will take anything A and kick us back our desired maybe of number.

[04:12] The implementation is just a composition that will first run our value through maybeValid, returning a maybe, that then gets passed to mapMaybe, which we partially applied doMath to. Let's give it a shot by replacing maybeValid with safeMath, passing in 10 to get just 400. 1 gets us our just 121, and 0will of course still gets our hundo.

[04:38] Any invalid value returns us a nothing, as we see here with NaN or with an object. As long as we're in a maybe, our functions will only be applied to values that we deem valid. Now we can extend our flows with confidence.

[04:53] Let's set up a composition with the ridiculously long name safeStringMath. Similar to safeMath, we can define safeStringMath as a function that takes any value A, but returns us a maybe of string instead. To get our maybeString, we extend safeMath with a composition that will use mapMaybe to lift and apply this stringify helper to the resulting maybe number that is returned from our safeMath function. Stringify takes any value and returns a string representation.

[05:24] Mapping our maybeNumber to the desired maybeString. To verify our object still returns a nothing, we just pass it to safeStringMath and give it a save. But when we pass a number, we now get back a maybe of string. Passing 21 gets us 961, 0still gets us our hundo, and 121 is the result of 1, except they are all strings still wrapped in a maybe. Eventually, we'll have to leave the safety and comfort of maybe and jump back into simple values.

[05:55] To do this, we can create another function called optionMaybe. We'll define our optionMaybe as a function that will take any type A returning another function that takes a maybe of A, giving us an A.

[06:09] To implement, we first take our default def and our maybe M. Then we check the length of M to determine if we're a nothing or a just. A just returns the extracted value, and nothing returns our default. Let's expose our option function and see what this looks like in practice.

[06:28] Like our other maybe functions, we just pluck it off of our maybe namespace for use in our file. Now, to work this into our current flow, we'll create another composition which we'll call stringMath. stringMath is defined as a function that will take any value A, and return us a [inaudible] string regardless of our input. We implement with yet another composition that will option our maybe with a default string of none, acting on the maybe that is returned from safeStringMath.

[06:59] Now with a little swaperoo, we can replace safeStringMath with stringMath. Give it a save, and observe our string of 121. Passing in 0gives us our hundo string, and 10 arrives at our string of 400. But passing in an invalid happy cat will safely default to none.

Massimiliano Filacchioni
Massimiliano Filacchioni
~ 6 years ago

This lesson is very clear and nice. I'm trying to better understand the notation used for function signatures, for this reason I would like to know if the following is also a valid signature for maybe:

// maybe : (a -> Boolean) -> a -> Maybe b

In other words, can b represent all values of a that satisfy the predicate, or have you used * (that for my background I find more intuitive anyway :-)) because it cannot?

Ian Hofmann-Hicks
Ian Hofmann-Hicksinstructor
~ 6 years ago

Good :eye:. You are :100: correct with that siggy and for your reasons for using a and b.

I went with * because I figured most casual learners would understand the wildcard-ness of that symbol, without having to explain that as sure as heck can be bs.

When I write that siggy IRL I typically flip the a and blike this little number:

// maybe :: (b -> Boolean) -> b -> Maybe a

as seen here: https://evilsoft.github.io/crocks/docs/crocks/Maybe.html#safe

Your are totes spot on with your intuition.

Markdown supported.
Become a member to join the discussionEnroll Today