11-- Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
22-- SPDX-License-Identifier: Apache-2.0
33
4+ {-# LANGUAGE PatternSynonyms #-}
45
56-- | Type and functions for non-empty lists. This module re-exports many functions with
67-- the same name as prelude list functions, so it is expected to import the module qualified.
1314-- ```
1415module DA.NonEmpty
1516 ( NonEmpty (.. )
17+ , pattern (:|)
18+ , (<|)
1619 , cons
1720 , append
1821 , map
@@ -40,10 +43,20 @@ import DA.NonEmpty.Types
4043import DA.Traversable qualified as T
4144import DA.List qualified as L
4245import DA.Action qualified as M
46+ import DA.Internal.Record qualified
47+
48+ infixr 5 :|
49+ infixr 5 <|
50+
51+ -- | Pattern synonym for constructing and matching non-empty lists.
52+ pattern (:|) : a -> [a] -> NonEmpty a
53+ pattern (:|) x xs = NonEmpty x xs
54+ {-# COMPLETE (:|) : NonEmpty #-}
4355
4456deriving instance Eq a => Eq (NonEmpty a )
45- deriving instance Show a => Show (NonEmpty a)
4657deriving instance Ord a => Ord (NonEmpty a )
58+ instance Show a => Show (NonEmpty a ) where
59+ show (x :| xs) = show x <> " :| " <> show xs
4760
4861-- NonEmpty is defined in a stable LF package so we need to handwrite the {Get,Set}Field instances here.
4962
@@ -76,22 +89,26 @@ instance Action NonEmpty where
7689 ys' = xs >>= toList . f
7790
7891instance F. Foldable NonEmpty where
79- foldr f z ne = f ne.hd (P.foldr f z ne.tl )
92+ foldr f z (h :| t) = f h (P. foldr f z t )
8093
8194instance T. Traversable NonEmpty where
82- mapA f l = liftA2 NonEmpty (f l.hd ) (T.mapA f l.tl )
95+ mapA f (h :| t) = liftA2 (:|) (f h ) (T. mapA f t )
8396
8497-- | Prepend an element to a non-empty list.
8598cons : a -> NonEmpty a -> NonEmpty a
86- cons a ne = NonEmpty a (ne.hd :: ne.tl)
99+ cons a (h :| t) = a :| h :: t
100+
101+ -- | Alias for `cons`.
102+ (<|) : a -> NonEmpty a -> NonEmpty a
103+ (<|) = cons
87104
88105-- | Append or concatenate two non-empty lists.
89106append : NonEmpty a -> NonEmpty a -> NonEmpty a
90- append l r = NonEmpty l.hd (l.tl ++ toList r)
107+ append (h :| t) r = h :| t ++ toList r
91108
92109-- | Apply a function over each element in the non-empty list.
93110map : (a -> b) -> NonEmpty a -> NonEmpty b
94- map f ne = NonEmpty (f ne.hd) ( P.map f ne.tl)
111+ map f (h :| t) = f h :| P. map f t
95112
96113-- | Turn a list into a non-empty list, if possible. Returns
97114-- `None` if the input list is empty, and `Some` otherwise.
@@ -130,7 +147,7 @@ delete = deleteBy (==)
130147-- | Apply a function repeatedly to pairs of elements from a non-empty list,
131148-- from the left. For example, `foldl1 (+) (NonEmpty 1 [2,3,4]) = ((1 + 2) + 3) + 4`.
132149foldl1 : (a -> a -> a) -> NonEmpty a -> a
133- foldl1 f l = L.foldl f l.hd l.tl
150+ foldl1 f (h :| t) = L. foldl f h t
134151
135152-- | Apply a function repeatedly to pairs of elements from a non-empty list,
136153-- from the right. For example, `foldr1 (+) (NonEmpty 1 [2,3,4]) = 1 + (2 + (3 + 4))`.
@@ -149,7 +166,7 @@ foldrA f x xs = foldr (\ y acc -> do v <- acc; f y v) (pure x) xs
149166
150167-- | The same as `foldr1` but running an action each time.
151168foldr1A : Action m => (a -> a -> m a) -> NonEmpty a -> m a
152- foldr1A f l = M.foldrA f l.hd l.tl
169+ foldr1A f (h :| t) = M. foldrA f h t
153170
154171-- | Apply a function repeatedly to pairs of elements from a non-empty list,
155172-- from the left, with a given initial value. For example,
@@ -163,7 +180,7 @@ foldlA f x xs = foldl (\ acc y -> do v <- acc; f v y) (pure x) xs
163180
164181-- | The same as `foldl1` but running an action each time.
165182foldl1A : Action m => (a -> a -> m a) -> NonEmpty a -> m a
166- foldl1A f l = M.foldlA f l.hd l.tl
183+ foldl1A f (h :| t) = M. foldlA f h t
167184
168185instance IsParties (NonEmpty Party ) where
169186 toParties = toList
0 commit comments