Skip to content

Instantly share code, notes, and snippets.

View qsona's full-sized avatar

qsona qsona

View GitHub Profile
@qsona
qsona / 0_README.md
Last active November 13, 2022 05:20
How to write polymorphic code with JavaScript's plain objects and functions (objects are immutable)

How to write polymorphic code with JavaScript's plain objects and functions (objects are immutable)

概要

JavaScript の Plain Object (ただの) と、関数のみを利用して、Polymorphism を実現したい。

とはいえ Polymorphism の定義は人によって違うので、以下の仕様とルールを満たすものとしたい。

そもそもそれは Polymorphism ではない、あるいは意味をなさない、というような反論があればそれも歓迎。

@qsona
qsona / connection.rb
Last active March 30, 2020 18:56
Faraday MyRaiseError middleware
# A patch for faraday raise_error middleware for my usecase
# faraday: https://github.com/lostisland/faraday
class Connection
def self.foo_service
Thread.current[:foo_connection] ||= Faraday.new(Rails.application.config.x.foo_service_url) do |conn|
conn.request :json
conn.response :my_raise_error # See below comment
conn.response :json, :content_type => /\bjson$/
@qsona
qsona / class.ts
Created September 26, 2019 16:38
class inheritance and type
abstract class A<T> {
a() { return new Map<string, T>(); }
static b(): number { return 1; }
}
class X<T> extends A<T> {}
class Y extends A<number> {}
new X<number>().a(); // OK
new Y().a(); // OK
X.b(); // OK
@qsona
qsona / index.js
Created August 20, 2019 17:16
minesweeper
const assert = require('assert');
const _ = require('lodash');
class Masu {
constructor(isMine, isOpened) {
this.isMine = isMine;
this.isOpened = isOpened;
}
}
@qsona
qsona / yubisen.ts
Created August 18, 2019 18:03
yubisen後退解析
import assert from 'assert';
type State = [number, number, number, number];
function stateToHashNum(state: State): number {
const [x1, x2, y1, y2] = state;
return (x1 << 9) | (x2 << 6) | (y1 << 3) | y2;
}
function hashNumToState(num: number): State {
@qsona
qsona / App.tsx
Last active August 17, 2020 13:32
TypeScript example of boardgame.io tutorial
import React from 'react';
import './App.css';
import { Client } from 'boardgame.io/react';
import { Game, IPlayer, IGameCtx } from 'boardgame.io/core';
import { AI, IAIMoveObj } from 'boardgame.io/ai';
type GameState = {
cells: Array<IPlayer | null>
};
@qsona
qsona / kanpe.md
Last active January 10, 2019 11:37
App Client Melting Pot #1 Panel Discussion

App Client Melting Pot #1 Panel Discussion

ダンボー田中vs軍曹関の設計トークバトル! ひゃたもあるよ

登壇者

# 22 引数に配列をとって、重複している値を昇順の配列にして返す関数を作れ。
# 例)引数 [1, 3, 2, 3, 8, 3, 2] => 返り値 [2, 3]
def q22(array)
dup = {} # key: number, value: duplicate count (2以上のときは2)
ans = []
array.each do |num|
case dup[num]
when nil
dup[num] = 1
@qsona
qsona / microfrontends_meetup.md
Last active July 30, 2018 02:22
Micro Frontends Meetup (WIP)

(WIP) Microservices Meetup vol.7 - Micro Frontends -

Micro Frontendsについて議論を深めたい欲が高まっているので

時期

2018年7月下旬 (登壇者決まり次第調整)

内容 (案)

@qsona
qsona / ruby.rb
Created August 18, 2017 06:24
form encode/decode
Faraday::NestedParamsEncoder.encode({"a"=>[{"b"=>"1", "c"=>"2"}, {"b"=>"3", "c"=>"4"}]})
# => "a%5B%5D%5Bb%5D=1&a%5B%5D%5Bc%5D=2&a%5B%5D%5Bb%5D=3&a%5B%5D%5Bc%5D=4"
URI.decode('a%5B%5D%5Bb%5D=1&a%5B%5D%5Bc%5D=2&a%5B%5D%5Bb%5D=3&a%5B%5D%5Bc%5D=4')
# => "a[][b]=1&a[][c]=2&a[][b]=3&a[][c]=4"
Faraday::NestedParamsEncoder.encode({"a"=>[{"b"=>"1", "c"=>"2"}, {"d"=>"3", "e"=>"4"}]})
# => "a%5B%5D%5Bb%5D=1&a%5B%5D%5Bc%5D=2&a%5B%5D%5Bd%5D=3&a%5B%5D%5Be%5D=4"
URI.decode("a%5B%5D%5Bb%5D=1&a%5B%5D%5Bc%5D=2&a%5B%5D%5Bd%5D=3&a%5B%5D%5Be%5D=4")
# => "a[][b]=1&a[][c]=2&a[][d]=3&a[][e]=4"
Faraday::NestedParamsEncoder.decode("a%5B%5D%5Bb%5D=1&a%5B%5D%5Bc%5D=2&a%5B%5D%5Bd%5D=3&a%5B%5D%5Be%5D=4")