Skip to content

Instantly share code, notes, and snippets.

View jsliang's full-sized avatar

Jui-Shan Liang (Jenny) jsliang

View GitHub Profile
@mofas
mofas / map.js
Last active May 9, 2018 06:16
Build map in function closure
// At first, we need to build empty map.
// Empty map is just a function, it will return undefined when invoked.
// When you set new key-value into map, it will wrap a new function around previous map,
// and when you get something from map with key, it will pass key to every nested function until it match or hit empty map.
const map = () => undefined;
const set = map => (key, value) => q => q === key ? value: map;
const get = map => key => typeof map === 'function' ? get(map(key))(key) : map;
// Demo time!
@mofas
mofas / y-combinator.js
Last active May 9, 2018 06:16
y-combinator
// basic idea
(f => f(f)(5))(f => n => n == 0 ? 1 : (n * f(f)(n-1)));
// This version doesn't work, because js is not lazy.
// const Y = f => (x => f(x(x)))(x => f(x(x)));
// const Y = f => (x => x(x))(x => f(a => x(x)(a)));
const Y = f => (x => f(y => x(x)(y)))(x => f(y => x(x)(y)));
@mofas
mofas / stream.js
Created April 29, 2018 00:50
Try to implement simple stream
// Stream
const stream = f => (x => x(x))(y => init => [init, m => y(y)(f(m))]);
const add3Stream = stream(n => n + 3);
const mult2Stream = stream(n => n * 2);
const inspect = stream(n => {
console.log('inspect:' + n);
return 0;
});
const pipe = s1 => s2 => x => {
javascript:function minutesSeconds(s){return(s-(s%=60))/60+(9<s?':':':0')+s}function start(){var currentURL=window.location.href;var bar=document.getElementsByClassName("ytp-progress-bar")[0];var seconds=bar.getAttribute("aria-valuenow");var secondsInMS=minutesSeconds(seconds);var URL=currentURL+"?t="+seconds;var obsidianFormat="["+secondsInMS+"]("+URL+")";navigator.clipboard.writeText(obsidianFormat);null;var elemDiv=document.createElement('div');elemDiv.innerHTML="<h1 style='font-size:100px; color:white; text-align:center; margin-top:2em;'>"+secondsInMS+"</h1>";elemDiv.style.cssText='position:absolute;width:100%;height:100%;opacity:0.8;z-index:1000;background:#000;';document.body.appendChild(elemDiv);setTimeout(function(){elemDiv.style.display="none"},600)}start();