Skip to content

Instantly share code, notes, and snippets.

@msakuta
msakuta / main.c
Created April 20, 2024 17:09
ctula
#include <stdio.h>
struct Rule {
int state;
int read;
int write;
int next;
char direction;
};
@msakuta
msakuta / rocket.rs
Created March 10, 2024 17:02
An example code for switching modes by generic type argument
use anyhow::Result;
#[derive(Debug)]
struct Tag;
#[derive(Debug)]
struct Launched;
#[derive(Debug)]
struct Rocket<Stage = Tag> {
color: &'static str,
speed: i32,
@msakuta
msakuta / epoll.c
Last active October 28, 2023 03:02
A simple epoll demonstration for non-blocking I/O with a single thread
#include <sys/epoll.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
@msakuta
msakuta / autograd.hs
Created August 17, 2023 00:04
Automatic differentiation in Haskell
data Term a = Value Int a | Add (Term a) (Term a) | Mul (Term a) (Term a)
-- deriving (Show)
(.+) :: Term a -> Term a -> Term a
(.+) a b = Add a b
(.*) :: (Num a) => Term a -> Term a -> Term a
(.*) a b = Mul a b
@msakuta
msakuta / array_cache.rs
Created March 4, 2023 14:54
Array cache effect measurement in Rust
const REPEAT: usize = 10;
const N: usize = 1024 * 16;
fn main() {
let mut array = vec![0; N * N];
let mut row = 0.;
let mut column = 0.;
for _ in 0..REPEAT {
package main
import (
"image"
"image/color"
"image/png"
"log"
"os"
// "fmt"
// "strings"
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
typedef unsigned char byte;
# echo "What's your name? "
# var name: string = readLine(stdin)
# echo "Hi, ", name, "!"
from pixie import newImage, writeFile, `[]=`, color
from math import pow
from std/times import cpuTime
proc f(i: var int) =
i = i * 2
use geos::Geom;
use anyhow::{Context, Result};
use std::io::Write;
fn main() {
fn try_it() -> Result<()> {
let gg1 = geos::Geometry::new_from_wkt("POLYGON ((0 0, 0 5, 6 6, 6 0, 0 0))")?;
let gg2 = geos::Geometry::new_from_wkt("POLYGON (((1 1, 1 3, 5 5, 5 1, 1 1))")
.with_context(|| format!("Failed to parse WKT"))?;
@msakuta
msakuta / thread_pool.cpp
Last active September 9, 2021 15:55
an implementation of work-stealing thread pool
// g++ thread_pool.cpp -lpthread -othread_pool
#include <thread>
#include <mutex>
#include <condition_variable>
#include <vector>
#include <functional>
#include <memory>
#include <iostream>
#include <sstream>