Skip to content

Instantly share code, notes, and snippets.

🏠
Working from home

Joe Chasinga jochasinga

View GitHub Profile
@jochasinga
jochasinga / fork_and_wait.c
Created Mar 22, 2019
Fork and wait system calls
View fork_and_wait.c
include <stdio.h>
include <stdlib.h>
include <unistd.h>
include <sys/wait.h>
int main(int argc, char *argv[])
{
printf("hello world (pid:%d)\n", (int) getpid());
int rc = fork();
// Fork failed, let's exit.
View refactor3.js
const baseConfig = {
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
headers: {
"Content-Type": "application/json; charset=utf-8",
},
redirect: "follow",
referrer: "no-referrer",
};
View refactor2.js
const baseConfig = {
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
headers: {
"Content-Type": "application/json; charset=utf-8",
},
redirect: "follow",
referrer: "no-referrer",
};
@jochasinga
jochasinga / refactor1.js
Last active Mar 7, 2019
Step one in refactor
View refactor1.js
async function postLoginData(data) {
const loginUrl = `${apiBaseUrl}/login`;
let response = await fetch(loginUrl, {
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
headers: {
"Content-Type": "application/json; charset=utf-8",
},
@jochasinga
jochasinga / newton-sqrt.scm
Created Nov 19, 2018
Newton's method of finding a square root
View newton-sqrt.scm
(define (square-of x)
(define (average x y)
(/ (+ x y) 2))
(define (improve guess x)
(average guess (/ x guess)))
(define (good-enough? guess x)
(< (abs (- (square-of guess) x)) 0.001))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
@jochasinga
jochasinga / newton.csv
Created Nov 19, 2018
Table demonstrating Newton's square root method derivation
View newton.csv
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 ... ...
View two_mod.ml
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 / matrix.ml
Created Oct 28, 2018
Experiment with matrix multiplication and dot product
View matrix.ml
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 Mar 4, 2018
Implementation of a Bitcoin Merkle Tree
View merkle.ml
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 Feb 9, 2018
Exploring first Bucklescript
View websocket.ml
type socket
type event
type config = {
reconnect: bool;
debug: bool;
timeout: int;
interval: int
}
You can’t perform that action at this time.