What is an isomorphism? You may have heard some rubbish about running code on the client and the server. It has nothing to do with an isomorphism.
An isomorphism is a pair of functions, to and from, where if I call two on x followed by a from, I should just get back my original x. It means I can convert and convert back to get my original x. We can do the same thing with the y here. We can go from and to to get back our y.
This is an interesting relationship. What it means is these functions prove our data type holds the same information as another data type. I claim a string is isomorphic to an array of characters. These two data types should hold the same information and be able to convert there and back without losing anything.
Let's go ahead and formalize this notion and make an example here. We have an isomorphism that takes a to and a from. We'll just package them up into a type here. There we are.
How might we turn our string into a list of characters? We'll make an isomorph. Let's call this isomorphism jars. Here, we are with the isomorphism. All we have to do is take a string and split it on everything. To get from our jars back to a string, we could just join it on everything. There's an isomorphism for us.
Let's use it in action. We have our jars. Let's call our results here, jars. Call two on hello, world and then jars.from. This should be the exact same hello, world. Let's console.log that. There we are. We have hello, world. Now, we can get rid of this from just to see that it did actually split on the every character here.
Why is this useful? Let's go ahead and implement a truncate. We'll take a string here. Now, we can turn our string into a list of characters with jars two. We have array methods at our disposal, we can say slice zero three. Then, we'll turn it right back to a string. We'll go ahead and concat this dot dot dot at the end.
This is rather useful to be able to convert between types and back and use the functionality on one type and know that it holds the same information so we can convert back to our type.
Let's go ahead and run truncate now on hello, world. There we go. We get our H-E-L dot dot dot. We can do all sorts of things here that work on lists of characters.
It's important to point out that strings are not isomorphic to an array of anything. They have to be holding characters. We are restricted to staying within arrays of characters here.
Let's make another isomorphism here. I claim that a singleton array, an array holding one value A, is isomorphic to our type either, null, or A. Let's go ahead an try to make this isomorphism. We'll make our isotype. What would our two look like? We'll take our either, and we'll turn it into this array here.
I'm saying you can go to and from either one. We're going to start with the two. We'll just fold out our either. The left case will return an empty array. The right case, we will put it into a singleton array.
Now, if we get our array on the other side to turn it into an either, we have the array holding one thing. We can actually deconstruct it right here in the function. We'll just say if you are there, we'll pop it in the right. Otherwise, we'll get a left out. Let's go ahead and call this singleton.
We have our function here. Let's go ahead and make a way to filter eithers. We're going to call filter either. This will take some either and some predicate and go ahead and first turn our either into an array so we can actually filter it. We'll say two on either. Then, we'll just filter it with the predicate. Then, we'll turn it right back afterwards from.
Now, we can say if I have some rights holding hello and I want to filter it and say let me make sure this right is matching the letter H, what we can do here is say, alright, if this predicate held, then, we can continue to map along and call two uppercase on x. But if it didn't pass the predicate, we'll have a left and this will never be called. Let's go ahead and call this our result. Now, we can filter our eithers.
Let's give this a shot. Then we have a right of hello here, but if we didn't match the H, we get a left of undefined. It never tried to uppercase this. We have the ability to filter an either just by converting to the singleton array and back.