Skip to content

Instantly share code, notes, and snippets.

Happily Hacking

Chris Penner ChrisPenner

Happily Hacking
View GitHub Profile
# Use at your own risk :P
if [ $# -lt 1 ]; then
cat << EOF
usage: copy <files>
exit 1
View TextSpan.hs
{-# LANGUAGE OverloadedStrings #-}
module TextSpan where
import Control.Lens
import qualified Data.Text as T
import Text.RawString.QQ (r)
import qualified Data.List as List
type Line = Int
type Col = Int
ChrisPenner / Folds.Filtering.hs
Created Jan 19, 2020
Deck of cards example for Optics By Example
View Folds.Filtering.hs
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)
View Optics
View DynamicLens.hs
import Control.Lens
import Data.Typeable
import Data.Dynamic
dynamicLens :: Typeable a => (Lens' s a) -> Lens' s Dynamic
dynamicLens l = lens getter setter
getter s = toDyn $ view l s
setter s b =
case fromDynamic b of
View OpticsTraverse.hs
You can use *most* optics as though they're a custom `traverse`
>>> (each . _Right) print (Left 1, Right 2, Left 3)
-- We can ignore the new structure it returns
(Left 1,Right (),Left 3)
It works on other types of effects too!
Here we use lists as a non-determinism effect to get all possible combos!
ChrisPenner / Lib.hs
Created Oct 1, 2019
Cofree animation
View Lib.hs
module Lib where
import Control.Comonad.Cofree
import Control.Monad.Reader
import Control.Concurrent
drawBar :: Float -> IO ()
drawBar n = putStrLn (replicate (ceiling n) '#')
type Animation = Cofree (Reader Float) Float
View InfixHoles.hs
*Infix* typed holes!
>>> "suggest" `_` "concat" :: String
<interactive>:1:11: error:
Found hole: _ :: [Char] -> [Char] -> String
Valid hole fits include
showString :: String -> ShowS
(++) :: forall a. [a] -> [a] -> [a]
showList :: forall a. Show a => [a] -> ShowS
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 / 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