Skip to content

Instantly share code, notes, and snippets.

View reu's full-sized avatar

Rodrigo Navarro reu

View GitHub Profile
@reu
reu / rational.ts
Created July 1, 2024 11:07
Quick and dirty rational numbers in TS
export type Rational = {
numerator: number;
denominator: number;
simplify: () => Rational;
add: (other: Rational) => Rational;
sub: (other: Rational) => Rational;
mul: (other: Rational) => Rational;
div: (other: Rational) => Rational;
valueOf: () => number;
toString: () => string;
@reu
reu / lfu.rs
Last active May 29, 2024 00:42
Rust LFU Leetcode
use std::{
borrow::Borrow,
collections::{BTreeSet, HashMap},
hash::Hash,
};
pub struct LFUCache(Lfu<i32, i32>);
// This is the Leetcode API we have to implement
impl LFUCache {
@reu
reu / proxy-non-blocking.rs
Last active December 16, 2023 13:44
Simple TCP proxy using thread per connection and non blocking IO
use std::{
io::{self, Read, Write},
net::{TcpListener, TcpStream},
os::fd::{AsRawFd, RawFd},
thread,
};
const READ: libc::c_short = libc::POLLRDNORM;
const WRITE: libc::c_short = libc::POLLWRNORM;
@reu
reu / tmux.conf
Created September 10, 2023 18:03
set -g default-terminal "screen-256color"
set -g terminal-overrides 'xterm*:smcup@:rmcup@'
set-option -sa terminal-overrides ",xterm*:Tc"
set -s escape-time 10
setw -g xterm-keys on
set -g mouse on
setw -g mode-keys vi
unbind -n -Tcopy-mode-vi MouseDragEnd1Pane
set -g base-index 1
@reu
reu / expression.ts
Last active August 21, 2023 15:21
Expression parser using combinators
import {
any,
char,
delimited,
int,
many0,
map,
multispace0,
nat,
parse,
@reu
reu / sig.rb
Created July 12, 2023 17:24
Ruby reactive signal implementation
require "set"
class Sig
def initialize(&computation)
@observers = Set.new
@dependencies = Set.new
@computation = computation || proc { nil }
compute if block_given?
end
@reu
reu / date.rs
Last active March 15, 2023 14:02
Operator overloading for building dates in Rust (don't, just don´t)
use std::ops::{Div, Sub};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Month {
Jan = 1,
Feb = 2,
Mar = 3,
Apr = 4,
Mai = 5,
Jun = 6,
@reu
reu / anymap.rs
Created April 12, 2022 22:15
Rust AnyMap
use std::any::{Any, TypeId};
use std::collections::HashMap;
#[derive(Default)]
pub struct AnyMap {
map: HashMap<TypeId, Box<dyn Any>>,
}
impl AnyMap {
pub fn insert<T: 'static>(&mut self, t: T) {
@reu
reu / linked-list.rs
Created March 24, 2022 12:25
Bad Rust linked cons list
use std::mem;
#[derive(Debug, PartialEq)]
pub enum List<T> {
Empty,
Cons(T, Box<List<T>>),
}
impl<T> List<T> {
pub fn new() -> Self {
@reu
reu / promises-iter.js
Created February 17, 2022 12:22
Async iterator that yields each promises results in resolve order
async function* promisesIter(promises) {
promises = [...promises];
const buffer = [];
for (promise of promises) {
promise.then(result => {
promises.splice(promises.indexOf(promise), 1);
buffer.push(result);
});
}
while (promises.length === 0 || await Promise.race(promises).then(() => true)) {