-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
104 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
-- SPDX-FileCopyrightText: Copyright Preetham Gujjula | ||
-- SPDX-License-Identifier: BSD-3-Clause | ||
{-# LANGUAGE UndecidableInstances #-} | ||
|
||
-- | | ||
-- Module: Data.List.ApplyMerge | ||
-- License: BSD-3-Clause | ||
-- Maintainer: Preetham Gujjula <[email protected]> | ||
-- Stability: experimental | ||
module Data.List.ApplyMerge (applyMerge, applyMergeOn) where | ||
module Data.List.ApplyMerge (applyMerge, applyMergeBy, applyMergeOn) where | ||
|
||
import ApplyMerge.IntSet qualified | ||
import Data.Proxy (Proxy (..)) | ||
import Data.Reflection (Reifies, reflect, reify) | ||
import Data.Semigroup (Arg (..)) | ||
|
||
-- | If given a binary function @f@ that is non-decreasing in both arguments, | ||
|
@@ -31,6 +34,35 @@ import Data.Semigroup (Arg (..)) | |
applyMerge :: (Ord c) => (a -> b -> c) -> [a] -> [b] -> [c] | ||
applyMerge = ApplyMerge.IntSet.applyMerge | ||
|
||
-- | Like 'applyMerge', but uses a custom comparison function. | ||
applyMergeBy :: (c -> c -> Ordering) -> (a -> b -> c) -> [a] -> [b] -> [c] | ||
applyMergeBy = applyMergeBy_ | ||
|
||
-- Reflection logic in applyMerge_ is based on "All about reflection: a | ||
-- tutorial" [1] by Arnaud Spiwack, licensed under CC BY 4.0 [2]. | ||
-- | ||
-- [1]: https://www.tweag.io/blog/2017-12-21-reflection-tutorial/ | ||
-- [2]: https://creativecommons.org/licenses/by/4.0/ | ||
applyMergeBy_ :: | ||
forall a b c. (c -> c -> Ordering) -> (a -> b -> c) -> [a] -> [b] -> [c] | ||
applyMergeBy_ cmp f as bs = | ||
reify cmp $ \(_ :: Proxy s) -> | ||
let f' :: a -> b -> ReflectedOrd s c | ||
f' a b = ReflectedOrd (f a b) | ||
in map unReflectedOrd (applyMerge f' as bs) | ||
|
||
newtype ReflectedOrd s a = ReflectedOrd {unReflectedOrd :: a} | ||
|
||
instance (Reifies s (a -> a -> Ordering)) => Eq (ReflectedOrd s a) where | ||
(==) (ReflectedOrd x) (ReflectedOrd y) = | ||
let cmp = reflect (Proxy :: Proxy s) | ||
in cmp x y == EQ | ||
|
||
instance (Reifies s (a -> a -> Ordering)) => Ord (ReflectedOrd s a) where | ||
compare (ReflectedOrd x) (ReflectedOrd y) = | ||
let cmp = reflect (Proxy :: Proxy s) | ||
in cmp x y | ||
|
||
-- | Like 'applyMerge', but applies a custom projection function before | ||
-- performing comparisons. | ||
-- | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,70 @@ | ||
-- SPDX-FileCopyrightText: Copyright Preetham Gujjula | ||
-- SPDX-License-Identifier: BSD-3-Clause | ||
{-# LANGUAGE UndecidableInstances #-} | ||
|
||
-- | | ||
-- Module: Data.List.NonEmpty.ApplyMerge | ||
-- License: BSD-3-Clause | ||
-- Maintainer: Preetham Gujjula <[email protected]> | ||
-- Stability: experimental | ||
module Data.List.NonEmpty.ApplyMerge (applyMerge, applyMergeOn) where | ||
module Data.List.NonEmpty.ApplyMerge | ||
( applyMerge, | ||
applyMergeBy, | ||
applyMergeOn, | ||
) | ||
where | ||
|
||
import ApplyMerge.IntSet qualified | ||
import Data.List.NonEmpty (NonEmpty) | ||
import Data.List.NonEmpty qualified as NonEmpty | ||
import Data.Proxy (Proxy (..)) | ||
import Data.Reflection (Reifies, reflect, reify) | ||
import Data.Semigroup (Arg (..)) | ||
|
||
-- | Like 'Data.List.ApplyMerge.applyMerge', but operates on 'NonEmpty's instead | ||
-- of lists. | ||
applyMerge :: (Ord c) => (a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c | ||
applyMerge = ApplyMerge.IntSet.applyMergeNonEmpty | ||
|
||
-- | Like 'applyMerge', but uses a custom comparison function. | ||
applyMergeBy :: | ||
(c -> c -> Ordering) -> | ||
(a -> b -> c) -> | ||
NonEmpty a -> | ||
NonEmpty b -> | ||
NonEmpty c | ||
applyMergeBy = applyMergeBy_ | ||
|
||
-- Reflection logic in applyMerge_ is based on "All about reflection: a | ||
-- tutorial" [1] by Arnaud Spiwack, licensed under CC BY 4.0 [2]. | ||
-- | ||
-- [1]: https://www.tweag.io/blog/2017-12-21-reflection-tutorial/ | ||
-- [2]: https://creativecommons.org/licenses/by/4.0/ | ||
applyMergeBy_ :: | ||
forall a b c. | ||
(c -> c -> Ordering) -> | ||
(a -> b -> c) -> | ||
NonEmpty a -> | ||
NonEmpty b -> | ||
NonEmpty c | ||
applyMergeBy_ cmp f as bs = | ||
reify cmp $ \(_ :: Proxy s) -> | ||
let f' :: a -> b -> ReflectedOrd s c | ||
f' a b = ReflectedOrd (f a b) | ||
in NonEmpty.map unReflectedOrd (applyMerge f' as bs) | ||
|
||
newtype ReflectedOrd s a = ReflectedOrd {unReflectedOrd :: a} | ||
|
||
instance (Reifies s (a -> a -> Ordering)) => Eq (ReflectedOrd s a) where | ||
(==) (ReflectedOrd x) (ReflectedOrd y) = | ||
let cmp = reflect (Proxy :: Proxy s) | ||
in cmp x y == EQ | ||
|
||
instance (Reifies s (a -> a -> Ordering)) => Ord (ReflectedOrd s a) where | ||
compare (ReflectedOrd x) (ReflectedOrd y) = | ||
let cmp = reflect (Proxy :: Proxy s) | ||
in cmp x y | ||
|
||
-- | Like 'applyMerge', but applies a custom projection function before | ||
-- performing comparisons. | ||
applyMergeOn :: | ||
|
This file contains 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