Skip to content

Instantly share code, notes, and snippets.

View edalorzo's full-sized avatar

Edwin Dalorzo edalorzo

View GitHub Profile
@edalorzo
edalorzo / Primes.java
Last active May 5, 2016 14:11
Infinite Primes Generator
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.LongPredicate;
import java.util.function.LongSupplier;
import java.util.stream.LongStream;
/**
* Primes generator.
*/
@edalorzo
edalorzo / streams.sml
Created November 7, 2014 14:07
Streams in SML
datatype 'a stream = Empty | Cons of 'a * (unit -> 'a stream)
fun from n = Cons(n, fn () => from(n +1))
val naturals = from(1)
fun filter f xs =
case xs of
Empty => Empty
| Cons(h,t) => if f(h)
then Cons(h, fn () => filter f (t()))
@edalorzo
edalorzo / StreamConcat.java
Created September 14, 2014 19:42
Stream Concatenation
public static <T> Stream<T> concatenate(Stream<T>... streams) {
return Stream.of(streams).reduce(Stream.empty(), Stream::concat);
}
public static void main(String[] args) {
Stream<? extends CharSequence> res = concatenate(
Stream.of("One"),
Stream.of(new StringBuffer("Two")),
Stream.of(new StringBuilder("Three"))
@edalorzo
edalorzo / SublimeTextMissingKeyBindings.json
Created June 4, 2014 21:46
Defines a few missing key bindings in Sublime Text
[
{ "keys": ["home"], "command": "move_to", "args": {"to": "bol"} },
{ "keys": ["end"], "command": "move_to", "args": {"to": "eol"} },
{ "keys": ["shift+end"], "command": "move_to", "args": {"to": "eol", "extend": true} },
{ "keys": ["shift+home"], "command": "move_to", "args": {"to": "bol", "extend": true } },
{ "keys": ["ctrl+home"], "command": "move_to", "args": {"to": "bof"} },
{ "keys": ["ctrl+end"], "command": "move_to", "args": {"to": "eof"} },
{ "keys": ["ctrl+shift+home"], "command": "move_to", "args": {"to": "bof", "extend": true} },
{ "keys": ["ctrl+shift+end"], "command": "move_to", "args": {"to": "eof", "extend": true} },
]
@edalorzo
edalorzo / base64.js
Created December 13, 2013 23:05
Base 64 Encoding and Decoding
(function(){
var index = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9','+','/','='];
var octets = ["","0","00","000","0000","00000","000000","0000000"];
var sixths = ["","00000","0000","000","00","0"];
var paddings = [[],[],[64,64],[],[64]];
@edalorzo
edalorzo / brainfuck.js
Created December 12, 2013 22:26
Branfuck Interpreter
function brainfuck(code,input){
var pc = 0;
var sysout = [];
var sysin = input.split('').reverse();
var ptr = 0;
var data = [0];
var size = code.length;
var program = code.split('')
var loops = [];
@edalorzo
edalorzo / befunge.js
Created December 12, 2013 22:25
Befunge-93 Interpreter
var interpret = (function(){
var stack = [];
var sysout = [];
var dir = '>'
var strmode = false;
var skipmode = false;
var pc = {row:0,col:0};
var program = [];
@edalorzo
edalorzo / pascal.js
Created December 6, 2013 06:06
Pascal Triangle
function pascal(depth){
var value = function(col,row){ return (col===1 || row===1 || col===row) ? 1 : value(col-1, row-1) + value(col, row-1); }
return Array.apply(null, {length: depth})
.map(function(u,row,self){ return Array.apply(null,{length:row+1}).map(function(u,col,self){ return value(col+1,row+1)}); });
}
function Value(value){
this.value = value || 0;
}
Value.prototype.eval = function(){ return this; };
Value.prototype.valueOf = function(){ return this.value.valueOf(); };
Value.prototype.toString = function(){ return this.value.toString(); };
var createOperator = function(){
function reduce(oper){
@edalorzo
edalorzo / romans.js
Created December 2, 2013 15:44
Convert decimal numbers to roman numerals and viceversa
asDecimal = function(){
var romans = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000 };
return function(roman){
return roman.split('').map(function(r){ return romans[r];})
.reduce(function(res,n,i,digits){
var prev = i > 0 ? digits[i-1] : 0;
return prev < n ? res + n - 2 * prev : res + n;
},0);
};
}();