Skip to content

Instantly share code, notes, and snippets.

value=(quad quit queue quote)
second_value=(queue queen)
# 先頭から最短マッチした部分を削除
echo ${value#q*e}
# quad quit ue
# 先頭から最長マッチした部分を削除
echo ${value##q*e}
# quad quit
@MikuroXina
MikuroXina / pairs.rs
Created September 9, 2022 12:48
Generate permutations on macro arguments in Rust.
macro_rules! pairs {
($macro:ident; $($items:literal),*) => {
pairs!(@ $macro; $($items),*; $($items),*);
};
(@ $macro:ident; $x:literal; $($ys:literal),*) => {
$(
$macro!($x, $ys);
)*
};
(@ $macro:ident; $x:literal, $($xs:literal),*; $($ys:literal),*) => {
@MikuroXina
MikuroXina / reducer.ts
Created August 15, 2022 08:55
The template for creating a custom reducer.
export interface State {
/* Type your state definition. */
}
export const initialState = (): State => ({
/* Type your initial state. */
});
const reducers = {
/* Type your reducer by key of the action name. */
@MikuroXina
MikuroXina / gen.ts
Last active September 4, 2022 02:45
Utility generator collection.
function* empty<T>(): Generator<T, void> {}
function* once<T>(elem: T): Generator<T, void> {
yield elem;
}
function* onceWith<T>(fn: () => T): Generator<T, void> {
yield fn();
}
@MikuroXina
MikuroXina / tropical.rs
Last active July 28, 2022 07:56
Tropical semi-ring definition in Rust.
use num::{One, Zero};
use std::ops;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Tropical {
Value(u64),
Infty,
}
impl Zero for Tropical {
@MikuroXina
MikuroXina / multi-curry.ts
Last active July 11, 2022 02:35
Currying manipulation for N-ary function.
type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false;
type Curried<F> = F extends (...args: infer A) => infer R
? Equal<F, () => R> extends true
? () => R
: (A extends [infer F, ...infer S]
? (arg: F) => Curried<(...rest: S) => R>
: R
)
: never;
@MikuroXina
MikuroXina / divisors.rs
Created April 24, 2022 06:01
Divisors enumerator
pub fn divisors(with: u64) -> Vec<u64> {
assert_ne!(with, 0, "cannot find divisors with 0");
let at_most = ((with as f64).sqrt() * 2.0).floor() as usize;
let mut divs = Vec::with_capacity(at_most);
for i in (1..)
.filter(|&i| with % i == 0)
.take_while(|&i| i * i <= with)
{
divs.push(i);
if i * i != with {
@MikuroXina
MikuroXina / extract-env.ts
Last active July 27, 2022 14:55
Extraction of environmental variables for Node.js.
/// 必要な環境変数名を可変長引数に渡すと, 環境変数ごとに対応した値が入ったオブジェクトを返す.
///
/// 対応した環境変数が定義されていない場合は `defaults` に設定した値へとフォールバックする.
///
/// # 例外
///
/// 対応した環境変数が定義されておらず `defaults` にも存在しない場合は例外を送出する.
///
/// # 使用例
///
@MikuroXina
MikuroXina / binary_search.rs
Last active September 9, 2022 12:58
Binary-search with given function in Rust.
use std::cmp::Ordering;
pub fn binary_search(max: usize, mut f: impl FnMut(usize) -> Ordering) -> Result<usize, usize> {
let mut left = 0;
let mut right = max;
while left < right {
let mid = left + (right - left) / 2;
match f(mid) {
Ordering::Less => left = mid + 1,
Ordering::Greater => right = mid - 1,
@MikuroXina
MikuroXina / ani2ico.rs
Last active October 26, 2022 12:13
The decomposer, converting the ani file to ico files.
use std::{env, fs, io::Write, path::PathBuf};
const ICON_MAGIC: &[u8] = b"icon";
fn next_partition(buf: &[u8]) -> Option<usize> {
buf.windows(ICON_MAGIC.len())
.position(|win| win == ICON_MAGIC)
}
fn hoge() -> Result<(), Box<dyn std::error::Error + 'static>> {