Skip to content

Instantly share code, notes, and snippets.

@kamoshi
kamoshi / aoc-2023-04-pointfree.hs
Created February 8, 2024 19:10
Advent of Code 2023 04, but it's pointfree
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
module Day04 (parse, solveA, solveB) where
import Data.Void (Void)
import Data.Text (Text)
import Data.Bits (shiftL, shiftR)
import Data.Bifunctor (first)
import Text.Megaparsec (Parsec, many, eof, runParser, errorBundlePretty)
import Text.Megaparsec.Char (string, space, char)
@kamoshi
kamoshi / applicatives.js
Created September 4, 2023 18:25
Applicative Maybe example in JavaScript
const Nothing = { kind: "Nothing" };
const Just = (val) => ({ kind: "Just", val: () => val });
function pure(val) {
return Just(val);
}
function fmap(f, x) {
return (x.kind == "Nothing")
? Nothing
@kamoshi
kamoshi / typelevel-props.ts
Created August 29, 2023 17:39
Typecheck nested props
/**
* Extract nested type from interface or object type.
*
* Example usage:
* ```ts
* type result = CheckNested<["a", "b"], {a: {b: number}}>;
* ```
*/
type ExtractNestedType<
Props extends any[],
use std::error::Error;
use clap::Parser;
use futures::StreamExt;
use reqwest::Client;
#[derive(clap::Parser)]
struct Cli {
#[arg(short, long, default_value_t = 1)]
count: u32,
}
@kamoshi
kamoshi / debugger.js
Created February 12, 2023 00:39
JS debugger loop hack
function H(n) {
function e(t) {
if ("string" == typeof t) return function(t){}.constructor("while (true) {}").apply("counter");
(t % 20 == 0)
? function() { return !0 }.constructor("debugger").call("action")
: function() { return !1 }.constructor("debugger").apply("stateObject"),
e(++t)
}
@kamoshi
kamoshi / str_length_hack.rs
Created December 21, 2022 16:14
Overwrite string length in a fat pointer via /proc on Unix
fn main() {
use std::fs::OpenOptions;
use std::io::{Seek, SeekFrom, Write};
let foo = "hello";
let fat_ptr_ptr = &foo as *const _ as u64;
println!("{fat_ptr_ptr}");
println!("before = {foo}");
@kamoshi
kamoshi / scala.hs
Created July 5, 2022 12:51
Haskell solutions to Scala exercises
-- Lista 0
last' :: [a] -> Maybe a
last' [] = Nothing
last' [x] = Just x
last' (_:xs) = last' xs
-- Lista 1
suma :: [Double] -> Double
suma [] = 0
suma (x:xs) = x + suma xs
@kamoshi
kamoshi / main.py
Created June 16, 2022 16:29
Sort Touhou characters in Tohosort alphabetically
from selenium import webdriver
from selenium.webdriver.common.by import By
if __name__ == '__main__':
driver = webdriver.Firefox(executable_path="./geckodriver")
driver.get("https://tohosort.frelia.my/")
driver.find_element(By.CSS_SELECTOR, ".starting.start.button").click()
while True:
@kamoshi
kamoshi / wipe_messages.py
Created June 2, 2021 14:35
Wipe discord messages with a self bot (kinda illegal)
import discord
from discord.ext import commands
bot = commands.Bot(">>>", self_bot=True)
@bot.event
async def on_ready():
print("Bot presence t u r n e d on ( ͡° ͜ʖ ͡°)")
@bot.command()
@kamoshi
kamoshi / test.cpp
Created December 13, 2019 15:48
io
char buffer[12];
while (fgets(buffer, sizeof buffer, input) != NULL)
{
std::string str = "";
char* ptr = buffer;
while (*ptr != NULL)
{
str += *ptr++;
}
std::cout << str;