| Copyright | (C) Frank Staals |
|---|---|
| License | see the LICENSE file |
| Maintainer | Frank Staals |
| Safe Haskell | None |
| Language | GHC2021 |
HGeometry.Sequence.Alternating
Description
A Type representing Alternating sequences. The sequence type itself is parameterized.
Synopsis
- data Alternating (f :: Type -> Type) sep a = Alternating a (f (sep, a))
- fromNonEmptyWith :: forall (g :: Type -> Type) f sep a. (HasFromFoldable g, Foldable1 f) => sep -> f a -> Alternating g sep a
- mapF :: (f (sep, a) -> g (sep', a)) -> Alternating f sep a -> Alternating g sep' a
- firstWithNeighbors :: forall (f :: Type -> Type) a sep sep'. Traversable f => (a -> sep -> a -> sep') -> Alternating f sep a -> Alternating f sep' a
- withNeighbours :: forall (f :: Type -> Type) sep a. Foldable f => Alternating f sep a -> [(a, sep, a)]
- mergeAlternating :: Ord t => (t -> a -> b -> c) -> Alternating [] t a -> Alternating [] t b -> [(t, c)]
- insertBreakPoints :: Ord t => [t] -> Alternating [] t a -> Alternating [] t a
- reverse :: Alternating [] b a -> Alternating [] b a
- consElemWith :: forall (f :: Type -> Type) sep a. Cons (f (sep, a)) (f (sep, a)) (sep, a) (sep, a) => (a -> a -> sep) -> a -> Alternating f sep a -> Alternating f sep a
- unconsAlt :: forall (f :: Type -> Type) sep a. Cons (f (sep, a)) (f (sep, a)) (sep, a) (sep, a) => Alternating f sep a -> Either a ((a, sep), Alternating f sep a)
- snocElemWith :: forall (f :: Type -> Type) sep a. Snoc (f (sep, a)) (f (sep, a)) (sep, a) (sep, a) => (a -> a -> sep) -> Alternating f sep a -> a -> Alternating f sep a
- unsnocAlt :: forall (f :: Type -> Type) sep a. Snoc (f (sep, a)) (f (sep, a)) (sep, a) (sep, a) => Alternating f sep a -> Either a (Alternating f sep a, (sep, a))
- concatAlternatingsWith :: forall (f :: Type -> Type) sep a. (Cons (f (sep, a)) (f (sep, a)) (sep, a) (sep, a), Snoc (f (sep, a)) (f (sep, a)) (sep, a) (sep, a), Semigroup (f (sep, a))) => (a -> a -> sep) -> Alternating f sep a -> Alternating f sep a -> Alternating f sep a
- separators :: Functor f => Alternating f sep a -> f sep
Documentation
data Alternating (f :: Type -> Type) sep a Source #
A (non-empty) alternating sequence of a's and sep's.
Constructors
| Alternating a (f (sep, a)) |
Instances
fromNonEmptyWith :: forall (g :: Type -> Type) f sep a. (HasFromFoldable g, Foldable1 f) => sep -> f a -> Alternating g sep a Source #
Given a separator, and some foldable structure, constructs an Alternating.
mapF :: (f (sep, a) -> g (sep', a)) -> Alternating f sep a -> Alternating g sep' a Source #
map some function changing the f into a g.
firstWithNeighbors :: forall (f :: Type -> Type) a sep sep'. Traversable f => (a -> sep -> a -> sep') -> Alternating f sep a -> Alternating f sep' a Source #
Map over the separators of the alterating together with the neighbours
withNeighbours :: forall (f :: Type -> Type) sep a. Foldable f => Alternating f sep a -> [(a, sep, a)] Source #
Computes a b with all its neighbours
>>>withNeighbours (Alternating 0 [('a', 1), ('b', 2), ('c',3)])[(0,'a',1),(1,'b',2),(2,'c',3)]
mergeAlternating :: Ord t => (t -> a -> b -> c) -> Alternating [] t a -> Alternating [] t b -> [(t, c)] Source #
Generic merging scheme that merges two Alternating Lists and applies the function
'f', with the current/new value at every event. So note that if the alternating
consists of 'Alternating a0 [(t1,a2)]' then the function is applied to a1, not to a0
(i.e. every value ai is considered alive on the interval [ti,t(i+1))
>>>let odds = Alternating "a" [(3,"c"), (5,"e"), (7,"g")]>>>let evens = Alternating "b" [(4,"d"), (6,"f"), (8,"h")]>>>mergeAlternating (\_ a b -> a <> b) odds evens[(3,"cb"),(4,"cd"),(5,"ed"),(6,"ef"),(7,"gf"),(8,"gh")]>>>mergeAlternating (\t a b -> if t `mod` 2 == 0 then a else b) odds evens[(3,"b"),(4,"c"),(5,"d"),(6,"e"),(7,"f"),(8,"g")]>>>mergeAlternating (\_ a b -> a <> b) odds (Alternating "b" [(0,"d"), (5,"e"), (8,"h")])[(0,"ad"),(3,"cd"),(5,"ee"),(7,"ge"),(8,"gh")]
insertBreakPoints :: Ord t => [t] -> Alternating [] t a -> Alternating [] t a Source #
Adds additional t-values in the alternating, (in sorted order). I.e. if we insert a
"breakpoint" at time t the current 'a' value is used at that time.
>>>insertBreakPoints [0,2,4,6,8,10] $ Alternating "a" [(3, "c"), (5, "e"), (7,"g")]Alternating "a" [(0,"a"),(2,"a"),(3,"c"),(4,"c"),(5,"e"),(6,"e"),(7,"g"),(8,"g"),(10,"g")]
reverse :: Alternating [] b a -> Alternating [] b a Source #
Reverses an alternating list.
>>>reverse $ Alternating "a" [(3, "c"), (5, "e"), (7, "g")]Alternating "g" [(7,"e"),(5,"c"),(3,"a")]
consElemWith :: forall (f :: Type -> Type) sep a. Cons (f (sep, a)) (f (sep, a)) (sep, a) (sep, a) => (a -> a -> sep) -> a -> Alternating f sep a -> Alternating f sep a Source #
Given a function f that takes the new element y and the (current) first element x and computes the new separating element s, conses y and the the separator onto alternating list.
>>>consElemWith (\_ _ -> ".") 0 (fromNonEmptyWith @[] "," (NonEmpty.fromList [1..5]))Alternating 0 [(".",1),(",",2),(",",3),(",",4),(",",5)]>>>consElemWith (\_ _ -> ".") 0 (fromNonEmptyWith @[] "," (NonEmpty.fromList [1]))Alternating 0 [(".",1)]
unconsAlt :: forall (f :: Type -> Type) sep a. Cons (f (sep, a)) (f (sep, a)) (sep, a) (sep, a) => Alternating f sep a -> Either a ((a, sep), Alternating f sep a) Source #
Uncons the Alternating, getting either just the first element (if there was only one), or the first element, the first separator, and the remaining alternating.
snocElemWith :: forall (f :: Type -> Type) sep a. Snoc (f (sep, a)) (f (sep, a)) (sep, a) (sep, a) => (a -> a -> sep) -> Alternating f sep a -> a -> Alternating f sep a Source #
Given a function f that takes the (current) last element x, and the new element y, and computes the new separating element s, snocs the separator and y onto the alternating list.
>>>snocElemWith (\_ _ -> ".") (fromNonEmptyWith @[] "," (NonEmpty.fromList [1..5])) 6Alternating 1 [(",",2),(",",3),(",",4),(",",5),(".",6)]>>>snocElemWith (\_ _ -> ".") (fromNonEmptyWith @[] "," (NonEmpty.fromList [1])) 6Alternating 1 [(".",6)]
unsnocAlt :: forall (f :: Type -> Type) sep a. Snoc (f (sep, a)) (f (sep, a)) (sep, a) (sep, a) => Alternating f sep a -> Either a (Alternating f sep a, (sep, a)) Source #
Unsnoc the alternating, getting either just the last element (if there was only one), or the "init" of the alternating, the last separator, and the last element the init would be the alternating without its last element.
concatAlternatingsWith Source #
Arguments
| :: forall (f :: Type -> Type) sep a. (Cons (f (sep, a)) (f (sep, a)) (sep, a) (sep, a), Snoc (f (sep, a)) (f (sep, a)) (sep, a) (sep, a), Semigroup (f (sep, a))) | |
| => (a -> a -> sep) | Function that combines the last element from the first alternating with the first of the second to compute the new separator. |
| -> Alternating f sep a | |
| -> Alternating f sep a | |
| -> Alternating f sep a |
Given a function that computes the new separator, combinges the two alternatings.
>>>let alt1 = fromNonEmptyWith "," (NonEmpty.fromList [1..2]) :: Alternating [] String Int>>>let alt2 = fromNonEmptyWith "," (NonEmpty.fromList [3..4])>>>concatAlternatingsWith (\x y -> "new sep " <> show x <> show y) alt1 alt2Alternating 1 [(",",2),("new sep 23",3),(",",4)]
separators :: Functor f => Alternating f sep a -> f sep Source #
Get the separators out of the alternating