Skip to content

Instantly share code, notes, and snippets.

@arc279
arc279 / a.mjs
Created April 25, 2024 02:10
javascript で並列 worker
// cf. https://maximorlov.com/parallel-tasks-with-pure-javascript/
import { setTimeout } from 'node:timers/promises';
async function doWork(iterator, i) {
for await (const value of iterator) {
await setTimeout(1000);
console.log(`worker ${i}: ${value}`);
}
console.log(`worker ${i} finish`)
}
@arc279
arc279 / main.go
Created October 7, 2023 01:08
lattigo/ckks の鍵を base64 文字列と相互変換するやつ
package main
import (
"encoding/base64"
"fmt"
"github.com/tuneinsight/lattigo/v4/ckks"
"github.com/tuneinsight/lattigo/v4/rlwe"
)
@arc279
arc279 / add-filenumber-to-winston.ts
Last active March 8, 2024 07:36
winston.version '3.8.2' の出力に filenname, linenumber を付与
import winston from "winston";
type LogLevel = keyof typeof winston.config.npm.levels;
const levels = Object.keys(winston.config.npm.levels);
const DEFAULT_LABEL = "-";
const formatAddLocation = winston.format((info) => {
const getLocation = () => {
const limitBak = Error.stackTraceLimit;
@arc279
arc279 / elapsed_millisec.sh
Created July 4, 2022 04:01
bash + awk で経過時間をミリ秒で取得
function elapsed_millisec() {
BEFORE=$(date +%s%3N)
eval "${@}"
AFTER=$(date +%s%3N)
awk -v OFMT="%.3f" -v before=${BEFORE} -v after=${AFTER} 'BEGIN { print (after - before) / 1000 }'
}
elapsed_millisec sleep 1.234
@arc279
arc279 / urlparse.jq
Created June 28, 2022 08:51
urlparse for jq
def mergeHeader:
[
"scheme",
"user",
"password",
"domain",
"path",
"querystring",
"fragment"
] as $header
@arc279
arc279 / Cargo.toml
Last active February 3, 2022 09:56
generic serialize / deserialize
[package]
name = "generic"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
@arc279
arc279 / f.py
Created June 23, 2021 09:08
python で関数合成
from functools import reduce
def f_add(x):
def __add(y):
return x + y
return __add
def f_mul(x):
def __mul(y):
return x * y
@arc279
arc279 / Makefile
Created May 18, 2021 06:07
bash で日付とか時刻の連番とか
TODAY := $(shell date --iso-8601="seconds")
N := 7
from = $(shell date +'%Y-%m-%d %H:00:00' -d '${TODAY} $1 day ago 1 day ago')
to = $(shell date +'%Y-%m-%d %H:00:00' -d '${TODAY} $1 day ago')
define bt
@ echo "BETWEEN '${from}' AND '${to}'"
endef
@arc279
arc279 / Makefile
Last active April 28, 2021 06:22
ネストしたjsonをjq で辿る
# サンプルはこちらにある json
# https://docs.aws.amazon.com/ja_jp/step-functions/latest/dg/sample-map-state.html
run:
@ cat sample.asl.json | jq -f a.jq
@arc279
arc279 / Makefile
Created March 5, 2021 04:15
insert の順序で mysql の deadlock が発生するケースの検証
# cf. https://ichirin2501.hatenablog.com/entry/2015/12/24/164916
export MYSQL_PWD := db_pass
CLI := mysql -h 127.0.0.1 -P 3306 -u root -D hoge
Q1 := INSERT INTO player (name) VALUES \
("a"), ("b"), ("c"), ("d"), ("e"), ("f"), ("g"), ("h"), ("i"), ("j"), ("k"), ("l"), ("m"), ("n"), ("o"), ("p"), ("q"), ("r"), ("s"), ("t"), ("u"), ("v"), ("w"), ("x"), ("y"), ("z")
Q2 := INSERT INTO player (name) VALUES \
("z"), ("y"), ("x"), ("w"), ("v"), ("u"), ("t"), ("s"), ("r"), ("q"), ("p"), ("o"), ("n"), ("m"), ("l"), ("k"), ("j"), ("i"), ("h"), ("g"), ("f"), ("e"), ("d"), ("c"), ("b"), ("a")