Skip to content

Commit

Permalink
Merge pull request #28 from purescript-contrib/topic/remove-st-array
Browse files Browse the repository at this point in the history
Topic/remove st array
  • Loading branch information
ethul committed Nov 29, 2014
2 parents c158ca2 + 0e1ffc8 commit 622b948
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 102 deletions.
15 changes: 0 additions & 15 deletions MODULE.md
Original file line number Diff line number Diff line change
Expand Up @@ -729,21 +729,6 @@
when :: forall e a b. b -> Q -> QEff e (Promise a b)


## Module Angular.ST

### Values

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

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

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

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

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


## Module Angular.Scope

### Types
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"dist"
],
"dependencies": {
"purescript-arrays": "0.2.2",
"purescript-arrays": "0.3.0",
"purescript-either": "0.1.4",
"purescript-exceptions": "0.2.2",
"purescript-foldable-traversable": "0.1.4",
Expand Down
56 changes: 23 additions & 33 deletions examples/Todomvc/Controller.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import qualified Data.Array as A
import qualified Data.String as S
import Data.Foldable
import Data.Maybe
import Control.Apply ((*>))
import Control.Monad (unless, when)
import Control.Monad.Eff
import Control.Monad.ST (ST(), STArray(), newSTArray, runSTArray)

import Angular (copy, extend)
import Angular.Location (Location(), getPath, setPath)
import Angular.Scope (Scope(), watch, readScope, extendScope, modifyScope)
import Angular.This(readThis, writeThis)
import Angular.ST (readSTArray, pushSTArray, pushAllSTArray, writeSTArray, spliceSTArray)
import Angular.This (readThis, writeThis)

import Todomvc.Storage (Store(), Todo(), get, put)

Expand All @@ -23,33 +22,31 @@ addTodo scope = do
unless empty $ do
let todo = { title: title, completed: false }
todos <- get
put $ todos <> [todo]
pushSTArray s.todos todo
extendScope { newTodo: ""
let res = todos <> [todo]
put res
extendScope { todos: res
, newTodo: ""
, remainingCount: s.remainingCount + 1 } scope

todoCompleted scope todo
= modifyScope (\s -> do
let change = if todo.completed then -1 else 1
arr <- readSTArray s.todos
put arr
put s.todos
return { remainingCount: s.remainingCount + change }
) scope

clearCompletedTodos scope = do
s <- readScope scope
arr <- readSTArray s.todos
let res = A.filter (\a -> not a.completed) arr
let res = A.filter (\a -> not a.completed) s.todos
put res
writeSTArray s.todos res
extendScope { todos: res } scope

markAll scope compl
= modifyScope (\s -> do
arr <- readSTArray s.todos
let res = (\a -> a { completed = not compl }) <$> arr
let res = (\a -> a { completed = not compl }) <$> s.todos
put res
writeSTArray s.todos res
return { remainingCount: if compl then A.length res else 0 }
return { todos: res
, remainingCount: if compl then A.length res else 0 }
) scope

editTodo scope todo = do
Expand All @@ -62,31 +59,26 @@ doneEditing scope todo
let title = S.trim todo.title
when (S.length title == 0) $ removeTodo scope todo
extend todo { title: title }
arr <- readSTArray s.todos
put arr
put s.todos
return { editedTodo: Nothing }
) scope

removeTodo scope todo = do
s <- readScope scope
arr <- readSTArray s.todos
let i = A.findIndex (\a -> refEq a todo) arr
unless (i == -1) do
let res = A.filter (\a -> not $ refEq a todo) s.todos
unless (A.length res == A.length s.todos) do
let c = if todo.completed then 0 else -1
extendScope { remainingCount: s.remainingCount + c} scope
spliceSTArray s.todos i 1 []
put arr
extendScope { todos: res
, remainingCount: s.remainingCount + c } scope
put res

revertEditing scope todo = do
s <- readScope scope
arr <- readSTArray s.todos
let i = A.findIndex (\a -> refEq a todo) arr
unless (i == -1) do
let res = A.filter (\a -> not $ refEq a todo) s.todos
unless (A.length res == A.length s.todos) do
case s.originalTodo of
Just t -> do
spliceSTArray s.todos i 1 [t]
doneEditing scope t
return unit
Just t -> extendScope { todos: res <> [t] } scope *>
doneEditing scope t
Nothing -> return unit

watchRemainingCount scope =
Expand All @@ -108,15 +100,13 @@ todoctrl scope this location = do
if S.length path == 0 then setPath "/" location else return ""
watchRemainingCount scope
watchLocationPath scope
todosRef <- newSTArray 0 { title: "", completed: false }
todos <- get
pushAllSTArray todosRef todos
let remainingCount = foldl (\b a -> if a.completed then b else b + 1) 0 todos
extendScope { fromMaybe: fromMaybe
, newTodo: ""
, editedTodo: Nothing
, originalTodo: Nothing
, todos: todosRef
, todos: todos
, remainingCount: remainingCount
, location: location
, addTodo: addTodo scope
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "purescript-angular",
"private": true,
"devDependencies": {
"gulp": "3.8.7",
"gulp": "3.8.10",
"gulp-clean": "0.3.1",
"gulp-plumber": "0.6.4",
"gulp-purescript": "0.1.1",
"gulp-purescript": "0.1.2",
"gulp-util": "3.0.0",
"node-static": "0.7.4"
}
Expand Down
51 changes: 0 additions & 51 deletions src/Angular/ST.purs

This file was deleted.

0 comments on commit 622b948

Please sign in to comment.