From 0b4d1ce8cabe1cdf7b46542fa18f26294303f941 Mon Sep 17 00:00:00 2001 From: Phil Freeman Date: Wed, 27 Aug 2014 11:17:25 -0700 Subject: [PATCH] Fix #1, example --- .gitignore | 2 +- Gruntfile.js | 13 ++++++++++++- bower.json | 3 --- examples/Collatz.purs | 16 ++++++++++++++++ src/Data/Unfoldable.purs | 36 +++++++++++++++++++++--------------- 5 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 examples/Collatz.purs diff --git a/.gitignore b/.gitignore index 9b41405..1bace05 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ /output/ /node_modules/ /bower_components/ -/tmp/ +/dist/ diff --git a/Gruntfile.js b/Gruntfile.js index e13c0ad..65dd0ae 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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"]); }; diff --git a/bower.json b/bower.json index 6893fc8..8197887 100644 --- a/bower.json +++ b/bower.json @@ -18,8 +18,5 @@ "purescript-arrays" : "*", "purescript-tuples" : "*", "purescript-maybe" : "*" - }, - "devDependencies": { - "purescript-quickcheck" : "*" } } diff --git a/examples/Collatz.purs b/examples/Collatz.purs new file mode 100644 index 0000000..ee7bb3a --- /dev/null +++ b/examples/Collatz.purs @@ -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 diff --git a/src/Data/Unfoldable.purs b/src/Data/Unfoldable.purs index 619e40e..72e790e 100644 --- a/src/Data/Unfoldable.purs +++ b/src/Data/Unfoldable.purs @@ -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))