Skip to content

Commit

Permalink
Fix #1, example
Browse files Browse the repository at this point in the history
  • Loading branch information
paf31 committed Aug 27, 2014
1 parent 813b494 commit 0b4d1ce
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/output/
/node_modules/
/bower_components/
/tmp/
/dist/
13 changes: 12 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,23 @@ module.exports = function(grunt) {
}
},

psc: {
options: {
modules: ["Main"],
main: "Main"
},
example: {
src: ["<%=libFiles%>", "examples/*.purs"],
dest: "dist/Main.js"
}
},

dotPsci: ["<%=libFiles%>"]
});

grunt.loadNpmTasks("grunt-contrib-clean");
grunt.loadNpmTasks("grunt-purescript");

grunt.registerTask("make", ["pscMake:lib", "dotPsci"]);
grunt.registerTask("make", ["pscMake:lib", "dotPsci", "psc:example"]);
grunt.registerTask("default", ["clean", "make"]);
};
3 changes: 0 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,5 @@
"purescript-arrays" : "*",
"purescript-tuples" : "*",
"purescript-maybe" : "*"
},
"devDependencies": {
"purescript-quickcheck" : "*"
}
}
16 changes: 16 additions & 0 deletions examples/Collatz.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Main where

import Data.Tuple
import Data.Maybe
import Data.Array
import Data.Unfoldable

import Debug.Trace

collatz :: Number -> [Number]
collatz = unfoldr step
where
step 1 = Nothing
step n = Just $ Tuple n $ if n % 2 == 0 then n / 2 else n * 3 + 1

main = print $ collatz 1000
36 changes: 21 additions & 15 deletions src/Data/Unfoldable.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,30 @@ module Data.Unfoldable where

import Data.Maybe
import Data.Tuple
import Data.Function
import Control.Monad.Eff
import Control.Monad.ST

class Unfoldable t where
unfoldr :: forall a b. (b -> Maybe (Tuple a b)) -> b -> t a

foreign import unfoldrArray
"function unfoldrArray(f, b) {\
\ var result = [];\
\ while (true) {\
\ var maybe = f(b);\
\ if (maybe.ctor === \"Data.Maybe.Nothing\") {\
\ return result;\
\ } else if (maybe.ctor === \"Data.Maybe.Just\") {\
\ result.push(maybe.values[0].values[0]);\
\ b = maybe.values[0].values[1];\
\ }\
\ }\
\}" :: forall a b. Fn2 (b -> Maybe (Tuple a b)) b [a]
foreign import newEmptySTArray
"function newEmptySTArray() {\
\ return [];\
\}" :: forall eff h a. Eff (st :: ST h | eff) (STArray h a)

instance unfoldableArray :: Unfoldable [] where
unfoldr = runFn2 unfoldrArray
unfoldr f b = runPure (runSTArray (do
arr <- newEmptySTArray
seed <- newSTRef b
idx <- newSTRef 0
untilE $ do
b1 <- readSTRef seed
case f b1 of
Nothing -> return true
Just (Tuple a b2) -> do
i <- readSTRef idx
pokeSTArray arr i a
writeSTRef seed b2
writeSTRef idx (i + 1)
return false
return arr))

0 comments on commit 0b4d1ce

Please sign in to comment.