-
-
Save abailly/1999571 to your computer and use it in GitHub Desktop.
Code from OpaDevCamp #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| opa test.opa hello.opa --database mongo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 } | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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