Skip to content

Instantly share code, notes, and snippets.

:bowtie:
Happily Hacking

Chris Penner ChrisPenner

:bowtie:
Happily Hacking
Block or report user

Report or block ChrisPenner

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@ChrisPenner
ChrisPenner / ChooseIndex.hs
Created Sep 14, 2019
Gain extra context in your lens chain by passing values as an index!
View ChooseIndex.hs
Sometimes when diving deep with optics you need to reference something earlier in your path after you dive deeper. Just stash it in your index!
Let's say we want to know which pets belong to which owner, we've got the data paired up like this:
pets :: [(String, [String])]
pets =
[ ("Steven", ["Spot", "Mittens"])
, ("Kaylee", ["Pepper", "Sparky"])
]
@ChrisPenner
ChrisPenner / LensyBinarySearch.hs
Created Aug 27, 2019
Binary tree search using lenses
View LensyBinarySearch.hs
-- Define a simple binary tree
data BT a
= BT { _leftTree :: BT a
, _val :: a
, _rightTree :: BT a
}
| Leaf
deriving (Show, Eq, Functor, Foldable, Traversable)
-- Generate traversals for the (partial) fields
makeLenses ''BT
@ChrisPenner
ChrisPenner / FindIslands.hs
Last active Aug 23, 2019
A quick experiment using union-find to determine which pieces of 'land' in a grid are connected. Could ostensibly be used with any equivalence/grouping predicate.
View FindIslands.hs
{-# LANGUAGE ScopedTypeVariables #-}
module Lib where
import Data.UnionFind.IO
import Control.Monad
import Control.Applicative
import Data.Foldable
import Data.Maybe
import qualified Data.Map as M
import qualified Data.Set as S
@ChrisPenner
ChrisPenner / DynamicComonad.hs
Created Apr 5, 2019
Dynamic Programming Comonads using Recursion Schemes
View DynamicComonad.hs
import Data.Functor.Foldable
import Control.Comonad.Cofree as C
import Control.Comonad.Trans.Cofree as CF
cofreeDynExtend :: forall f a b.
Functor f
=> (CofreeF f a (C.Cofree f b) -> b)
-> C.Cofree f a
-> C.Cofree f b
cofreeDynExtend f = cata extract'
@ChrisPenner
ChrisPenner / GenericMonoid.hs
Last active Feb 22, 2019
Derive Monoid for using Generics
View GenericMonoid.hs
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DeriveGeneric #-}
module GenericMonoid where
import Data.Monoid
import Data.Maybe
@ChrisPenner
ChrisPenner / matrix-search-comonad.hs
Last active Nov 12, 2018
Matrix path search using comonads
View matrix-search-comonad.hs
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Lib where
View update-monads.md

Today we're going to take a peek at the Update monad! It's a monad which was formalized and described in Update Monads: Cointerpreting Directed Containers by Danel Ahman and Tarmo Uustalu. Most folks probably haven't heard of it before, likely because most of what you'd use it for is well encompassed by the Reader, Writer, and State monads. The Update Monad can do everything that Reader, Writer, and State can do, but as a trade-off tends to be less efficient at each of those tasks. It's definitely still worth checking out though; not only is it interesting, there are a few things it handles quite elegantly that might be a bit awkward to do in other ways.

@ChrisPenner
ChrisPenner / README.md
Created Jun 3, 2018
Use Google Sheet as BigQuery Dataset
View README.md

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
View custom-state-handlers.purs
module Main where
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Data.Either (Either(..))
import Data.Monoid (class Monoid, mempty)
import Run (Run, extract, lift, on, peel, send)
import Run.State (STATE, State(..))
@ChrisPenner
ChrisPenner / FreeMonadOptimization.hs
Last active Aug 24, 2017
Free Monads vs MTL regarding optimizations using AST transformations
View FreeMonadOptimization.hs
{-# language DeriveFunctor #-}
{-# language GeneralizedNewtypeDeriving #-}
module FreeOpt where
import Control.Monad.Free
import Control.Monad.Trans
import System.Directory
-- | Define our Free Monad DSL
data FileF r =
You can’t perform that action at this time.