Skip to content

Instantly share code, notes, and snippets.

@abailly
Created March 8, 2012 08:04
Show Gist options
  • Save abailly/1999571 to your computer and use it in GitHub Desktop.
Save abailly/1999571 to your computer and use it in GitHub Desktop.
Code from OpaDevCamp #1
opa test.opa hello.opa --database mongo
import stdlib.core.web.resource
database todoList {
Todolist.todolist /todos
}
todo_urls = parser {
| "/todo"(.*) : Resource.page("foo",
<h1>Hello</h1>
<label for="todo">Todo:</label>
<input id="todo" onnewline={
function (_) {
item = Dom.get_value(#todo) ;
/todoList/todos/items <+ item
#todos =+ <li>{ item }</li>
}
}>What's next</input>
<ul id="todos">
{
List.map(function(item) { <li> { item } </li> }, /todoList/todos/items)
}
</ul>
)
}
Server.start(
Server.http,
{ custom:todo_urls }
)
type Todolist.todolist = { list(string) items }
module Todolist {
function add(list, item) {
{ items : List.add(item,list.items) }
}
function size(list) {
List.length(list.items)
}
function remove(list, item) {
{items: List.filter(function(anItem) { anItem != item }, list.items)}
}
}
emptyTodolist = { items : [] }
nonEmptyTodoList = { items : ["do something"] }
twoElementsTodoList = { items : ["do something", "do something else"] }
@assert(Todolist.size(Todolist.add(emptyTodolist, "do something")) == 1)
@assert(Todolist.size(Todolist.add(nonEmptyTodoList, "do something else")) == 2)
@assert(Todolist.size(Todolist.remove(nonEmptyTodoList, "do something")) == 0)
@assert(Todolist.remove(twoElementsTodoList, "do something else") == { items: ["do something"]})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment