Skip to content

Instantly share code, notes, and snippets.

@brosenan
brosenan / functional.cpp
Created September 8, 2016 07:21
C++ is a Dynamic, Pure, Functional Programming Language
#include <iostream>
template <typename T>
struct Print {
static void print() {
std::cout << T::val;
}
};
template <int N>
@brosenan
brosenan / state-monad.clj
Created March 3, 2017 14:50
State Monad in Clojure
(ns foo)
(defn m-const [val]
(fn [state]
[state val]))
(defn m-do [& actions]
(if (= (count actions) 1)
(first actions)
; else

Keybase proof

I hereby claim:

  • I am brosenan on github.
  • I am brosenan (https://keybase.io/brosenan) on keybase.
  • I have a public key ASDsLHzZo2z937kJK2kzjxZNPW9xy3kERbjREvrHqKXw4wo

To claim this, I am signing this object:

@brosenan
brosenan / tree.rs
Last active January 24, 2018 13:24
use std::mem;
type TreeRef<K, V> = Option<Box<Tree<K, V>>>;
struct Tree<K: PartialOrd, V> {
key: K,
val: V,
left: TreeRef<K, V>,
right: TreeRef<K, V>
}
@brosenan
brosenan / linear-types.clj
Created October 19, 2018 05:59
Example code in an imaginary, linearly-typed functional programming language.
;; This is not really Clojure code. I used the .clj extention since it provides the closests syntax highlighting to this imaginary language.
(defabst constantly [?t] (-> ?t (-> ?t))
(fn [c]
(fn []
(copy c))))
(deftype List [?t]
(::nil)
(::item ?t (List ?t)))
use std::collections::HashMap;
use std::hash::Hash;
struct Memoize<F, T1, T2> where F: Fn(&T1) -> T2 {
cache: HashMap<T1, T2>,
func: F,
}
impl<F, T1, T2> Memoize<F, T1, T2> where F: Fn(&T1) -> T2, T1: Eq + Hash, T2: Clone {
fn new(func: F) -> Memoize<F, T1, T2> {
#[derive(Debug)]
struct List<T> {
elem: T,
next: Option<Box<List<T>>>,
}
#[derive(Debug)]
struct Queue<T> {
tail: Option<Box<List<T>>>,
head: *mut Option<Box<List<T>>>,
use std::ops::{Mul, Add};
struct Parser {
parser: Box<Fn(&[u8], usize) -> Vec<usize>>,
}
fn empty() -> Parser {
Parser{parser: Box::new(|_s: &[u8], idx| vec![idx])}
}
use std::collections::HashMap;
use std::sync::Arc;
#[derive(Clone)]
pub enum Value {
Num(f64),
Str(String),
Struct(HashMap<String, Arc<Value>>),
}
@brosenan
brosenan / main.dart
Created August 2, 2019 10:34
A "Game of Sorts"
import 'package:flutter/material.dart';
import 'dart:math';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(