Skip to content

Instantly share code, notes, and snippets.

@qexat
Last active September 4, 2023 13:52
Show Gist options
  • Select an option

  • Save qexat/050cc2e396b15250b66f6482d660a987 to your computer and use it in GitHub Desktop.

Select an option

Save qexat/050cc2e396b15250b66f6482d660a987 to your computer and use it in GitHub Desktop.
Elevated `foldr1`
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)
@qexat

qexat commented Sep 4, 2023

Copy link
Copy Markdown
Author

lift -> toLifted and demote -> fromLifted
elevatedCall -> elevatedFoldr

Thanks for the suggestions @jake-87

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment