Skip to content

Instantly share code, notes, and snippets.

View BartMassey's full-sized avatar

Bart Massey BartMassey

View GitHub Profile
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
inline static void
incref(int32_t *refp) {
asm volatile("addl $1, (%0)" : : "r" (refp) : "memory");
}
inline static void
@BartMassey
BartMassey / soln.js
Last active December 11, 2018 11:19
Solution to Advent of Code 2018 Day 11 in Javascript
// Copyright © 2018 Bart Massey
// This program is licensed under the "MIT License".
// Please see the file LICENSE in this distribution
// for license terms.
// Advent of Code Day 11.
"use strict";
function power_level(x, y, gsn) {
@BartMassey
BartMassey / badprime.rs
Last active November 26, 2018 10:33
Various prime number implementations, including a buggy one
// https://www.reddit.com/r/rust/comments/a0g730/new_to_rust_discouraged_by_slowrunning_code/
fn nth_prime_bad(initial: u64) -> u64 {
let mut prime_count = 0;
let mut curr = 2;
let mut prime = None;
while prime_count < initial {
let mut denom = 2;
while denom < curr {
if curr % denom == 0 {
@BartMassey
BartMassey / nzbin.v
Created November 2, 2018 04:13
Proof of binary ←→ nat interconvertibility
Inductive nzbin : Type :=
| O
| A (b: nzbin)
| B (b: nzbin).
Fixpoint nzincr (b : nzbin) : nzbin :=
match b with
| O => A O
| A c => B c
| B c => A (nzincr c)
-- Modified from https://www.snoyman.com/blog/2018/10/raii-better-than-bracket-pattern
import Control.Exception (bracket)
import Text.Printf (printf)
data MyResource = MyResource { handle :: Int }
newMyResource :: IO MyResource
newMyResource = do
putStrLn "Creating a new MyResource"
@BartMassey
BartMassey / gc.py
Created May 17, 2018 19:27
Demonstration of need for garbage collection in Python
# Copyright (c) 2018 Bart Massey
# GC demo.
# Create a circular structure. The reference
# count of x and y would be 2: one for the
# variable and 1 for the internal reference.
x = [1]
y = [2]
y[0] = x
@BartMassey
BartMassey / swagger-params.json
Created May 10, 2018 20:28
Swagger 2.0 complex parameter demo
{
"consumes": [
"application/json"
],
"swagger": "2.0",
"basePath": "/",
"schemes": [
"https"
],
"produces": [
@BartMassey
BartMassey / deck.rs
Created May 10, 2018 00:47
Deck of cards in Rust
// Copyright © 2018 Bart Massey
// Simple deck of cards using enum_derive.
#[macro_use] extern crate custom_derive;
#[macro_use] extern crate enum_derive;
custom_derive! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, IterVariants(Suits))]
-- Copyright © 2017 Bart Massey
-- | Illustrate the FTP problem in a realistic way.
import Data.List
import Data.Maybe
-- | Given a list of values `xs`, return a list of tuples
-- consisting of a list of `n` copies of a given value and a
-- count of its occurrences, ordered by value. Setting `n`
@BartMassey
BartMassey / memsafe.rs
Created February 4, 2018 03:28
Memory safety demo in Rust by Will Chrichton. Compile with `rustc -o memsafe memsafe.rs`
// http://willcrichton.net/notes/rust-memory-safety/
// Modified by Bart Massey 2018-02-03
#![feature(alloc, allocator_api)]
extern crate alloc;
use std::slice;
use std::heap::{Heap, Alloc};
use alloc::allocator::Layout;