Skip to content

Instantly share code, notes, and snippets.

@kfish
Created October 15, 2013 10:45
Show Gist options
  • Save kfish/6989765 to your computer and use it in GitHub Desktop.
Save kfish/6989765 to your computer and use it in GitHub Desktop.
<!doctype html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script src="TodoFay.js"></script>
<script src="todo.js"></script>
<link rel="stylesheet" href="todo.css">
</head>
<body>
<h2>Todo</h2>
<div ng-controller="TodoCtrl">
<span>{{remaining()}} of {{state().todos.length}} remaining</span>
[ <a href="" ng-click="archive()">archive</a> ]
<ul class="unstyled">
<li ng-repeat="todo in state().todos">
<input type="checkbox" ng-model="todo.done">
<span class="done-{{todo.done}}">{{todo.text}}</span>
</li>
</ul>
<form ng-submit="addTodo()">
<input type="text" ng-model="state().todoText" size="30"
placeholder="add new todo here">
<input class="btn-primary" type="submit" value="add">
</form>
</div>
</body>
</html>
function TodoCtrl($scope) {
var state = new Strict.TodoFay.initialState(7);
var sc = Strict.TodoFay.todoSC;
$scope.state = function () {
return state;
}
var mutate = function (f) {
return (function () {
console.log("mutate " + f)
state = Strict.TodoFay[f](state);
})
}
var fayget = function (f) {
return (function () {
return Strict.TodoFay[f](state);
})
}
console.log(state.todos);
console.log(sc.muts);
for (var m in sc.muts) {
console.log("Setting up mutator for " + sc.muts[m]);
$scope[sc.muts[m]] = mutate(sc.muts[m]);
}
for (var g in sc.gets) {
$scope[sc.gets[g]] = fayget(sc.gets[g]);
}
}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RebindableSyntax #-}
module TodoFay (
Todo(..)
, TodoState(..)
, initialState
, remaining
, addTodo
, archive
, StateController(..)
, todoSC
) where
import Fay.Text
import FFI
import Prelude
data Todo = Todo
{ text :: Text
, done :: Bool
}
data TodoState = TS
{ todos :: [Todo]
, todoText :: Text
}
initialTodos :: [Todo]
initialTodos =
[ Todo "Learn Fay" False
, Todo "Write a fay-angular app" False
]
initialState :: Int -> TodoState
initialState _ = TS initialTodos ""
addTodo :: TodoState -> TodoState
addTodo (TS ts0 txt) = TS (ts0 ++ [Todo txt False]) ""
remaining :: TodoState -> Int
remaining = Prelude.length . filter (not . done) . todos
archive :: TodoState -> TodoState
archive (TS ts txt) = TS (filter (not . done) ts) txt
data StateController = SC
{ muts :: [Text]
, gets :: [Text]
}
todoSC = SC ["addTodo", "archive"] ["remaining"]
@kfish
Copy link
Author

kfish commented Oct 15, 2013

[Object, Object]
0: Object
$$hashKey: "004"
done: false
instance: "Todo"
text: "Learn Fay"
proto: Object
1: Object
$$hashKey: "006"
done: true
instance: "Todo"
text: "Write a fay-angular app"
proto: Object
length: 2
proto: Array[0]
todo.js:22
Fay$$Cons {car: "addTodo", cdr: Fay$$Cons}
car: "addTodo"
cdr: Fay$$Cons
proto: Fay$$Cons
todo.js:23
Setting up mutator for addTodo todo.js:25
Setting up mutator for [object Object]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment