After listening to the latest Magic Read-along episode "You should watch this" (which you should go listen to now) I got
caught up thinking about Brian's idea of an Endomorphism version of Kleisli composition for use with Redux,
it's actually a very similar model to what I'm using in my event
framework for event listeners so I figured I'd try to formalize the pattern and recognize
some of the concepts involved. IIRC Brian
described the idea of a Redux-reducer, which is usually of type s -> Action -> s
, it takes a state and an action and returns
a new state. He then re-arranged
the arguments to Action -> s -> s
. He then recognized this as Action -> Endo s
(an Endo
-morphism is just any function
from one type to itself: a -> a
).
He would take his list of reducers and partially apply them with the Action
, yielding a list of type Endo s
where s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Use at your own risk :P | |
FOLDER="/tmp/copy-pasta" | |
if [ $# -lt 1 ]; then | |
cat << EOF | |
usage: copy <files> | |
EOF | |
exit 1 | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Lens | |
import Data.Typeable | |
import Data.Dynamic | |
dynamicLens :: Typeable a => (Lens' s a) -> Lens' s Dynamic | |
dynamicLens l = lens getter setter | |
where | |
getter s = toDyn $ view l s | |
setter s b = | |
case fromDynamic b of |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You can use *most* optics as though they're a custom `traverse` | |
>>> (each . _Right) print (Left 1, Right 2, Left 3) | |
2 | |
-- 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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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 |