Skip to content

Instantly share code, notes, and snippets.

View weidagang's full-sized avatar

Dagang Wei weidagang

  • Bay Area, California
View GitHub Profile
@weidagang
weidagang / free-monad.js
Last active December 21, 2020 08:55 — forked from DrBoolean/free-er.js
Free Monad in JavaScript
// Forked and modified from https://gist.github.com/DrBoolean/34c1d3651d1338b1b187
const daggy = require('daggy')
const compose = (f, g) => x => f(g(x))
const id = x => x
const kleisli_compose = (f, g) => x => f(x).flatMap(g)
//=============FREE=============
const Free = daggy.taggedSum({Suspend: ['x', 'f'], Pure: ['x']})
@weidagang
weidagang / go.js
Created October 27, 2017 14:34 — forked from Gozala/go.js
Go routines for JS
// Utility function for detecting generators.
let isGenerator = x => {
return Function.isGenerator &&
Function.isGenerator.call(x)
}
// Data type represents channel into which values
// can be `put`, or `received` from. Channel is
// very much like queue where reads and writes are
// synchronized via continuation passing.
import Data.IORef
main :: IO ()
main = do
putStrLn "Create a mutable state 0."
ref <- newIORef (0 :: Int)
putStrLn "Increase by 1."
modifyIORef ref (+1)
result <- readIORef ref
print result
@weidagang
weidagang / io.hs
Created February 3, 2017 07:26
Haskell IO
readInt :: IO Int
readInt = readLn
main = do
print "Press any key to start:"
line <- getLine
print "Type the first integer x:"
x <- readInt
print "Type the second integer y:"
y <- readInt
@weidagang
weidagang / typeclass.java
Last active January 14, 2017 02:15
Simulating Haskell typeclass with Java generic interface.
// Simulating Haskell typeclass with Java generic interface.
public class Main {
public static void main(String[] args) {
MyInt i1 = new MyInt(1);
MyInt i2 = new MyInt(2);
MyInt i3 = i1.add(i2);
System.out.println(i3); // "3"
MyString s1 = new MyString("1");
@weidagang
weidagang / distributed_mvcc_cross_row_transaction.py
Last active December 29, 2020 11:44
Distributed MVCC based cross-row transaction
#!/usr/bin/python3.5
# Author: Dagang Wei (github.com/weidagang)
# Created: 2016-11-19
# Last modified: 2016-11-27
# License: MIT
# Self link: https://gist.github.com/weidagang/1b001d0e55c4eff15ad34cc469fafb84
#
# This code demonstrates the core algorithm for distributed MVCC based cross-row
# transactions. The algorithm is built on top of a distributed key-value database
@weidagang
weidagang / mvcc_read_committed.py
Last active February 13, 2018 14:49
MVCC transaction
# In-memory MVCC transaction of read-committed isolation level.
next_xid = 1
active_xids = set()
records = []
def new_transaction():
global next_xid
print('Txn %d: start' % next_xid)
xid = next_xid
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@weidagang
weidagang / least_square_approximation.m
Last active January 4, 2022 12:44
MATLAB - least square approximation
% MATLAB code for finding the best fit line using least squares method.
% input in the form of matrix, each row is a (x, y).
input = [...
1, 2;...
2, 4.5;...
3, 5.9;...
4, 8.1;...
5, 9.8;...
6, 12.3];
@weidagang
weidagang / lazy-evaluation
Last active August 29, 2015 14:10
Lazy Evaluation
Prelude> let ones = 1 : ones
Prelude> take 3 ones
[1,1,1]
Prelude> let lessThan x y = y < x
Prelude> takeWhile (lessThan 5) [1..]
[1,2,3,4]