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 / mapreduce.hs
Created November 18, 2014 06:25
mapreduce.hs
reduce :: (a -> b -> b) -> b -> [a] -> b
reduce f b [] = b
reduce f b (x:xs) = f x (reduce f b xs)
map :: (a -> b) -> [a] -> [b]
map f = reduce (\a b -> (f a) : b) []
@weidagang
weidagang / language-in-450-lines.js
Last active August 29, 2015 14:08
language-in-450-lines
///////////////////
/// PARSER CORE ///
///////////////////
// There is an annotated configuration object in the GRAMMAR section below
function Parser(config) {
var prio = this.priorities = config.priorities;
prio["boundary:$"] = [-1, -1];
this.re = config.re;
this.toktypes = config.toktypes;
@weidagang
weidagang / StateMonad.cs
Created July 20, 2014 06:29
Label a Binary Tree with State Monad
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// We demonstrate three ways of labeling a binary tree with unique
// integer node numbers: (1) by hand, (2) non-monadically, but
// functionally, by threading an updating counter state variable
// through function arguments, and (3) monadically, by using a
// partially generalized state-monad implementation to handle the
@weidagang
weidagang / SumOdd.scala
Created July 1, 2014 14:30
HackerRank: Sum of Odd Numbers
package sum.odd
object Main {
def f(arr: List[Int]): Int = arr.map(x => (if (0 == x % 2) 0 else x)).reduce((x, y) => x + y)
def main(args: Array[String]) {
val r = f(List(2, 3, 4, 5, -7));
println(r);
}
}
@weidagang
weidagang / FilterArray.scala
Created July 1, 2014 10:52
HackerRank: Filter Array
package filter.array
object Main {
def filter[T](arr: List[T], cond: T => Boolean): List[T] = arr match {
case x :: xs => if (cond(x)) x :: filter(xs, cond) else filter(xs, cond)
case _ => List[T]()
}
def f(delim: Int,arr: List[Int]): List[Int] = filter(arr, (x: Int) => x < delim)
@weidagang
weidagang / ListReplication.scala
Created July 1, 2014 08:19
HackerRank: List Replication
package list.replication
object Main {
def repeat[T](num: Int, e: T): List[T] = if (num <= 0) List[T]() else e :: repeat(num - 1, e);
def f(num: Int, arr: List[Int]): List[Int] = arr.flatMap(e => repeat(num, e));
def main(args: Array[String]) {
val lst1 = f(4, List(1, 2, 3));
lst1.map(e => println(e))
@weidagang
weidagang / ListLength.scala
Created July 1, 2014 08:18
HackerRank: List Length
package list.length
object Main {
def f(arr: List[Int]): Int = arr match {
case x :: xs => 1 + f(xs)
case _ => 0
}
def main(args: Array[String]) {
val len = f(List(1, 2, 3));
@weidagang
weidagang / timespan.pl
Created July 1, 2014 02:46
Perl: timespan between 2 keywords
#!/usr/bin/env perl
use File::Basename
$nargs = @ARGV;
sub usage() {
my $prog = basename($0);
print "Usage: $prog <start-text> <end-text>\n";
}
@weidagang
weidagang / Boolean.scala
Last active August 29, 2015 14:03
Make True and False as subtypes of Boolean in Scala (Note that the "object" keyword is the Scala way to declare a singleton class)
abstract class Boolean {
def == (other: Boolean): Boolean;
def != (other: Boolean): Boolean;
def && (other: Boolean): Boolean;
def || (other: Boolean): Boolean;
def unary_! (): Boolean;
}
object True extends Boolean {
def == (other: Boolean) = other;
@weidagang
weidagang / bash_aliases.sh
Last active August 29, 2015 14:01
Useful bash aliases
# Bash command aliases and utils
# code format
function astyle-java {
astyle --mode=java --style=linux -s4 $@
}
function astyle-c {
astyle --mode=c --style=linux -s4 $@
}