Skip to content

Instantly share code, notes, and snippets.

@glguy
glguy / Each.hs
Created February 24, 2014 18:48
Stand-alone implementation of the automatic Each instance generation
{-# LANGUAGE TemplateHaskell #-}
module Each (makeEachInstance) where
import Control.Applicative (Applicative, (<*>), (<$>), pure)
import Control.Lens (Each(each), _2, folded, lengthOf, to, toListOf, view)
import Control.Monad (replicateM)
import Data.Map (Map, fromList)
import Data.Traversable (sequenceA)
import Language.Haskell.TH.Lens (HasTypeVars, conFields, name, substTypeVars, typeVars)
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-} -- because GHC doesn't know how fundeps work
module GenericGenericEach where
import Data.Array
import Control.Comonad
data Pointer a = Pointer !(Array Int a) !Int
deriving (Show)
@glguy
glguy / gist:74960a3f1531b64a201b
Last active August 29, 2015 14:05
Lens Cookbook

Optic Cookbook

The imports for building the various field-oriented optics are pretty minimal. It's not until you make a Getter or a Fold that you need to look outside of base.

This cookbook only covers the field oriented optics and not the constructor oriented ones. If you want to build a Prism or an Iso without a lens dependency, you should copy the definition of lens' prism and iso combinators and add a profunctors dependency to your project. Those two combinators are quite self-contained.

@glguy
glguy / MarkSweep.hs
Last active August 29, 2015 14:18
mark and sweep
module MarkSweep where
import Data.Array.IO
import Data.Word
import Data.Bits
import Data.Bits.Lens
import Data.Foldable (traverse_)
import Control.Lens
import Control.Monad
@glguy
glguy / keybase.md
Created August 20, 2015 17:16
keybase.md

Keybase proof

I hereby claim:

  • I am glguy on github.
  • I am glguy (https://keybase.io/glguy) on keybase.
  • I have a public key whose fingerprint is 15C3 00D6 96C2 8FD2 BE8E 957D BFDD BB57 E705 C00F

To claim this, I am signing this object:

fn main() {
let mut v = vec![false,false,true,false];
for x in &mut v {
if *x { break }
*x = true;
}
println!("{:?}", v);
{-# Language RankNTypes, TypeInType, ConstraintKinds, GADTs, TypeFamilies, TypeOperators, DataKinds #-}
module Tree where
import Data.Kind
import Data.Bifunctor
data SomeN (f :: k) where
Base :: f -> SomeN f
Step :: (forall a. SomeN (f a)) -> SomeN f
module Underscore (doUnderscore) where
import Language.Haskell.TH
import Control.Monad (unless)
import Data.Generics (everything, everywhereM, mkM, mkQ)
import Data.Monoid (Any(Any))
-- | The placeholder for arguments is the wildcard @_@
placeholder :: Exp
placeholder = UnboundVarE (mkName "_")
-- Puzzle explained at https://github.com/dag/all-about-monads/blob/master/examples/example24.hs
-- Print each possible solution, one per line
main :: IO ()
main = mapM_ putStrLn solution
data Sex = Male | Female deriving (Show, Eq)
-- | Boolean implication
(==>) :: Bool -> Bool -> Bool