Skip to content

Instantly share code, notes, and snippets.

@jochasinga
jochasinga / merkle.ml
Created March 4, 2018 08:05
Implementation of a Bitcoin Merkle Tree
type 'a tree =
| Leaf
| Node of 'a * 'a tree * 'a tree
let node_of_tx tx = if String.length tx > 0 then Node (tx, Leaf, Leaf) else Leaf
let tree_of_txs txs =
let nodes = List.map node_of_tx txs in
match nodes with
| [] -> Leaf
@jochasinga
jochasinga / websocket.ml
Last active February 9, 2018 08:00
Exploring first Bucklescript
type socket
type event
type config = {
reconnect: bool;
debug: bool;
timeout: int;
interval: int
}
@jochasinga
jochasinga / bst.es6
Created February 6, 2017 03:15
Binary search tree implemented in ES6
'use strict';
class Node {
constructor(data) {
this.data = data;
this.left = undefined;
this.right = undefined;
}
@jochasinga
jochasinga / bin_search.es6
Created February 5, 2017 22:09
Binary search a sorted array.
function search(dst, arr) {
// sort the input array
arr.sort((a, b) => a-b);
// if array is reduced to a member and still no match, return false
if ((arr.length == 1) && (arr[0] !== dst))
return false;
// define a middlebound
@jochasinga
jochasinga / buttonsWithInput.elm
Created January 2, 2017 23:12
Simple user input and buttons example
-- Modified button example http://elm-lang.org/examples/buttons
-- Added a reset button and a field to indicate step size to increment or decrement
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick, onInput)
import String
-- MAIN
@jochasinga
jochasinga / session.go
Last active October 1, 2017 16:43
Example of middlewares in Go
package main
import (
"log"
"strings"
)
type Session struct {
Email string
Password string
@jochasinga
jochasinga / curry.go
Created November 20, 2016 07:28
Example of currying (higher order functions) in Go
package main
import "fmt"
type List struct {
Inner []int
}
func NewList(s []int) List {
return List{Inner: s}
@jochasinga
jochasinga / server.go
Created November 9, 2016 20:37
Database server in Go that uses url.Value as temporary key-value store and path parameter + query string to set and get data.
/* Database server */
package main
import (
"fmt"
"html"
"log"
"net/http"
"net/url"
@jochasinga
jochasinga / printTime.ino
Created October 3, 2016 09:15
Get and print local time from an Arduino Yun.
#include <Process.h>
Process date; // process used to get the date
int dates, month, years, hours, minutes, seconds; // for the results
int lastSecond = -1; // need an impossible value for comparison
void setup() {
Bridge.begin(); // initialize Bridge
Serial.begin(9600); // initialize serial
@jochasinga
jochasinga / di_3.go
Last active May 31, 2016 14:33
Example of dependency injection in action
package di
import (
"fmt"
"regexp"
)
// This is a pretty dumb function. It can only works
// if you want to stay with "Hello" forever.
func PrintIfMatchedHello(msg string) {