Last active
September 4, 2023 13:52
-
-
Save qexat/050cc2e396b15250b66f6482d660a987 to your computer and use it in GitHub Desktop.
Elevated `foldr1`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Data.Foldable (foldr1) | |
| {- | |
| Fold a list of values into an elevated operator. | |
| This allows applying an operator on a list which values type do not | |
| support the operator, but can be casted to a type which does. | |
| The `toLifted` function casts every value of `values` to a type that `op` | |
| accepts, then `fromLifted` casts back the result to the `values` type. | |
| Elevated | -- op -- | |
| | / \ | |
| | ↑ toLifted ↑ ↓ fromLifted ↓ | |
| | / \ | |
| Normal | values -- -- (result) | |
| -} | |
| elevatedFoldr1 :: (a -> a -> a) -> (b -> a) -> (a -> b) -> [b] -> b | |
| elevatedFoldr1 op toLifted fromLifted values = | |
| fromLifted $ foldr1 op (map toLifted values) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
lift->toLiftedanddemote->fromLiftedelevatedCall->elevatedFoldrThanks for the suggestions @jake-87