Skip to content

Instantly share code, notes, and snippets.

@jochasinga
jochasinga / newton.csv
Created November 19, 2018 11:38
Table demonstrating Newton's square root method derivation
Guess Quotient Average
1 2/1 = 2 (2 + 1)/2 = 1.5
1.5 2/1.5 = 1.3333 1.3333 + 1.5/2 = 1.4167
1.4167 2/1.4167 = 1.4118 (1.4167 + 1.4118)/2 = 1.4142
1.4142 ... ...
@jochasinga
jochasinga / two_mod.ml
Created November 7, 2018 18:06
Ocaml modules
module State = struct
type t = int
let inc (n: t) = Transformer.inc n
end
;;
module Transformer = struct
let inc (n: State.t) = n + 1
end
;;
@jochasinga
jochasinga / index.js
Created October 15, 2014 16:14
Angularfire three-way binding
// Register firebase module
var app = angular.module("app", ["firebase"]);
// Set up controller function
app.controller("Ctrl", function($scope, $firebase) {
var firebaseRef = new Firebase(
// Replace this fictional URL with your own
"https://burning-limbo-6666.firebaseio.com/colors"
);
// create an AngularFire ref to the data
@jochasinga
jochasinga / matrix.ml
Created October 28, 2018 13:44
Experiment with matrix multiplication and dot product
let transpose A =
let new_mat = Array.make_matrix (Array.length A.(0)) (Array.length A) 0
in
for y = 0 to (Array.length A) - 1 do
for x = 0 to (Array.length A) - 1 do
new_mat.(y).(x) <- A.(x).(y)
done
done;
new_mat
in
@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 / 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 / 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 / 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}