Skip to content

Instantly share code, notes, and snippets.

function gcd(m, n) { return n ? gcd(n, m % n) : m; }
// CalcAgeAt calculates the age at the given time.
func CalcAgeAt(t time.Time, birthDay time.Time) int {
age := t.Year() - birthDay.Year()
if t.YearDay() < birthDay.YearDay() {
age--
}
return age
}
// Kahan-Babuska algorithm
export const sum = (x: number[]) => {
if (x.length === 0) return 0
let res = x[0], c = 0, t = 0
for (let i = 1; i < x.length; i++) {
t = res + x[i]
if (Math.abs(res) >= Math.abs(x[i])) {
c += res - t + x[i]
} else {
c += x[i] - t + res
const grades = ['小1', '小2', '小3', '小4', '小5', '小6', '中1', '中2', '中3'];
// 誕生日からat時点での学年を算出
const birthdayToSchoolGrade = (birthday, at) => {
const d = new Date(at);
d.setMonth(d.getMonth() - 3);
d.setMonth(3);
d.setDate(1);
const bd = new Date(birthday);
const dNum = Number(d.toISOString().substring(0, 10).replaceAll('-', ''));
const memoize = (func) => {
const memo = new Map()
return (...args) => {
const key = JSON.stringify(args)
if (!memo.has(key)) memo.set(key, func(...args))
return memo.get(key)
}
}
/*
#!/bin/bash
FROM_USER=user1
FROM_HOST=host1
FROM_DB=database1
FROM_TBL=table1
TO_USER=user2
TO_HOST=host2
TO_DB=database2
func Repeat(s string, n uint) string {
switch n {
case 0:
return ""
case 1:
return s
}
max := len(s) * int(n)
var b strings.Builder
function sharpeRatio(array, interest = 0) {
if (array.length < 3) return { mean: 0, sd: 0, sharpeRatio: 0 };
let mean = 0, sqsum = 0, l = array.length;
for (let n = 1; n < l; n++) {
const changeRate = (array[n] - array[n - 1]) / array[n - 1];
const x = changeRate - mean;
mean += x / n;
sqsum += (n - 1) * x * x / n;
}
const sd = Math.sqrt(sqsum / (l - 1));
// t期運用時点における運用総額をs(t)とする。
// 初期金額をα,各期の積立金額をβ,期待リターンをR(= 1 + r)とすると,各期の運用総額sは,期間tの関数S(t)として次の式で表される。
//
// S(t) = (α * R^t) + β * Σ(k = 0, t - 1) R^k
//
// ※各期の計算は以下のようになる
//
// S(t = 0) = α
// S(t = 1) = α * R + β = (α * R^1) + (β * R^0)
// S(t = 2) = (α * R + β) * R + β = (α * R^2) + (β * R^1 + β * R^0)
function debounce(fun, wait = 0) {
let id = null;
return () => {
if (id !== null) clearTimeout(id)
id = setTimeout(() => {
fun();
id = null;
}, wait);
};
}