Skip to content

Instantly share code, notes, and snippets.

@monadplus
monadplus / rename.sh
Created September 11, 2024 18:11
Rename files using a pattern
#!/bin/sh
for f in *mkv; do
source="$f"
target=$(echo "$f" | sed -r 's/Modern Family S([[:digit:]]+)E([[:digit:]]+)[^.]*.mkv/Modern Family S\1E\2.mkv/')
echo "$source => $target"
mv "$source" "$target"
done
@monadplus
monadplus / void.hs
Created August 16, 2024 14:35
Data.Void
{-# LANGUAGE BangPatterns #-}
module Main where
import Data.Void
-- We still need to pattern-match against Left because
-- laziness allows that branch to still be valid even if Void is unhabited
-- i.e. it could still be populated by bottom values.
f :: Either Void Int -> Int
@monadplus
monadplus / IxMonad.hs
Created May 18, 2023 20:01
Haskell: indexed monad example
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RebindableSyntax #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module IxMonad where
[
{
"backcolor": "#ffffff",
"name": "keyboard.io Model 01 - james's layout, based on algernon's Dvorak version",
"author": "James Cash <james.cash@occasionallycogent.com>",
"switchMount": "alps",
"switchBrand": "matias",
"switchType": "PG155B01",
"pcb": false,
"plate": true
@monadplus
monadplus / json_macro.rs
Created November 21, 2022 22:02
Rust: json macro
use std::collections::BTreeMap;
trait ToValue {
fn to_value(self) -> Value;
}
impl ToValue for bool {
fn to_value(self) -> Value {
Value::Bool(self)
}
@monadplus
monadplus / unordered_futures.rs
Created November 16, 2022 21:51
Rust: UnorderedFutures
use futures::{stream::FuturesUnordered, StreamExt};
async fn do_something(i: u8) -> String {
format! {"Future {i:#b}"}
}
#[tokio::main]
async fn main() {
let v = std::sync::Arc::new(tokio::sync::Mutex::new(vec![]));
(0..100)
@monadplus
monadplus / unwind_safe.rs
Created November 2, 2022 21:24
Rust: UnwindSafe
//! https://doc.rust-lang.org/std/panic/fn.catch_unwind.html
use std::panic::UnwindSafe;
use rand::random;
struct Boxed<'a, T> {
inner: &'a mut [T],
}
impl<'a, T> UnwindSafe for Boxed<'a, T> {}
@monadplus
monadplus / gats.rs
Created November 2, 2022 13:02
Rust: exploring GATs
use std::error::Error;
struct WindowsMut<'t, T> {
slice: &'t mut [T],
start: usize,
window_size: usize,
}
trait LendingIterator {
type Item<'a>
@monadplus
monadplus / palindrome.rs
Last active October 28, 2022 14:41
Rust: is palindrome
use std::{
error::Error,
fs::File,
io::{self, BufRead},
};
use clap::{ArgGroup, Parser};
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]