Skip to content

Instantly share code, notes, and snippets.

What

A technique for writing parsers.

Why

  • Easy to understand
  • Generally applicable
  • Full power of the programming language at your disposal
  • Declarative
~/Projects/mrcss
( master )☛ time npm test
> mrcss@1.0.0 test /Users/michaeltbaker/Projects/mrcss
> mocha --compilers js:babel-core/register test/*
Hello test
1) does a thing
warning: there are multiple derivations named ‘nodejs-0.12.0’; using the first one
installing ‘nodejs-0.12.0’
these derivations will be built:
/nix/store/8ap1vgh9979cl8p4b3c4xl3r8nqs1iid-nodejs-0.12.0.drv
building path(s) ‘/nix/store/pv1l6a2dqwfy41ldnpvgp1i7arzfvkqr-nodejs-0.12.0’
unpacking sources
unpacking source archive /nix/store/846c6d0zaqpg50ph7h6vba3fgdprcmqj-node-v0.12.0.tar.gz
source root is node-v0.12.0
patching sources
applying patch /nix/store/w9f0cqmzjgpdcyfqaihzf80bdwn7x4xg-no-xcode.patch
26 client.authorization = Signet::OAuth2::Client.new(
27 :token_credential_uri => 'https://www.googleapis.com/oauth2/v3/token',
28 :audience => 'https://www.googleapis.com/oauth2/v3/token',
29 :scope => scope,
30 :issuer => GMAIL_ISSUER,
31 :signing_key => key,
32 :person => employee_email_address,
33 )
/*
data Creator = Stylist Int
| Member Int
| Service
data Service = Service1
| Service2
*/
select * from outfits where creator = (Stylist _);
{-# LANGUAGE NoMonomorphismRestriction #-}
class Symantics r where
int :: Int -> r Int
bool :: Bool -> r Bool
add :: r Int -> r Int -> r Int
mul :: r Int -> r Int -> r Int
leq :: r Int -> r Int -> r Bool
if_ :: r Bool -> r a -> r a -> r a
app :: r (a -> b) -> r a -> r b

I'm experimenting with some ideas and I think one of the problems I have could be solved with lenses, but it doesn't seem to work how I expect it to when I try to implement it.

I want to create a tree of data structures, where each node stores a lens that goes from the root of the tree down to that node. Here is a conceptual image. The idea in that image is that the color of the lens matches the color of the path that it represents through the tree.

Here is an example of a tree shaped data structure without storing the path lenses at the nodes:

{-# LANGUAGE TemplateHaskell #-}
@MichaelBaker
MichaelBaker / gist:916c04cb1d444e29c486
Created June 17, 2014 18:16
Reader Monad Example
import Control.Monad.Reader (runReader, ask)
data Game = Game { playerOne :: String
, playerTwo :: String
, currentValue :: Int
}
data Move = Move { player :: String
, value :: Int
}
vertex = "#version 110\n attribute vec3 position; void main() { gl_Position = vec4(position, 1.0); }"
fragment = "#version 110\n void main() { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); } "
time = Monotonic
tris = [Triangle 0 0 0 (cos x) (sin x) 0 (cos (x^2)) (sin (x^2)) 0 | x <- [0.0,0.08..2*pi]]
nums = zip tris [0..]
len = length tris
main = do
withBasicWindow windowWidth windowHeight "Metropolis" $ \window -> do
@MichaelBaker
MichaelBaker / gist:5810053
Last active December 18, 2015 16:18
Breadth first numbering
data Tree a = EndNode | Leaf | Node a (Tree a) (Tree a)
data FlatTree = FlatLeaf | FlatNode deriving (Show)
data NumberedTree a = NumLeaf | NumNode Integer deriving (Show)
instance (Show a) => Show (Tree a) where
show = showTreeIndented 0
where showTreeIndented indentation Leaf = concat (take indentation $ repeat " ") ++ "[]\n"
showTreeIndented indentation (Node value left right) = concat (take indentation $ repeat " ") ++ (show value) ++ "\n" ++ showTreeIndented (indentation + 1) left ++ showTreeIndented (indentation + 1) right