by zero_shift · 1y ago · 0 comments
We ideally want an infix function that can reduce the "truthiness" of two values.
Let us imagine this language is a Haskell-type-thing, and we can define pseudo-operators with pattern matching
infixr 3 &&
(&&) :: Optional -> Optional -> Optional
Empty && _ = Empty
_ && Empty = Empty
Some A && Some B = Some A
_ && _ = Empty
infixr 2 ||
Some || _ = Some
None || Some = Some
None || None = None
_ || _ = None
Hmm, let's see how that looks a = b && c
d = e || f
The good news is that we are free from the tyranny of booleans. The bad news is that we just reinvented JavaScript :-)The first one has some arbitrariness (do you take the left or right value if both are Just). But, thankfully the Applicative typeclass gives both <* and *>, which lets you choose which value you want:
Just A <* Just B = Just A
Just A *> Just B = Just B
(There's the possibility to merge values too, with f <$> Just A <*> Just B, which evaluates to Just (f A B). I feel like this is a "don't try to understand it, just get used to it" sort of syntax. It can be pretty convenient though.)There's a whole lot more to JavaScript typing that makes it JavaScript.
After all, Python does this too (but the spellings are "and" and "or").
Good Job! Now do the "no scalar values" challenge. FP fanbois gonna love the unwrapping.