Skip to content

Instantly share code, notes, and snippets.

View Yoric's full-sized avatar

David Teller Yoric

View GitHub Profile
@Yoric
Yoric / hello_web.js
Created May 30, 2011 09:56
Variant on Hello, Web JS-style
/**
* The function defining our user interface.
*/
start = -> <>Hello, web!</> //HTML-like content. As the last value of the function, this is the result.
//Reduced syntax for trivial functions
/**
* Create and start a server delivering user interface [start]
*/
start_server(one_page_server("Hello", start))
@Yoric
Yoric / hello_web.py
Created May 30, 2011 09:59
Hello, web in Opa (revised syntax)
/**
* The function defining our user interface.
*/
def start():
<>Hello, web!</> //HTML-like content. As the last value of the function, this is the result.
/**
* Create and start a server delivering user interface [start]
*/
server:
@Yoric
Yoric / hello_web2.py
Created May 30, 2011 09:59
Hello, web in Opa (revised syntax)
server:
one_page_server("Hello", def(): <>Hello, web!</>)
@Yoric
Yoric / storage.opajs
Created June 1, 2011 12:17
Storage (variant on match)
/**
* Add a path called [/storage] to the schema of our graph database.
*
* This path is is a dictionary indexed by [string]s of values
* of type [option(string)], i.e. values that may either be
* a string or omitted. To find out whether the value is present,
* one should use pattern-matching - case [{some: ...}] if the
* value is present, [{none}] if the value is omitted.
*
* Note: db is a keyword.
@Yoric
Yoric / storage.js
Created June 1, 2011 12:19
Storage (variant on fields)
/**
* Add a path called [/storage] to the schema of our graph database.
*
* This path is is a dictionary indexed by [string]s of values
* of type [option(string)], i.e. values that may either be
* a string or omitted. To find out whether the value is present,
* one should use pattern-matching - case [{some: ...}] if the
* value is present, [{none}] if the value is omitted.
*
* Note: db is a keyword.
@Yoric
Yoric / weather.opa
Created June 13, 2011 13:34
Weather parser
world_weather_parser =
request = xml_parser
<request> <type>_*</> <query>query={Xml.Rule.string}</> </> -> query
current = xml_parser
<current_condition>_*</current_condition> -> void
entry = xml_parser
<weather> <date>dateStr={Xml.Rule.string}</>
<tempMaxC>tempMax={Xml.Rule.integer}</>
<tempMaxF>_*</>
<tempMinC>tempMin={Xml.Rule.integer}</>
/**
* @param source A function whose calls will be monitored
* @param observer A function triggered whenever [source] is called
*/
(function(source, observer){
var original_apply = source.apply;
source.apply = function(args) {
observer();
original_apply.apply(args);
}
(**
Fold upon a matrix in a spiral.
Note: implementation is tail-rec
@param f A function to apply consecutively to [init] and to elements of the matrix
@param init An initial value (see the general definition of fold functions for more details)
@param matrix A matrix, i.e. an array of arrays of the same length
*)
let fold_spiral f init matrix =
(**
Enumerate the values of a matrix in spiral order.
@param matrix A matrix, i.e. an array of arrays of the same length
@return A lazy enumeration of the matrix.
*)
let enum_spiral matrix =
let rec loop ~depth =
let minx = depth
and maxx = width - depth - 1
@Yoric
Yoric / common.cpp
Created June 18, 2011 09:14
C++ brush-up, part 1
#include <iostream>
#include "common.hpp"
template<class T>
void cio(const char* text, T& result)
{
std::cout << text;
std::cin >> result;
}