Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add createIO.

  • Loading branch information...
commit ba4036d5a02ba2e88d599cce9fa59ff81a297a45 1 parent 765adc9
@bos authored
Showing with 14 additions and 3 deletions.
  1. +13 −2 Statistics/Function.hs
  2. +1 −1  statistics.cabal
View
15 Statistics/Function.hs
@@ -15,11 +15,13 @@ module Statistics.Function
minMax
, sort
, partialSort
+ -- * Array setup
, createU
+ , createIO
) where
import Control.Exception (assert)
-import Control.Monad.ST (ST)
+import Control.Monad.ST (ST, unsafeIOToST, unsafeSTToIO)
import Data.Array.Vector.Algorithms.Combinators (apply)
import Data.Array.Vector
import qualified Data.Array.Vector.Algorithms.Intro as I
@@ -48,7 +50,8 @@ minMax = fini . foldlU go (MM (1/0) (-1/0))
fini (MM lo hi) = lo :*: hi
{-# INLINE minMax #-}
--- | Create an array, using the given action to populate each element.
+-- | Create an array, using the given 'ST' action to populate each
+-- element.
createU :: (UA e) => forall s. Int -> (Int -> ST s e) -> ST s (UArr e)
createU size itemAt = assert (size >= 0) $
newMU size >>= loop 0
@@ -58,3 +61,11 @@ createU size itemAt = assert (size >= 0) $
r <- itemAt k
writeMU arr k r
loop (k+1) arr
+{-# INLINE createU #-}
+
+-- | Create an array, using the given 'IO' action to populate each
+-- element.
+createIO :: (UA e) => Int -> (Int -> IO e) -> IO (UArr e)
+createIO size itemAt =
+ unsafeSTToIO $ createU size (unsafeIOToST . itemAt)
+{-# INLINE createIO #-}
View
2  statistics.cabal
@@ -1,5 +1,5 @@
name: statistics
-version: 0.3.1
+version: 0.3.2
synopsis: A library of statistical types, data, and functions
description:
This library provides a number of common functions and types useful
Please sign in to comment.
Something went wrong with that request. Please try again.