Skip to content

Instantly share code, notes, and snippets.

@MikuroXina
MikuroXina / ceil_sqrt.rs
Created May 11, 2025 18:26
Finds the ceiling value of square root with Rust.
/// Finds the ceiling value of square root of `n`.
pub fn ceil_sqrt(n: i64) -> i64 {
let mut base = (n as f64).sqrt() as i64 - 1;
while (base + 1) * (base + 1) <= n {
base += 1;
}
base
}
@MikuroXina
MikuroXina / parser.hs
Last active April 28, 2025 15:30
An implementation of parser combinator with Haskell.
{-# LANGUAGE LambdaCase #-}
{-# OPTIONS_GHC -Wno-name-shadowing #-}
module Lib (
(+++),
chainL,
chainR,
char,
delimited,
integer,
@MikuroXina
MikuroXina / interpreter.hs
Last active April 28, 2025 00:54
An AST interpreter example for language with scoped variables and addition, subtraction, logical and/or.
import Control.Monad.State.Lazy
import qualified Data.Map.Strict as Map
import Data.Maybe
data Op = Plus | Minus | And | Or deriving (Show)
newtype VarName = VarName (String) deriving (Show, Ord, Eq)
data Expr
= Var (VarName)
@MikuroXina
MikuroXina / sized_array.ts
Created March 4, 2025 06:58
A Fixed-size Array type for TypeScript.
export type SizedArray<N extends number, T> = SizedArrayInner<N, T, []>;
type SizedArrayInner<
N extends number,
T,
Acc extends unknown[],
> = N extends PositiveInteger<N>
? number extends N
? T[]
: N extends Acc["length"]
@MikuroXina
MikuroXina / peace_for_all.go
Last active March 1, 2025 02:12
The (unofficial) completed Golang code written in an UNIQLO Akamai PEACE FOR ALL T-shirt.
package main
import (
"fmt"
"html"
"log"
"net/http"
"strconv"
"strings"
"time"
@MikuroXina
MikuroXina / fibonacci_section.rs
Created February 23, 2025 11:41
An implementation of Fibonacci-section method to find the maximum of a unimodal sequence.
/// Searches the maximum value of `query` in section between `lower` (exclusive) and `upper` (exclusive).
fn fibonacci_section(lower: usize, upper: usize, mut query: impl FnMut(usize) -> u32) -> u32 {
let n = upper - lower - 1;
if n == 1 {
return query(1);
}
let mut fib = vec![1, 2];
for i in 2.. {
let next = fib[i - 2] + fib[i - 1];
if next > n {
@MikuroXina
MikuroXina / mine_sweeper.rs
Last active February 10, 2025 15:49
An object-oriented mine sweeper implementation with Rust.
/// Design reference: https://zenn.dev/yuhi_junior/articles/062cf4f30b083d
use itertools::Itertools;
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Cell {
has_bomb: bool,
flagged: bool,
@MikuroXina
MikuroXina / round.rs
Created November 25, 2024 08:15
Emulations of rounding modes with Rust.
#![feature(float_next_up_down)]
//! Source: http://verifiedby.me/adiary/pub/kashi/image/201406/nas2014.pdf
/// Floating point number with mathematical error.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct F64E {
/// An actual result by the default rounding mode.
base: f64,
/// Mathematical error for an ideal result.
@MikuroXina
MikuroXina / crc.rs
Created November 6, 2024 20:32
Example implementation of CRC-32 with Rust.
#[derive(Debug, Clone)]
pub struct Crc32 {
table: [u32; 256],
}
impl Crc32 {
pub const SOURCE: u32 = 0xedb8_8320;
pub fn new() -> Self {
let mut table = [0u32; 256];
@MikuroXina
MikuroXina / algo_x.rs
Last active October 11, 2024 10:50
An implementation of Knuth's Algorithm X and Dancing Linked List with Rust.
pub fn algorithm_x(
list: &mut DancingLinkedList,
stack: &mut Vec<usize>,
solutions: &mut Vec<Vec<usize>>,
) {
if list.is_empty() {
solutions.push(stack.clone());
return;
}