Skip to content

Commit

Permalink
working example code in purescript
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Mar 20, 2015
1 parent 34fb261 commit 68b41b5
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
dist
node_modules
bower_components
.psci
102 changes: 61 additions & 41 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,77 @@
module.exports = function(grunt) {
"use strict";

grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
pkg: grunt.file.readJSON("package.json"),

jshint: {
files: ['js/*.js', 'test/*.js'],
options: {
jshintrc: '.jshintrc'
}
},
src: ["src/**/*.purs", "bower_components/**/src/**/*.purs"],

// jshint: {
// files: ["js/*.js", "test/*.js"],
// options: {
// jshintrc: ".jshintrc"
// }
// },

// browserify: {
// dev: {
// src: ["js/io.js"],
// dest: "dist/cube-composer-bundle.js",
// options: {
// browserifyOptions: {
// debug: true
// }
// }
// },
// prod: {
// src: ["js/io.js"],
// dest: "dist/cube-composer-bundle.js"
// }
// },

// uglify: {
// dist: {
// files: {
// "dist/cube-composer-bundle.js": "dist/cube-composer-bundle.js"
// }
// }
// },

browserify: {
dev: {
src: ['js/io.js'],
dest: 'dist/cube-composer-bundle.js',
options: {
browserifyOptions: {
debug: true
}
}
// mochaTest: {
// test: {
// src: ["test/*.js"]
// }
// },

psc: {
options: {
"main": "CubeComposer",
"modules": ["CubeComposer"]
},
prod: {
src: ['js/io.js'],
dest: 'dist/cube-composer-bundle.js'
}
},

uglify: {
dist: {
files: {
'dist/cube-composer-bundle.js': 'dist/cube-composer-bundle.js'
}
all: {
src: ["<%=src%>"],
dest: "dist/main.js"
}
},

mochaTest: {
test: {
src: ['test/*.js']
}
},
dotPsci: ["<%=src%>"],

watch: {
files: ['Gruntfile.js', 'js/*.js', 'test/*.js'],
tasks: ['dev']
files: ["Gruntfile.js", "<%=src%>"],
tasks: ["dev"]
}
});

grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-contrib-watch');
// grunt.loadNpmTasks("grunt-contrib-jshint");
// grunt.loadNpmTasks("grunt-browserify");
// grunt.loadNpmTasks("grunt-contrib-uglify");
// grunt.loadNpmTasks("grunt-mocha-test");
grunt.loadNpmTasks("grunt-purescript");
grunt.loadNpmTasks("grunt-contrib-watch");

grunt.registerTask('dev', ['jshint', 'mochaTest', 'browserify:dev']);
grunt.registerTask('prod', ['jshint', 'mochaTest', 'browserify:prod', 'uglify']);
grunt.registerTask('default', 'prod');
// grunt.registerTask("dev", ["jshint", "mochaTest", "browserify:dev"]);
grunt.registerTask("dev", ["psc", "dotPsci"]);
// grunt.registerTask("prod", ["jshint", "mochaTest", "browserify:prod", "uglify"]);
// grunt.registerTask("default", "prod");
};
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,14 @@
},
"homepage": "https://github.com/sharkdp/cube-composer",
"dependencies": {
"grunt-purescript": "^0.6.0",
"isomer": "^0.2.4",
"jquery": "^2.1.3",
"ramda": "^0.11.0",
"sortablejs": "^1.1.1"
},
"devDependencies": {
"grunt": "^0.4.5",
"grunt-browserify": "^3.5.0",
"grunt-contrib-jshint": "^0.11.0",
"grunt-contrib-uglify": "^0.8.0",
"grunt-contrib-watch": "^0.6.1",
"grunt-mocha-test": "^0.12.7",
"mocha": "^2.2.1",
"should": "^5.2.0"
}
}
72 changes: 72 additions & 0 deletions src/CubeComposer.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module CubeComposer where

import Data.Array
import Data.Foldable
import Data.Maybe
import Debug.Trace

data Cube = Red | Blue

instance showCube :: Show Cube where
show Red = "Red"
show Blue = "Blue"

instance eqCube :: Eq Cube where
(==) Red Red = true
(==) Blue Blue = true
(==) Red Blue = false
(==) Blue Red = false
(/=) x y = not (x == y)

type Stack = [Cube]
type Wall = [Stack]

type Transformer = Wall -> Wall


replace :: Cube -> Cube -> Cube -> Cube
replace a b x = if x == a then b else x

map2d :: (Cube -> Cube) -> Wall -> Wall
map2d = map >>> map

-- | Adaption of Haskells scanl
scanl :: forall a b . (b -> a -> b) -> b -> [a] -> [b]
scanl f bi as = bi : (case as of
[] -> []
a:as' -> scanl f (f bi a) as')

-- | Successively apply all transformers to the initial wall and return
-- | all intermediate transformation steps
allSteps :: [Transformer] -> Wall -> [Wall]
allSteps ts wi = scanl (flip ($)) wi ts

-------- TRANSFORMERS ----------

tTail :: Transformer
tTail = map ((fromMaybe []) <<< tail)

tReplace :: Transformer
tReplace = map2d (replace Red Blue)

{-- tStackEqual :: Transformer --}
{-- tStackEqual [] = [] --}
{-- tStackEqual (s:ss) = (flatten (s:(takeWhile (== s) ss))) : (tStackEqual (dropWhile (== s) ss)) --}

-------- TESTING -------

ts :: [Transformer]
ts = [
tReplace
, tTail
]

initial :: Wall
initial = [[Red], [Red], [Red, Blue], [Red, Blue], [Red, Blue], [Blue]]

main = do
trace $ "Initial: " ++ (show initial)
trace "Steps:"
sequence_ $ map (trace <<< show) $ allSteps ts initial
trace "---"
trace ""

0 comments on commit 68b41b5

Please sign in to comment.