Skip to content

Commit 52ba9e5

Browse files
committed
Add Data.Sequence.fromArray.
Sugested by David Feuer in #88. The implementation on GHC uses GHC.Arr module and is considerably faster than on non-GHC compilers.
1 parent 9df67f5 commit 52ba9e5

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

Data/Sequence.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ module Data.Sequence (
6262
(><), -- :: Seq a -> Seq a -> Seq a
6363
fromList, -- :: [a] -> Seq a
6464
fromFunction, -- :: Int -> (Int -> a) -> Seq a
65+
fromArray, -- :: Ix i => Array i a -> Seq a
6566
-- ** Repetition
6667
replicate, -- :: Int -> a -> Seq a
6768
replicateA, -- :: Applicative f => Int -> f a -> f (Seq a)
@@ -180,6 +181,13 @@ import Text.Read (Lexeme(Ident), lexP, parens, prec,
180181
import Data.Data
181182
#endif
182183

184+
-- Array stuff, with GHC.Arr on GHC
185+
import Data.Array (Ix, Array)
186+
import qualified Data.Array
187+
#ifdef __GLASGOW_HASKELL__
188+
import qualified GHC.Arr
189+
#endif
190+
183191
-- Coercion on GHC 7.8+
184192
#if __GLASGOW_HASKELL__ >= 708
185193
import Data.Coerce
@@ -1399,6 +1407,17 @@ fromFunction len f | len < 0 = error "Data.Sequence.fromFunction called with neg
13991407
#endif
14001408
{-# INLINE lift_elem #-}
14011409

1410+
-- | /O(n)/. Create a sequence consisting of the elements of an 'Array'.
1411+
-- Note that the resulting sequence elements may be evaluated lazily (as on GHC),
1412+
-- so you must force the entire structure to be sure that the original array
1413+
-- can be garbage-collected.
1414+
fromArray :: Ix i => Array i a -> Seq a
1415+
#ifdef __GLASGOW_HASKELL__
1416+
fromArray a = fromFunction (GHC.Arr.numElements a) (GHC.Arr.unsafeAt a)
1417+
#else
1418+
fromArray a = fromList2 (Data.Array.rangeSize (Data.Array.bounds a)) (Data.Array.elems a)
1419+
#endif
1420+
14021421
-- Splitting
14031422

14041423
-- | /O(log(min(i,n-i)))/. The first @i@ elements of a sequence.

tests/seq-properties.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Data.Sequence -- needs to be compiled with -DTESTING for use here
22

33
import Control.Applicative (Applicative(..))
44
import Control.Arrow ((***))
5+
import Data.Array (listArray)
56
import Data.Foldable (Foldable(..), toList, all, sum)
67
import Data.Functor ((<$>), (<$))
78
import Data.Maybe
@@ -37,6 +38,7 @@ main = defaultMain
3738
, testProperty "(><)" prop_append
3839
, testProperty "fromList" prop_fromList
3940
, testProperty "fromFunction" prop_fromFunction
41+
, testProperty "fromArray" prop_fromArray
4042
, testProperty "replicate" prop_replicate
4143
, testProperty "replicateA" prop_replicateA
4244
, testProperty "replicateM" prop_replicateM
@@ -275,6 +277,10 @@ prop_fromFunction :: [A] -> Bool
275277
prop_fromFunction xs =
276278
toList' (fromFunction (Prelude.length xs) (xs!!)) ~= xs
277279

280+
prop_fromArray :: [A] -> Bool
281+
prop_fromArray xs =
282+
toList' (fromArray (listArray (42, 42+Prelude.length xs-1) xs)) ~= xs
283+
278284
-- ** Repetition
279285

280286
prop_replicate :: NonNegative Int -> A -> Bool

0 commit comments

Comments
 (0)