Skip to content

Instantly share code, notes, and snippets.

View BartMassey's full-sized avatar

Bart Massey BartMassey

View GitHub Profile
-- 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 / 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)
@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 / 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) {
#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 / scope.py
Last active May 26, 2019 09:02
Python scoping rules demo
a = 'a'
def f1():
global a
a = 'af1'
f1()
# prints af1
print(a)
@BartMassey
BartMassey / option.js
Last active July 26, 2019 21:37
"Option type" for Javascript
"use strict";
class Some {
constructor(value) {
this.value = value;
}
is_some(self) {
return true;
}
#![deny(unused)]
use std::io::{self, Read};
#[derive(PartialEq, Copy, Clone)]
enum Tile {
None,
Wall,
Goal,
}
// Copyright © 2019 Jeff Austin, Kamakshi Nagar
// [This program is licensed under the "MIT License"]
// Please see the file LICENSE in the source
// distribution of this software for license terms.
/*
citations:
Mark Morrissey --CS333 Operating Systems--Portland State University practice exams:
https://web.cecs.pdx.edu/~markem/CS333/exams/Final%202019-01.pdf
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::io::Read;
#[derive(Serialize, Deserialize)]
#[serde(rename_all="camelCase")]
struct Item {
transfer_status : u64,
dismantle_permission : u64,