Skip to content

Instantly share code, notes, and snippets.

function markov<T>(findFirst: () => T[][], findNext: (previous: T) => T[][]): T[][] {
function chooseRandom<T>(xs: T[]): T {
return xs[Math.floor(Math.random() * xs.length)];
}
function last<T>(xs: T[]): T {
return xs[xs.length - 1];
}
var result: T[][] = [];
@AyaMorisawa
AyaMorisawa / ILocale.ts
Last active October 23, 2015 10:33
JSON Locale definition
interface ILocale {
langCode: string;
langName: string;
groups: {
groupName: string,
translates: {
source: string,
translate: string
}[]
}[];
interface Functor<A> {
map<B>(f: (a: A) => B): Functor<B>;
}
interface Monad<A> extends Functor<A> {
map<B>(f: (a: A) => B): Monad<B>;
bind<B>(f: (a: A) => Monad<B>): Monad<B>;
}
interface Eq<A> {
var p = console.log.bind(console);
var q = require('readline-sync').question;
var gcd = (a, b) => !b ? a : gcd(b, a % b);
var r = () => Math.floor(Math.random()*9)+1
var i = 1;
for(;;) {
var x = r();
var y = r();
var df = x % y === 0;
if(df) continue;
var p = console.log.bind(console);
var q = require('readline-sync').question;
var r = () => Math.floor(Math.random()*15)+1
var i = 1;
for(;;) {
var x = r();
p(`(${i}) ${Math.pow(x, 2)}`);
var a = parseInt(q('Input the answer: '));
var c = a === x;
p(c ? 'Good!' : `Bad: The correct answer is ${x}`);
// Task
const logTask = Task.sync<string, void>(text => console.log(text));
const taskA = Task.delay(1000).map(() => 1 + 2 + 3).map(String);
const taskB = Task.resolve('Hello');
Task.all([taskA, taskB]).map(xs => xs.join(', ')).next(logTask).run();
Task.race([taskA, taskB]).next(logTask).run();
// Promise
const logPromise = (text: string) => console.log(text);
const promiseA = new Promise<void>(resolve => setTimeout(resolve, 1000)).then(() => 1 + 2 + 3).then(String);
Task.delay(1000).next(() => print('Yeah'));
const { value, stories } = new Writer(3, ['3'])
.write(x => [x * 5, '* 5'])
.write(x => [x + 3, '+ 3']);
print(value).next(() => print(stories.join(' '))).run();
// Output:
// 18
// 3 * 5 + 3
function quicksort([x, ...xs]: number[]): number[] {
return typeof x === 'undefined' ? [] : (() => {
const smallerSorted = quicksort(xs.filter(a => a <= x));
const biggerSorted = quicksort(xs.filter(a => a > x));
return [...smallerSorted, x, ...biggerSorted];
})();
}
var canvas;
var ctx;
var x;
var y;
var isRightPressed;
var isLeftPressed;
var isUpPressed;
var isDownPressed;
window.addEventListener('load', function() {