Skip to content

Instantly share code, notes, and snippets.

View hiroshi-maybe's full-sized avatar

Hiroshi Kori hiroshi-maybe

  • San Jose, CA, United States
View GitHub Profile
@hiroshi-maybe
hiroshi-maybe / eq.hs
Last active August 29, 2015 14:25
Type class default implementation
class MyEq a where
equalsTo :: a -> a -> Bool
equalsTo x y = not (notEqualsTo x y)
notEqualsTo :: a -> a -> Bool
notEqualsTo x y = not (equalsTo x y)
instance MyEq Integer where
equalsTo x y = x == y
main = do putStrLn (show $ notEqualsTo (1::Integer) (2::Integer))
@hiroshi-maybe
hiroshi-maybe / eq.swift
Last active August 29, 2015 14:25
Infinite recursive call in Swift 2.0
protocol Eq {
typealias A = Self
func equalsTo(a: A) -> Bool
func notEqualsTo(a: A) -> Bool
}
extension Eq {
func equalsTo(a: A) -> Bool { return !self.notEqualsTo(a) }
func notEqualsTo(a: A) -> Bool { return self.equalsTo(a) }
}
@hiroshi-maybe
hiroshi-maybe / capture_test.swift
Created July 22, 2015 22:03
Swift research about capture in closure
var x: X? = X()
// leaks if closure1 is called, and it does not capture self by [unowned self]
//x!.closure1()
// if capture self, crashes because x is deallocated befor closure is called.
x!.method()
x = nil
extension NSTimer {
@hiroshi-maybe
hiroshi-maybe / mabe-monad.swift
Last active August 29, 2015 14:24
Maybe monad written in Swift
// from Swiftz
public class K1<A> { public init() {} }
protocol Monad {
typealias A
typealias B
typealias FB = K1<B>
func bind(A -> FB) -> FB;
static func ret(A) -> Self
}
@hiroshi-maybe
hiroshi-maybe / timeout-seq.js
Last active August 29, 2015 14:23
Sequential timeouts by promises
// node --harmony timeout-seq.js
[3,5,10]
.map(function(sec) { return sec * 1000; })
.map(function(duration) {
return function task(resolve) {
setTimeout(function() {
console.log(duration);
resolve();
}, duration);
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
@hiroshi-maybe
hiroshi-maybe / base_form.html
Created August 13, 2014 07:08
Base HTML using jQuery
<html>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
@hiroshi-maybe
hiroshi-maybe / hike.js
Last active August 29, 2015 14:01
Casual argument parser in JavaScript
"use strict";
/****** Library START ******/
var hike = function() {
var user_fn, i, len, rule_strs, rule_parsed,
rule = {}, /* arg length -> [arg name] */
_arguments = Array.prototype.slice.call(arguments);
user_fn = _arguments.pop();
@hiroshi-maybe
hiroshi-maybe / dijkstra.js
Created April 18, 2014 09:12
Implementation of `Dijkstra's algorithm` in JavaScript
var graph1 = {
vertex: ["1","2","3"],
edge: [,
/* vertex1, vertex2, weight */
["1", "2", 4],
["1", "3", 7],
["2", "3", 1]
]
},
graph2 = {
@hiroshi-maybe
hiroshi-maybe / prime.js
Last active December 29, 2015 07:09
3 different implementations to get prime numbers in limited range.
// 71 ms for input:1000000
var prime_procedural = function(end) {
var i,j,n;
var sieve = [],
primes = [];
sieve[0]=false;
for(i=1; i<end+1; i+=1) {
sieve[i]=true;