1- module Data.ArrayBuffer.Typed ( Writer ()
2- , asInt8Array
1+ module Data.ArrayBuffer.Typed ( asInt8Array
32 , asInt16Array
43 , asInt32Array
54 , asUint8Array
@@ -17,18 +16,16 @@ module Data.ArrayBuffer.Typed( Writer()
1716 , toIntArray
1817 ) where
1918
20- import Prelude (Unit , ($))
19+ import Control.Monad.Eff (Eff )
20+ import Data.ArrayBuffer.ArrayBuffer (ARRAYBUFFER )
2121import Data.ArrayBuffer.Types (ArrayView , ByteOffset , DataView , Float64Array , Float32Array , Uint8ClampedArray , Uint32Array , Uint16Array , Uint8Array , Int32Array , Int16Array , Int8Array )
2222import Data.Function.Uncurried (Fn2 , Fn3 , runFn2 , runFn3 )
2323import Data.Maybe (Maybe (..))
24- import Control.Monad.Eff (Eff )
25-
26- foreign import data Writer :: !
24+ import Prelude (Unit , ($), bind , pure )
2725
2826-- | Create typed int8 array viewing the buffer mapped by the `DataView`
2927foreign import asInt8Array :: DataView -> Int8Array
3028
31-
3229-- | Create typed int16 array viewing the buffer mapped by the `DataView`
3330foreign import asInt16Array :: DataView -> Int16Array
3431
@@ -54,36 +51,39 @@ foreign import asFloat32Array :: DataView -> Float32Array
5451foreign import asFloat64Array :: DataView -> Float64Array
5552
5653-- | Interpret typed array as a `DataView`.
57- foreign import dataView :: forall a . ArrayView a -> DataView
54+ foreign import dataView :: forall a e . ArrayView a -> Eff ( arrayBuffer :: ARRAYBUFFER | e ) DataView
5855
59- foreign import setImpl :: forall a e . Fn3 (ArrayView a ) ByteOffset (ArrayView a ) (Eff (writer :: Writer | e ) Unit )
56+ foreign import setImpl :: forall a e . Fn3 (ArrayView a ) ByteOffset (ArrayView a ) (Eff (arrayBuffer :: ARRAYBUFFER | e ) Unit )
6057
6158-- | Stores multiple values in the last typed array, reading input values from ther first typed array.
62- set :: forall a e . ArrayView a -> ByteOffset -> ArrayView a -> Eff (writer :: Writer | e ) Unit
59+ set :: forall a e . ArrayView a -> ByteOffset -> ArrayView a -> Eff (arrayBuffer :: ARRAYBUFFER | e ) Unit
6360set = runFn3 setImpl
6461
65- foreign import unsafeAtImpl :: forall a . Fn2 (ArrayView a ) Int Number
62+ foreign import unsafeAtImpl :: forall a e . Fn2 (ArrayView a ) Int ( Eff ( arrayBuffer :: ARRAYBUFFER | e ) Number )
6663
6764-- | Fetch element at index.
68- unsafeAt :: forall a . ArrayView a -> Int -> Number
65+ unsafeAt :: forall a e . ArrayView a -> Int -> Eff ( arrayBuffer :: ARRAYBUFFER | e ) Number
6966unsafeAt = runFn2 unsafeAtImpl
7067
71- foreign import hasIndexImpl :: forall a . Fn2 (ArrayView a ) Int Boolean
68+ foreign import hasIndexImpl :: forall a e . Fn2 (ArrayView a ) Int ( Eff ( arrayBuffer :: ARRAYBUFFER | e ) Boolean )
7269
7370-- | Determine if a certain index is valid.
74- hasIndex :: forall a . ArrayView a -> Int -> Boolean
71+ hasIndex :: forall a e . ArrayView a -> Int -> Eff ( arrayBuffer :: ARRAYBUFFER | e ) Boolean
7572hasIndex = runFn2 hasIndexImpl
7673
7774-- | Fetch element at index.
78- at :: forall a . ArrayView a -> Int -> Maybe Number
79- at a n = if a `hasIndex` n then
80- Just $ unsafeAt a n
81- else
82- Nothing
75+ at :: forall a e . ArrayView a -> Int -> Eff (arrayBuffer :: ARRAYBUFFER | e ) (Maybe Number )
76+ at a n = do
77+ indexExists <- a `hasIndex` n
78+ if indexExists
79+ then do
80+ element <- unsafeAt a n
81+ pure $ Just element
82+ else
83+ pure Nothing
8384
8485-- | Turn typed array into an array.
85- foreign import toArray :: forall a . ArrayView a -> Array Number
86+ foreign import toArray :: forall a e . ArrayView a -> Eff ( arrayBuffer :: ARRAYBUFFER | e ) ( Array Number )
8687
8788-- | Turn typed array into integer array.
88- foreign import toIntArray :: forall a . ArrayView a -> Array Int
89-
89+ foreign import toIntArray :: forall a e . ArrayView a -> Eff (arrayBuffer :: ARRAYBUFFER | e ) (Array Int )
0 commit comments