Skip to content

Instantly share code, notes, and snippets.

Avatar

Dagang Wei weidagang

  • Bay Area, California
View GitHub Profile
@weidagang
weidagang / free-monad.js
Last active Dec 21, 2020 — forked from DrBoolean/free-er.js
Free Monad in JavaScript
View free-monad.js
// 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 Oct 27, 2017 — forked from Gozala/go.js
Go routines for JS
View go.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.
View ioref.hs
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
View io.hs
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 Jan 14, 2017
Simulating Haskell typeclass with Java generic interface.
View typeclass.java
// 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 Dec 29, 2020
Distributed MVCC based cross-row transaction
View distributed_mvcc_cross_row_transaction.py
#!/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
View mvcc_read_committed.py
# 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
View least_square_approximation.ipynb
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 Jan 22, 2021
MATLAB - least square approximation
View least_square_approximation.m
% 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 Aug 29, 2015
Lazy Evaluation
View 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]