Skip to content

Instantly share code, notes, and snippets.

View friedbrice's full-sized avatar
🔵
You have unread notifications

Daniel P. Brice friedbrice

🔵
You have unread notifications
View GitHub Profile
{-# LANGUAGE DerivingVia #-}
-- | Basic memoization.
--
-- Functions yielded by 'memoize' and 'memoizeRec' may continue to allocate
-- memory without bound as long as they remain in scope. That is, you can keep
-- them around in a single short-lived thread, such as responding to an HTTP
-- request, but if kept at top-level will cause memory leaks. Use 'runMemRec'
-- to free memory as soon as the result is computed (i.e. forced).
module Memoize (Mem, memoize, MemRec, memoizeRec, runMemRec) where
@friedbrice
friedbrice / fibonacci.hs
Last active August 14, 2021 01:27
Constant-memory, log-time fibonacci numbers.
import Data.Semigroup
data Fib =
Fib Integer Integer Integer
instance Semigroup Fib where
Fib a1 b1 c1 <> Fib a2 b2 c2 =
Fib (a1*a2 + b1*b2) (a1*b2 + b1*c2) (b1*b2 + c1*c2)
stimes =
@friedbrice
friedbrice / iterm_open_with
Created June 26, 2021 01:19 — forked from trinitronx/iterm_open_with
Semantic history command for iTerm2 and Sublime Text 3. Allows iTerm integration of Command+Click to open a file in default app (if non-text), or Sublime Text with optional line number and column. Detects relative paths based on PWD.
#!/bin/sh
# iterm_open_with - open a URL, file from CWD, full path, or path with linenumber in default app or Sublime Text if text file
# For usage with iTerm2:
# In iTerm's Preferences > Profiles > Default > Advanced > Semantic History,
# choose "Run command..." and enter "/your/path/to/iterm_open_with \5 \1 \2".
# Usage: iterm_open_with $(pwd) filename [linenumber]
# $(pwd) = current working directory (either use `pwd` or $PWD)
# filename = filename to open
# lineno = line number
pwd=$1
@friedbrice
friedbrice / Visitor.java
Last active March 4, 2021 03:37
The visitor pattern is essentially the same thing as Church encoding
public final class Visitor {
/** Shape
*
* We wish to define a data type with exactly two variants: Circle and Rectangle.
*
* In particular, we do not want Shape to be open to extension by
* new kinds of variants (as would be the case with an abstract class).
* The reason we do not want the variants of Shape to be open to extension
* is because this will grant us the ability to extend the operations
@friedbrice
friedbrice / basic-type-level-programming.hs
Last active November 22, 2020 07:12
Basic Type-level Programming
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE NoStarIsType #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Main where
-- This is some boilerplate code, don't worry about it.
{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
main = return ()
-- ** Start here for Interlude 1. **
-- I. Types
type Address = {
street: string
zipCode: string
}
type Contact = {
name: string
phoneNumber: string
email: string
}
{-# LANGUAGE DataKinds, KindSignatures, GADTs, TypeOperators #-}
module BlockMatrix where
import GHC.TypeLits
data Block (n :: Nat) (m :: Nat) a where
Number :: a -> Block 1 1 a
Fourths :: Block n1 m1 a -> Block n1 m2 a -> Block n2 m1 a -> Block n2 m2 a -> Block (n1 + n2) (m1 + m2) a
Vhalves :: Block n m1 a -> Block n m2 a -> Block n (m1 + m2) a
Hhalves :: Block n1 m a -> Block n2 m a -> Block (n1 + n2) m a
@friedbrice
friedbrice / Haskell (Cabal).sublime-syntax
Created July 28, 2020 16:42
Rudimentary Sublime Text Config for Haskell
%YAML 1.2
---
# See http://www.sublimetext.com/docs/3/syntax.html
name: Haskell (Cabal)
file_extensions:
- cabal
scope: source.cabal
variables:
ident: '[a-zA-Z\_][a-zA-Z\_\-0-9]*'
@friedbrice
friedbrice / MOVED.md
Last active July 27, 2020 06:02
Stack wrapper for single-file haskell programs.