Skip to content

Instantly share code, notes, and snippets.

@johesoman
johesoman / run_one_test
Last active April 30, 2020 08:28
Run just one test with shadow-cljs. I put `test_runner.cljs` in the directory `dev`.
#!/usr/bin/env bash
npx shadow-cljs compile run-one-test && node ./target/run-one-test.js $@
@johesoman
johesoman / route.clj
Last active August 30, 2019 13:35
A Compojure-inspired library for routing in HTTP APIs.
(ns route.core
(:require [clojure.string :as string]))
(defn map-vals [f m]
(into {} (map (fn [[k v]] [k (f v)]) m)))
(defn split-path [path]
(let [sub-paths (string/split path #"/")]
(if (= (first sub-paths) "")
(rest sub-paths)
@johesoman
johesoman / .gitignore - Node.js
Last active July 30, 2019 10:01
.gitignore for Node.js projects.
# node
node_modules
build
# VS Code
.vscode
# Mac
.DS_Store
@johesoman
johesoman / RecursiveBacktrackerMazeGenerator.fs
Created June 10, 2019 22:11
An implementation of the recursive backtracker maze generator algorithm. The API allows users to specify the probability for replacing a '.' with a '#', and generate potentially unsolvable mazes. Setting the probability to 0 guarantees that the generated maze is solvable.
module RecursiveBacktrackerMazeGenerator
// ++++++++++
// + Random +
// ++++++++++
module Random =
let _rng = new System.Random()
let nextInt lo hi = _rng.Next(lo, hi)
@johesoman
johesoman / levenshtein_evaluator.rs
Created June 10, 2019 21:40
Given words S and T, the program prints the shortest sequence of edits necessary to change S into T.
use std::iter::FromIterator;
use std::iter::Iterator;
use std::cmp::min;
use std::cmp::Ord;
#[derive(Debug)]
#[derive(Clone)]
pub enum Edit{
Delete,
Insert,
@johesoman
johesoman / quick_sort.rs
Created May 26, 2019 12:27
A small Quick Sort implementation showing of Rust's expressiveness.
fn partition<T, F>(elems: &mut [T], pred: F ) -> usize
where F: Fn(&T) -> bool
{
let mut i = 0;
for j in 0 .. elems.len(){
if pred(&elems[j]) {
elems.swap(i, j);
i += 1;
}
@johesoman
johesoman / SuffixArray.fs
Created May 26, 2019 11:00
An implementation a suffix array and an LCP array. It provides a fast way to count the number of unique substrings of a string.
module SuffixArray
open System
// ++++++++
// + util +
// ++++++++
let time f =
let t = new Diagnostics.Stopwatch()
@johesoman
johesoman / StreamFizzBuzz.fs
Last active May 26, 2019 09:47
A stream-based solution to the Fizz-Buzz problem.
module StreamFizzBuzz
// Infinite stream of positive integers starting at 1.
let nums = Seq.initInfinite ((+) 1)
// Infinite stream of boolean literals, every fifth literal is true.
let buzzes = seq {while true do yield! [false; false; false; false; true]}
// Infinite stream of boolean literals, every third literal is true.
let fizzes = seq {while true do yield! [false; false; true]}
@johesoman
johesoman / competitive.rs
Last active April 8, 2019 18:16
A skeleton for using Rust in online programming challenges - complete with fast IO and macros for parsing input.
use std::fs::File;
use std::fmt::Write as W;
use std::os::unix::io::{FromRawFd, IntoRawFd};
use std::io::{BufRead, BufReader, BufWriter, Write};
// ++++++++++++++
// + IO helpers +
// ++++++++++++++
macro_rules! scan {