-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from purescript/st
Move STArray functions into Data.Array.ST
- Loading branch information
Showing
4 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
module Data.Array.ST | ||
( STArray(..) | ||
, runSTArray | ||
, emptySTArray | ||
, peekSTArray | ||
, pokeSTArray | ||
, pushSTArray | ||
) where | ||
|
||
import Data.Maybe | ||
import Data.Function | ||
|
||
import Control.Monad.Eff | ||
import Control.Monad.ST (ST()) | ||
|
||
foreign import data STArray :: * -> * -> * | ||
|
||
foreign import runSTArray """ | ||
function runSTArray(f) { | ||
return f; | ||
}""" :: forall a r. (forall h. Eff (st :: ST h | r) (STArray h a)) -> Eff r [a] | ||
|
||
foreign import emptySTArray """ | ||
function emptySTArray() { | ||
return []; | ||
}""" :: forall a h r. Eff (st :: ST h | r) (STArray h a) | ||
|
||
foreign import peekSTArrayImpl """ | ||
function peekSTArrayImpl(arr, i, s, f) { | ||
return function() { | ||
var index = ~~i; | ||
if (0 <= index && index < arr.length) { | ||
return s(arr[index]); | ||
} else { | ||
return f; | ||
} | ||
}; | ||
}""" :: forall a h e r. Fn4 (STArray h a) | ||
Number | ||
(a -> r) | ||
r | ||
(Eff (st :: ST h | e) r) | ||
|
||
peekSTArray :: forall a h r. STArray h a -> Number -> Eff (st :: ST h | r) (Maybe a) | ||
peekSTArray arr i = runFn4 peekSTArrayImpl arr i Just Nothing | ||
|
||
foreign import pokeSTArrayImpl """ | ||
function pokeSTArrayImpl(arr, i, a) { | ||
return function() { | ||
var index = ~~i; | ||
if (0 <= index && index <= arr.length) { | ||
arr[index] = a; | ||
return true; | ||
} | ||
return false; | ||
}; | ||
}""" :: forall a h e. Fn3 (STArray h a) | ||
Number | ||
a | ||
(Eff (st :: ST h | e) Boolean) | ||
|
||
pokeSTArray :: forall a h r. STArray h a -> Number -> a -> Eff (st :: ST h | r) Boolean | ||
pokeSTArray arr i a = runFn3 pokeSTArrayImpl arr i a | ||
|
||
foreign import pushSTArrayImpl """ | ||
function pushSTArrayImpl(arr, a) { | ||
return function() { | ||
arr.push(a); | ||
return {}; | ||
}; | ||
}""" :: forall a h e. Fn2 (STArray h a) | ||
a | ||
(Eff (st :: ST h | e) Unit) | ||
|
||
pushSTArray :: forall a h r. STArray h a -> a -> Eff (st :: ST h | r) Unit | ||
pushSTArray arr a = runFn2 pushSTArrayImpl arr a |
0e73b8d
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.
This is a breaking change.
Now, for users of the old prelude and of this version of purescript-arrays, it conflicts.
0e73b8d
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've bumped the minor version on
arrays
so this should no longer be an issue.