-
Notifications
You must be signed in to change notification settings - Fork 60
Heterogeneous array creation #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
treeowl
wants to merge
7
commits into
haskell:master
Choose a base branch
from
treeowl:gencreate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d01fdd2
Heterogeneous array creation
treeowl 26ce85b
Add more bells and whistles
treeowl e8685d6
Expand, reduce
treeowl 8e71351
More changes
treeowl fe9fef0
Rename
treeowl fd2f964
Add necessary kind signature
treeowl f783ec9
Update extensions in Cabal file; update changelog
treeowl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -7,6 +7,9 @@ | |
| {-# LANGUAGE DeriveDataTypeable #-} | ||
| {-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
| {-# LANGUAGE BangPatterns #-} | ||
| #if __GLASGOW_HASKELL__ >= 708 | ||
|
||
| {-# LANGUAGE PolyKinds #-} | ||
| #endif | ||
|
|
||
| -- | | ||
| -- Module : Data.Primitive.SmallArray | ||
|
|
@@ -52,6 +55,9 @@ module Data.Primitive.SmallArray | |
| , unsafeFreezeSmallArray | ||
| , thawSmallArray | ||
| , runSmallArray | ||
| , runSmallArrays | ||
| , runSmallArraysHetOf | ||
| , runSmallArraysHetOfThen | ||
| , unsafeThawSmallArray | ||
| , sizeofSmallArray | ||
| , sizeofSmallMutableArray | ||
|
|
@@ -102,6 +108,7 @@ import qualified Data.Primitive.Array as Array | |
| #if MIN_VERSION_base(4,9,0) || MIN_VERSION_transformers(0,4,0) | ||
| import Data.Functor.Classes (Eq1(..),Ord1(..),Show1(..),Read1(..)) | ||
| #endif | ||
| import Data.Functor.Compose | ||
|
|
||
| #if HAVE_SMALL_ARRAY | ||
| data SmallArray a = SmallArray (SmallArray# a) | ||
|
|
@@ -940,3 +947,74 @@ smallArrayFromListN n l = SmallArray (Array.fromListN n l) | |
| -- | Create a 'SmallArray' from a list. | ||
| smallArrayFromList :: [a] -> SmallArray a | ||
| smallArrayFromList l = smallArrayFromListN (length l) l | ||
|
|
||
| -- | Create any number of arrays of the same type within an arbitrary | ||
| -- 'Traversable' context. This will often be useful with traversables | ||
| -- like @(c,)@, @'Either' e@, @'Compose' (c,) ('Either' e)@, and | ||
| -- @'Compose' ('Either' e) (c,)@. For a more general version, see | ||
| -- 'runArraysHetOf'. | ||
| runSmallArrays | ||
| :: Traversable t | ||
| => (forall s. ST s (t (SmallMutableArray s a))) | ||
| -> t (SmallArray a) | ||
| runSmallArrays m = runST $ m >>= traverse unsafeFreezeSmallArray | ||
|
|
||
| -- | Create arbitrarily many arrays that may have different types. For | ||
| -- a simpler but less general version, see 'runArrays'. | ||
|
||
| -- | ||
| -- === __Examples__ | ||
| -- | ||
| -- ==== @'runSmallArrays'@ | ||
| -- | ||
| -- @ | ||
| -- newtype Ha t a v = Ha {unHa :: t (v a)} | ||
| -- runSmallArrays m = unHa $ runSmallArraysHetOf (\f (Ha t) -> Ha <$> traverse f t) (Ha <$> m) | ||
| -- @ | ||
| -- | ||
| -- ==== @unzipSmallArray@ | ||
| -- | ||
| -- @ | ||
| -- unzipSmallArray :: Array (a, b) -> (Array a, Array b) | ||
| -- unzipSmallArray ar = | ||
| -- unPair $ runSmallArraysHetOf traversePair $ do | ||
| -- xs <- newSmallArray sz undefined | ||
| -- ys <- newSmallArray sz undefined | ||
| -- let go k | ||
| -- | k == sz = pure (Pair (xs, ys)) | ||
| -- | otherwise = do | ||
| -- (x,y) <- indexSmallArrayM ar k | ||
| -- writeSmallArray xs k x | ||
| -- writeSmallArray ys k y | ||
| -- go (k + 1) | ||
| -- go 0 | ||
| -- where sz = sizeofSmallArray ar | ||
| -- | ||
| -- data Pair ab v where | ||
| -- Pair :: {unPair :: (v a, v b)} -> Pair (a,b) v | ||
| -- | ||
| -- traversePair :: Applicative h => (forall x. f x -> h (g x)) -> Pair ab f -> h (Pair ab g) | ||
| -- traversePair f (Pair (xs, ys)) = liftA2 (\x y -> Pair (x,y)) (f xs) (f ys) | ||
| -- @ | ||
| runSmallArraysHetOf | ||
| :: (forall h f g. | ||
| (Applicative h => (forall x. f x -> h (g x)) -> t f -> h (u g))) | ||
| -- ^ A rank-2 traversal | ||
| -> (forall s. ST s (t (SmallMutableArray s))) | ||
| -- ^ An 'ST' action producing a rank-2 container of 'MutableArray's. | ||
| -> u SmallArray | ||
| runSmallArraysHetOf f m = runST $ m >>= f unsafeFreezeSmallArray | ||
|
|
||
| -- | Similar to 'runSmallArraysHetOf', but takes a function to traverse over the | ||
| -- generated 'SmallArray's as it freezes them. | ||
| runSmallArraysHetOfThen | ||
| :: Applicative q | ||
| => (forall h f g. | ||
| (Applicative h => (forall x. f x -> h (g x)) -> t f -> h (u g))) | ||
| -- ^ A rank-2 traversal | ||
| -> (forall x. SmallArray x -> q (r x)) | ||
| -- ^ A function to traverse over a container of 'Array's | ||
| -> (forall s. ST s (t (SmallMutableArray s))) | ||
| -- ^ An 'ST' action producing a rank-2 container of 'MutableArray's. | ||
| -> q (u r) | ||
| runSmallArraysHetOfThen trav post m = | ||
| runST $ m >>= getCompose . trav (Compose . fmap post . unsafeFreezeSmallArray) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be 706? (That's the first GHC version that reliably supported
PolyKinds.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mistakenly though it didn't stabilize till later. Fixed!