Skip to content

Instantly share code, notes, and snippets.

View yancya's full-sized avatar
💭
😂

Shinta Koyanagi yancya

💭
😂
View GitHub Profile
@yancya
yancya / 3times.js
Last active October 12, 2017 14:44
function times(n) {
const iterator = (function* gen(n) {
let count = 0;
while (count < n) {
yield count;
count++;
}
})(n);
return Array.from(iterator);
}
SELECT *
FROM UNNEST(ARRAY<STRUCT<`name` string, `group` string, age INT64>>[
('a', 'A', 10),('b', 'A', 11),('c', 'A', 12),
('f', 'B', 20),('e', 'B', 10),('d', 'B', 11)
])
)
SELECT `group`, `age`, COUNT(DISTINCT `name`)
FROM t
GROUP BY ROLLUP(1, 2)
ORDER BY 1, 2;
@yancya
yancya / with_resources_sample.rb
Created September 29, 2017 06:56
with_resources のサンプル
require 'with_resources/toplevel'
using WithResources::TopLevel
resources = -> () {
hello = StringIO.new('Hello')
world = StringIO.new('World')
}
with(resources) do |hello, world|
puts "#{hello.gets}, #{world.gets}"
require 'tapp'
def main(str)
x, y, b = str.split(',').map(&:to_i)
x_size = x.to_s(b).size
min = if x_size.odd?
(1..((b-1).to_s*(x_size / 2 + 1)).to_i(b)).flat_map { |n| check(n, b) }.uniq.select { |_n| _n < x }.size
else
@yancya
yancya / 0_to_cube.rb
Last active July 7, 2017 05:45
CUBE 相当の SQL を生成するメソッド
# columns example
# [
# ['hoge total', 'hoge'],
# ['fuga total', 'fuga'],
# ['piyo total', 'piyo'],
# ]
def cube_select_expressions(columns)
[true, false].repeated_permutation(columns.size).map { |flags|
flags.zip(columns).map { |flag, (total_label, column_name)|
flag ? column_name : "'#{total_label}' AS #{column_name}"
def main(str)
n = str.to_i
n3 = '0' + n.to_s(3)
nums = case n3
when /0$/
[
n3.sub(/20+$/) { |m| '0' + '2' * (m.size - 1) }.to_i(3),
n3.sub(/10+$/) { |m| '0' + '1' * (m.size - 1) }.to_i(3),
n + 1,
@yancya
yancya / fib.ts
Created June 28, 2017 05:19
RxJS でフィボナッチ数列
import { Observable, Subject } from 'rxjs';
const subj = new Subject();
subj.take(10).
startWith([1, 1]).
scan(([a, b], _) => [b, a + b]).
do(([a, b]) => console.log(a)).
subscribe();
const { Observable } = require('rxjs');
const streams = Array(10).fill(null).map(_ => {
const f = Math.random();
console.log(f);
return Observable.of(f).delay(f * 100);
})
Observable.
race(...streams).
const { Observable } = require('rxjs');
const n$ = new Observable.range(0, 100);
const fizz$ = n$.map(n => n % 3 == 0 ? 'Fizz' : '');
const buzz$ = n$.map(n => n % 5 == 0 ? 'Buzz' : '');
Observable.
zip(n$, fizz$, buzz$).
map(([n, fizz, buzz]) => fizz + buzz || n).
do(console.log).
const { Observable } = require('rxjs');
new Observable.range(0, 100).
map(n => [n]).
map(a => a.concat([a[0]%3 == 0 ? 'Fizz' : ''])).
map(a => a.concat([a[0]%5 == 0 ? 'Buzz' : ''])).
map(([n, fizz, buzz]) => fizz + buzz || n).
do(console.log).
subscribe();