Skip to content

Loading…

Add & export mapCollectionWithKey #7

Merged
merged 3 commits into from

3 participants

@jaspervdj

No description provided.

@travisbot

This pull request fails (merged 1d51686 into 6c8750e).

@travisbot

This pull request fails (merged d331629 into 6c8750e).

@maoe maoe merged commit 1810d97 into tsurucapital:master

1 check was pending

Details default The Travis build is in progress
@travisbot

This pull request fails (merged b3bcee7 into 6c8750e).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 25 additions and 6 deletions.
  1. +15 −5 FRP/Euphoria/Collection.hs
  2. +1 −1 euphoria.cabal
  3. +9 −0 tests/Test/Collection.hs
View
20 FRP/Euphoria/Collection.hs
@@ -22,6 +22,7 @@ module FRP.Euphoria.Collection
, openCollection
-- * other functions
, mapCollection
+, mapCollectionWithKey
, justCollection
, sequenceCollection
) where
@@ -97,11 +98,20 @@ instance SignalSet (Collection k a) where
-- | Like 'fmap', but the Collection and interior 'Event' stream are memoized
mapCollection :: (a -> b) -> Collection k a -> SignalGen (Collection k b)
-mapCollection f aC = do
- updateE <- snd <$> openCollection aC
- newCurD <- memoD $ fmap ((fmap . fmap) f . fst) $ unCollection aC
- newUpdateE <- memoE $ (fmap . fmap) f updateE
- makeCollection newCurD newUpdateE
+mapCollection = mapCollectionWithKey . const
+
+-- | A version of 'mapCollection' which provides access to the key
+mapCollectionWithKey :: (k -> a -> b) -> Collection k a -> SignalGen (Collection k b)
+mapCollectionWithKey f aC = do
+ updateE <- snd <$> openCollection aC
+ newCurD <- memoD $ fmap (fmap ft . fst) $ unCollection aC
+ newUpdateE <- memoE $ fmap fcu updateE
+ makeCollection newCurD newUpdateE
+ where
+ -- f applied to tuples and collection updates
+ ft (k, x) = (k, f k x)
+ fcu (AddItem k x) = AddItem k (f k x)
+ fcu (RemoveItem k) = RemoveItem k
justCollection :: forall k a. (Enum k) => Collection k (Maybe a) -> SignalGen (Collection k a)
-- Inefficient, quick-hack implementation
View
2 euphoria.cabal
@@ -2,7 +2,7 @@
-- see http://haskell.org/cabal/users-guide/
name: euphoria
-version: 0.4.4.0
+version: 0.4.5.0
synopsis: Dynamic network FRP with events and continuous values
description:
View
9 tests/Test/Collection.hs
@@ -38,3 +38,12 @@ case_switchCollection = do
]
where
mkD list = signalToDiscrete <$> signalFromList list
+
+case_mapCollection :: Assertion
+case_mapCollection = do
+ result <- networkToList 1 $ do
+ col <- mapCollection show $
+ collectionFromList [(0 :: Int, 1 :: Int), (1, 2), (2, 3)]
+ discreteToSignal $ collectionToDiscreteList col
+
+ result @?= [[(0, "1"), (1, "2"), (2, "3")]]
Something went wrong with that request. Please try again.