Skip to content

Commit

Permalink
Add Data.List.NonEmpty.ApplyMerge
Browse files Browse the repository at this point in the history
  • Loading branch information
pgujjula committed May 9, 2024
1 parent bc069ef commit 1904869
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
3 changes: 3 additions & 0 deletions apply-merge.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ source-repository head
library
exposed-modules:
Data.List.ApplyMerge
Data.List.NonEmpty.ApplyMerge
other-modules:
ApplyMerge.IntSet
hs-source-dirs:
Expand All @@ -55,6 +56,7 @@ test-suite apply-merge-tests
ApplyMerge.IntSet
Data.DoublyLinkedList.STRef
Data.List.ApplyMerge
Data.List.NonEmpty.ApplyMerge
Data.PQueue.Prio.Min.Mutable
Test.ApplyMerge.Common
Test.ApplyMerge.DoublyLinkedList
Expand Down Expand Up @@ -88,6 +90,7 @@ benchmark apply-merge-benchmarks
ApplyMerge.IntSet
Data.DoublyLinkedList.STRef
Data.List.ApplyMerge
Data.List.NonEmpty.ApplyMerge
Data.PQueue.Prio.Min.Mutable
Bench.Data.DoublyLinkedList.STRef
Bench.PriorityQueue.MinPQueue
Expand Down
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ library:
source-dirs: src
exposed-modules:
- Data.List.ApplyMerge
- Data.List.NonEmpty.ApplyMerge
other-modules:
- ApplyMerge.IntSet
dependencies:
Expand Down
2 changes: 1 addition & 1 deletion src/ApplyMerge/DoublyLinkedList.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE NoFieldSelectors #-}

module ApplyMerge.DoublyLinkedList (applyMerge) where
module ApplyMerge.DoublyLinkedList (applyMerge, applyMergeNonEmpty) where

import Control.Monad (guard, (>=>))
import Control.Monad.ST qualified as Strict
Expand Down
2 changes: 1 addition & 1 deletion src/ApplyMerge/IntMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE NoFieldSelectors #-}

module ApplyMerge.IntMap (applyMerge) where
module ApplyMerge.IntMap (applyMerge, applyMergeNonEmpty) where

import Control.Arrow ((>>>))
import Control.Monad (guard)
Expand Down
2 changes: 1 addition & 1 deletion src/ApplyMerge/IntSet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE NoFieldSelectors #-}

module ApplyMerge.IntSet (applyMerge) where
module ApplyMerge.IntSet (applyMerge, applyMergeNonEmpty) where

import Control.Arrow ((>>>))
import Control.Monad (guard)
Expand Down
29 changes: 29 additions & 0 deletions src/Data/List/NonEmpty/ApplyMerge.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- SPDX-FileCopyrightText: Copyright Preetham Gujjula
-- SPDX-License-Identifier: BSD-3-Clause

-- |
-- Module: Data.List.NonEmpty.ApplyMerge
-- License: BSD-3-Clause
-- Maintainer: Preetham Gujjula <[email protected]>
-- Stability: experimental
module Data.List.NonEmpty.ApplyMerge (applyMerge, applyMergeOn) where

import ApplyMerge.IntSet qualified
import Data.List.NonEmpty (NonEmpty)
import Data.List.NonEmpty qualified as NonEmpty
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 applies a custom projection function before
-- performing comparisons.
applyMergeOn ::
(Ord d) => (c -> d) -> (a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c
applyMergeOn p f as bs =
let f' a b =
let c = f a b
in Arg (p c) c
in NonEmpty.map (\(Arg _ c) -> c) (applyMerge f' as bs)

0 comments on commit 1904869

Please sign in to comment.