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 #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# OPTIONS_GHC -Wall #-}
module FileSystem where
import Control.Applicative
import Control.Monad
import Control.Monad.State
import Data.Bifunctor
@friedbrice
friedbrice / Escardo.hs
Last active October 21, 2022 02:24
Looking for a Needle That Might Not Be in an Infinite Haystack
-- Based on "Infinite sets that admit fast exhaustive search" by Martín Escardó
--
--
--
--
--
--
--
--
--
{-# 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
friedbrice / main.hs
Created July 18, 2022 17:40 — forked from itsuart/main.hs
Hello world Yesod example.
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings #-}
import Yesod
import Yesod.Core.Handler
data HelloWorld = HelloWorld
mkYesod "HelloWorld" [parseRoutes|
/ HomeR GET
|]
@friedbrice
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.)
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
{-# 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