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.
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 |
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 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' |
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 #-} | |
{-# LANGUAGE TypeApplications #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE DeriveGeneric #-} | |
module GenericMonoid where | |
import Data.Monoid | |
import Data.Maybe |
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 DeriveFunctor #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE OverloadedLists #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE ViewPatterns #-} | |
{-# LANGUAGE DeriveFoldable #-} | |
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE TypeApplications #-} | |
module Lib where |
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
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 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(..)) |
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 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 = |