Skip to content

Instantly share code, notes, and snippets.

@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)]
@monadplus
monadplus / ghc_options.md
Last active September 5, 2022 10:16
Haskell: ghc-options

Cabal: ghc-options

  • ghc-options specified in *.cabal apply to LOCAL packages.

  • If you want your ghc-options to be applied to ALL packages (including dependencies), you need to create a top-level cabal.project:

    packages: .
    
    package *
    

ghc-options: -O2

@monadplus
monadplus / Measure.hs
Created August 10, 2022 17:26
Haskell: be careful when measuring elapsed time
-- | >>> measure (return $ sum [1..100000000])
-- (5000000050000000,1.892e-6)
measure :: IO a -> IO (a, Double)
measure io = do
start <- getMonotonicTimeNSec
a <- io
end <- getMonotonicTimeNSec
return (a, fromIntegral (end - start) / 1_000_000_000)
-- | >>> measure (return $ sum [1..100000000])