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 / object_literal_format.js
Last active December 27, 2015 20:09
Object literal format in Javascript.
// obj1 is much more human readable!
var obj1 = {
"a" : "value",
"bc" : "value",
"def" : "value"
}
var obj2 = {
"a" : "value",
@hiroshi-maybe
hiroshi-maybe / quine.hs
Created May 6, 2013 07:09
Minimum quine on Haskell.
main=putStr$q++show q;q="main=putStr$q++show q;q="
@hiroshi-maybe
hiroshi-maybe / reverse.js
Created May 1, 2013 09:10
A function to reverse a list (loop: n/2, space: n).
var reverse = function (a) {
for (var i=0, arrayLen=a.length ; i<arrayLen/2; i+=1) {
var temp = a[arrayLen-i-1];
a[arrayLen-i-1] = a[i];
a[i] = temp;
}
return a;
};
@hiroshi-maybe
hiroshi-maybe / isSublist.js
Last active December 16, 2015 20:39
A function to check sublist.
var isSublist = function(a,b) {
var a_idx=0;
for(var b_idx=0; b_idx<b.length; b_idx+=1) {
if (a[a_idx]==b[b_idx]) {
if (a_idx==a.length-1) {
return true;
}
a_idx+=1 ;
}
}
@hiroshi-maybe
hiroshi-maybe / isSublist.hs
Created May 1, 2013 08:49
A function to check sublist.
isSublist :: (Eq a) => [a] -> [a] -> Bool
isSublist [] _ = True
isSublist _ [] = False
isSublist (x:xs) ys = case dropWhile (/=x) ys of
[] -> False
(x:rest) -> isSublist xs rest
import Control.Monad.Fix
------------------------------------------------
-- fact
-- fact is a fixpoint for lambda_fact
fact n = if n == 0 then 1 else n * fact (n-1)
lambda_fact = (\g n -> if n == 0 then 1 else n * g (n-1))
-- fixpoint combinator 'fix' "encode" recursion of fact
andThen f1 f2 = do f1
f2
--foo bar = (print "Executing foo") `andThen` (print bar) `andThen` (do return (length bar))
-- Container Thing
data Thing a = Thing a deriving Show
value :: Thing a -> a
value (Thing x) = x
@hiroshi-maybe
hiroshi-maybe / split_int.hs
Created March 30, 2013 08:41
Split an integer to a set of integers (i.e. split_int 5 3 -> [1,1,3], [1,2,2])
split_int :: Int -> Int -> [[Int]]
split_int n set_num = split_int_helper n set_num 1
split_int_helper n 1 _ = [[n]]
split_int_helper n set_num start_idx = do
let last_idx = truncate $ (fromIntegral n)/(fromIntegral set_num)
x <- [start_idx..last_idx]
y <- split_int_helper (n-x) (set_num-1) x
return (x:y)
@hiroshi-maybe
hiroshi-maybe / infinite_newton_raphson_list.hs
Created March 23, 2013 10:09
Generate infinite list of Newton-Raphson method
repApply :: (a->a)->a->[a]
repApply f x = x : repApply f (f x)
newton :: Int -> [Double]
newton n = repApply (\x -> (x+(fromIntegral n)/x)/2) 1
@hiroshi-maybe
hiroshi-maybe / power_set_generator.js
Created March 11, 2013 02:18
Power set generator.
var power_set_generator = function(set) {
var _index=0, _set=set, _max_size= 1<<set.length;
return Object.create(Object.prototype, {
next : { value : function() {
var result = [], n=_index,i=0;
if (_index >= _max_size) return;
for (; n; n>>=1, i+=1) if (n & 1) result.push(_set[i]);
_index+=1;
return result;
}}