This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| value=(quad quit queue quote) | |
| second_value=(queue queen) | |
| # 先頭から最短マッチした部分を削除 | |
| echo ${value#q*e} | |
| # quad quit ue | |
| # 先頭から最長マッチした部分を削除 | |
| echo ${value##q*e} | |
| # quad quit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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),*) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// 必要な環境変数名を可変長引数に渡すと, 環境変数ごとに対応した値が入ったオブジェクトを返す. | |
| /// | |
| /// 対応した環境変数が定義されていない場合は `defaults` に設定した値へとフォールバックする. | |
| /// | |
| /// # 例外 | |
| /// | |
| /// 対応した環境変数が定義されておらず `defaults` にも存在しない場合は例外を送出する. | |
| /// | |
| /// # 使用例 | |
| /// |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>> { |