Skip to content

Instantly share code, notes, and snippets.

View ChrisPenner's full-sized avatar
:bowtie:
Happily Hacking

Chris Penner ChrisPenner

:bowtie:
Happily Hacking
View GitHub Profile
@ChrisPenner
ChrisPenner / Optics Cheatsheet.md
Last active April 12, 2024 14:24
Optics Cheatsheet
@ChrisPenner
ChrisPenner / Battleship.lhs
Last active September 10, 2023 03:12
Hit! You sunk my Adjunction!
Today we'll be looking into Kmett's
[adjunctions](http://hackage.haskell.org/package/adjunctions) library,
particularly the meat of the library in Data.Functor.Adjunction.
This post is a literate haskell file, which means you can load it right up in
ghci and play around with it! Like any good haskell file we need half a dozen
language pragmas and imports before we get started.
> {-# language DeriveFunctor #-}
> {-# language TypeFamilies #-}
def factorial(n):
if n == 0: return 1
else: return factorial(n-1) * n
def tail_factorial(n, accumulator=1):
if n == 0: return accumulator
else: return tail_factorial(n-1, accumulator * n)
@ChrisPenner
ChrisPenner / DynamicBFS.hs
Created May 12, 2022 18:38
Effectful, lazy, BFS using LogicT.
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module BFS where
import Control.Applicative
import Control.Monad.Logic
import Control.Monad.Reader
@ChrisPenner
ChrisPenner / Tape.lhs
Last active January 1, 2023 13:09
Building up Zippers from Distributive, Representable, and Cofree
We're going to take a look at an alternative way to define a Zipper Comonad
over a data type. Typically one would define a Zipper Comonad by defining a new
datatype which represents the Zipper; then implementing `duplicate` and
`extract` for it. `extract` is typically straightforward to write, but I've had
some serious trouble writing `duplicate` for some more complex data-types like
trees.
We're looking at a different way of building a zipper, The advantages of this
method are that we can build it up out of smaller instances piece by piece.
Each piece is a easier to write, and we also gain several utility functions
@ChrisPenner
ChrisPenner / Folds.Filtering.hs
Created January 19, 2020 16:48
Deck of cards example for Optics By Example
module Folds.Filtering where
import Control.Lens
data Card =
Card { _name :: String
, _aura :: Aura
, _holo :: Bool -- Is the card holographic
, _moves :: [Move]
} deriving (Show, Eq)
@ChrisPenner
ChrisPenner / Cookies.hs
Created March 18, 2022 02:49
Cookie handling in Servant.
import Data.Function ((&))
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Text (Text)
import qualified Data.Text as Text
import GHC.TypeLits (KnownSymbol, Nat, Symbol, symbolVal)
import Servant
import qualified Web.Cookie as Cookie
-- | Allows deserializing a single cookie in a servant route.
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE RankNTypes #-}
module Recurser where
import Control.Lens
import Data.Monoid
import Data.Foldable
import Data.Functor.Contravariant
@ChrisPenner
ChrisPenner / README.md
Created June 3, 2018 00:31
Use Google Sheet as BigQuery Dataset

Generating BQ schema from google sheet header row

To generate a new schema:

  • Copy the ID header row from your google sheet

  • pbpaste | python make_schema.py

  • There's your BQ schema!

  • Add a new dataset to bigquery

  • Use your spreadsheet link as the file location

@ChrisPenner
ChrisPenner / README.md
Last active December 8, 2021 22:15
Syntax highlighting for text files, breaks up walls of white text.

Plain-text Syntax Highlighting

Over the years I've gotten used to reading code with syntax highlighting. The colouring of the highlighting provides anchoring points for my eyes as I scroll, helping me to keep my place. However, when I'd be editing plain-text, I have no anchoring points, just a huge wall of text; so I wrote a simple syntax file for it.

All it does is highlights capitalized words, very simple. This typically ends up highlighting things of importance: headings, names, and most importantly the beginning of sentences. I've found this to be a good amount of highlighting without being overwhelming, providing context without being overly flashy.

Here's my current version: Plain-text Syntax Highlighting