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
View FileSystem.hs
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# OPTIONS_GHC -Wall #-}
module FileSystem where
import Control.Applicative
import Control.Monad
import Control.Monad.State
import Data.Bifunctor
friedbrice / Escardo.hs
Last active October 21, 2022 02:24
Looking for a Needle That Might Not Be in an Infinite Haystack
View Escardo.hs
-- Based on "Infinite sets that admit fast exhaustive search" by Martín Escardó
View Algebra.hs
{-# OPTIONS_GHC -Wall -fno-warn-orphans -fno-warn-unused-top-binds -fno-warn-name-shadowing #-}
{-# LANGUAGE FunctionalDependencies, OverloadedStrings #-}
import Data.List hiding (singleton)
import Data.Map
import Data.Semigroup
import Data.String
import GHC.Num.Integer qualified
import GHC.Real qualified
import Numeric.Natural
friedbrice / main.hs
Created July 18, 2022 17:40 — forked from itsuart/main.hs
Hello world Yesod example.
View main.hs
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings #-}
import Yesod
import Yesod.Core.Handler
data HelloWorld = HelloWorld
mkYesod "HelloWorld" [parseRoutes|
/ HomeR GET
friedbrice / App.hs
Last active May 13, 2022 05:29
Zero-parameter type classes as a dependency injection framework. (Don't ever do this in real life.)
View App.hs
module Yolo.App where
import Yolo.Capabilities
app :: (Console, Database, Exception, Logging) => IO ()
app = do
x1 <- loggingDivision 6 2
x2 <- loggingDivision 5 0
x3 <- consoleDivision
View Memoize.hs
{-# 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 / fibonacci.hs
Last active August 14, 2021 01:27
Constant-memory, log-time fibonacci numbers.
View fibonacci.hs
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 / 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.
View iterm_open_with
# 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
friedbrice /
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 / basic-type-level-programming.hs
Last active November 22, 2020 07:12
Basic Type-level Programming
View basic-type-level-programming.hs
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE NoStarIsType #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Main where