Skip to content

Instantly share code, notes, and snippets.

@yuushimizu
yuushimizu / use.ts
Created April 18, 2023 02:47
Compress GraphQL when importing *.graphql files
import DetailedIssueQuery from "./query.graphql";
@yuushimizu
yuushimizu / svelte-dialog-store.ts
Created August 24, 2021 17:00
svelte-dialog-store
<script>
import {writable} from "svelte/store";
function createDialog() {
const internal = writable(undefined);
return {
...internal,
async show(param) {
let onResult = () => {};
const promise = new Promise((resolve) => {
@yuushimizu
yuushimizu / location-params.ts
Last active August 25, 2021 00:59
location-params
export type Mapper<T> = {
toParam(value: T): string;
fromParam(value: string): T;
defaultValue: T;
};
export type Mappers<Params> = {
[Name in keyof Params]: Mapper<Params[Name]>;
};
@yuushimizu
yuushimizu / svelte-async-readable.ts
Created August 24, 2021 16:11
svelte-async-readable
import {readable, writable, Readable} from "svelte/store";
export type AsyncReadable<T> = Readable<T> & {
promise: Readable<Promise<T>>;
};
export function asyncReadable<T>(initialValue: T, resolver: () => Promise<T>): AsyncReadable<T> {
const promiseStore = writable(Promise.resolve(initialValue));
return {
...readable(initialValue, (set: (value: T) => void) => {
@yuushimizu
yuushimizu / Coroutine.swift
Created April 17, 2019 01:31
immutable coroutine in swift
import Cocoa
@dynamicCallable
struct Coroutine<Argument, Result> {
private enum Block {
case step((Argument) -> Result)
case delegation((Argument) -> Coroutine)
}
private let first: Block
@yuushimizu
yuushimizu / codeiq432-1.rb
Created September 2, 2013 10:53
CodeIQ 432
numbers = []
crossing_points = 0
open(ARGV[0]) {|input|
order = 0
until input.eof?
n = input.readline.to_i
crossing_points << n
crossing_points += numbers.count {|m| m > n}
numbers << n
end
@yuushimizu
yuushimizu / smart-scroll.js
Created February 24, 2012 05:47
Rapid scrolling on smartphones.
javascript:(function(){if(scrollY<=0)scrollTo(scrollX,0);var d=document,m=Math,b=d.body,ww=innerWidth,wh=innerHeight,bw=b.scrollWidth,bh=b.scrollHeight,c=d.createElement('div'),s=c.style,f=d.createElement('div'),fs=f.style;s.position=fs.position='absolute';s.padding=s.mergin=fs.padding=fs.mergin=s.top=s.left=0;s.width=bw+'px';s.height=bh+'px';s.zIndex=9999;s.backgroundColor='rgba(0,0,0,0.2)';fs.border='dashed 1px #f00';fs.backgroundColor='rgba(255,0,0,0.1)';fs.width=ww*ww/bw+'px';fs.height=wh*wh/bh+'px';var mf=function(nx,ny){fs.left=nx+ww*nx/bw+'px';fs.top=ny+wh*ny/bh+'px'};mf(scrollX,scrollY);fs.zIndex=9998;c.ontouchmove=c.ontouchstart=c.onmousemove=function(e){var t=e.touches?e.touches[0]:e,nx=m.max(m.min((t.pageX-scrollX)*bw/ww-ww/2,bw-ww),0),ny=m.max(m.min((t.pageY-scrollY)*bh/wh-wh/2,bh-wh),0);fs.visibility='hidden';mf(nx,ny);scrollTo(nx,ny);fs.visibility='visible';return false};c.ontouchend=c.ontouchcancel=c.onmousedown=function(){var x=scrollX,y=scrollY;b.removeChild(c);b.removeChild(f);scrollTo(x,y)}
@yuushimizu
yuushimizu / gdq2011-web-game.js
Created September 12, 2011 14:42
GDQ2011 Web Game Bookmarklet
javascript:void((function(){t=$('head :last').text();ca=eval(t.substring(t.indexOf('setup(')+6,t.indexOf(']')+1));(function(ca){$('#answer').val($.map(ca,function(c,i){ca.splice(i,1,null);r=$.inArray(c,ca);ca.splice(i,1,c);return r}).join(','));$('#solve').submit()})(ca)})())
@yuushimizu
yuushimizu / multi_method.rb
Created March 23, 2011 02:23
multi-method in ruby
module MultiMethod
class GenericProc
class Node
attr_reader :type, :generic
def initialize(type)
@type = type
@generic = GenericProc.new
end
end
def self.truncate_args_for(proc, args)