Skip to content

Commit

Permalink
Merge pull request #18 from purescript/st
Browse files Browse the repository at this point in the history
Move STArray functions into Data.Array.ST
  • Loading branch information
paf31 committed Nov 27, 2014
2 parents cba046d + 8e3ac24 commit 0e73b8d
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function(grunt) {

pscMake: ["<%=libFiles%>"],
dotPsci: ["<%=libFiles%>"],
docgen: {
pscDocs: {
readme: {
src: "src/**/*.purs",
dest: "README.md"
Expand All @@ -25,6 +25,6 @@ module.exports = function(grunt) {
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.loadNpmTasks("grunt-purescript");

grunt.registerTask("make", ["pscMake", "dotPsci", "docgen"]);
grunt.registerTask("make", ["pscMake", "dotPsci", "pscDocs"]);
grunt.registerTask("default", ["make"]);
};
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,26 @@
zipWith :: forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]


## Module Data.Array.ST

### Types

data STArray :: * -> * -> *


### Values

emptySTArray :: forall a h r. Eff (st :: ST h | r) (STArray h a)

peekSTArray :: forall a h r. STArray h a -> Number -> Eff (st :: ST h | r) (Maybe a)

pokeSTArray :: forall a h r. STArray h a -> Number -> a -> Eff (st :: ST h | r) Boolean

pushSTArray :: forall a h r. STArray h a -> a -> Eff (st :: ST h | r) Unit

runSTArray :: forall a r. (forall h. Eff (st :: ST h | r) (STArray h a)) -> Eff r [a]


## Module Data.Array.Unsafe

### Values
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"dependencies": {
"grunt": "~0.4.4",
"grunt-purescript": "~0.5.1",
"grunt-contrib-clean": "~0.5.0"
"grunt-contrib-clean": "~0.6.0"
}
}
76 changes: 76 additions & 0 deletions src/Data/Array/ST.purs
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

2 comments on commit 0e73b8d

@NightRa
Copy link

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.

@paf31
Copy link
Contributor Author

@paf31 paf31 commented on 0e73b8d Nov 28, 2014

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.

Please sign in to comment.