Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env stack
-- stack --resolver lts-3.13 --install-ghc runghc --package base --package attoparsec --package text
{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative ((<|>), many)
import Data.Attoparsec.Text (Parser)
import Data.Attoparsec.Combinator (lookAhead)
import qualified Data.Attoparsec.Text as P -- (*)
import Data.Char (chr)
#! /usr/bin/env stack
-- stack --resolver lts-3.13 --install-ghc runghc --package base --package bytestring --package iso3166-country-codes --package lens --package QuickCheck --package syb --package tagged --package uri-bytestring
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE DataKinds #-}
import Data.Proxy (Proxy(Proxy))
import GHC.TypeLits (symbolVal)
main = putStrLn $ symbolVal (Proxy :: Proxy "Hello, world!")
let
inherit (builtins) map;
inherit ((import <nixpkgs> {}).lib)
fold hasSuffix stringToCharacters concatStrings any;
in rec {
inits = fold (x: y: [[]] ++ map (z: [x] ++ z) y) [[]];
hasInfix = i: str: any (x: hasSuffix i (concatStrings x)) (inits (stringToCharacters str));
}
@fmap
fmap / json.nix
Last active August 29, 2015 14:18
let
inherit (builtins) isAttrs typeOf toJSON;
inherit ((import <nixpkgs> {}).lib) mapAttrsRecursive;
in rec {
show = value: if typeOf value == "lambda" then "<LAMBDA>" else value; # XXX: Not sure how to discriminate between functions and 'PRIMOP'.
json = value: toJSON (if ! isAttrs value then show value else mapAttrsRecursive (_: show) value);
}
@fmap
fmap / ddl
Created February 10, 2015 03:23
#!/usr/bin/env bash
file "$(realpath $1)" | grep -q "shared object" || {
echo "Argument error: address a shared object file" >&2
exit 1
};
find $(nix-store -q --referrers "$1") -executable -type f -name '*.so' -exec sh -c '
realpath {} | xargs ldd | grep -o "/nix/store/.* " | xargs realpath | sed "s,^,$(nix-store -q --deriver {}):,"
' ';' | sort -u | grep "$(realpath $1)" | grep -Po '^.*(?=:)'
0 this well-oiled machine:
barters capacity for
"this well-oiled machine:"
1 moral nihilism:
as if spurning direction
would yield a scalar
@fmap
fmap / pipes.txt
Last active August 29, 2015 14:05
| Upstream output | Upstream input | Downstream input | Downstream output | Base monad | Return value
Proxy a' a b' b m r | a' | a | b' | b | m | r
Effect m r | Void | () | () | Void | m | r
Pipe a b m r | () | a | () | b | m | r
Producer b m r | Void | () | () | b | m | r
Consumer a m r | () | a | () | Void | m | r
Client a' a m r | a' | a | () | Void | m | r
Server b' b m r | Void | () | b' | b | m | r
import Prelude hiding (span, head, null)
import Control.Monad (liftM2)
import Data.Vector (Vector, null, empty, cons, span, head, thaw, freeze)
import qualified Data.Vector.Algorithms.Intro as Intro (sort)
group :: Eq a => Vector a -> Vector (Vector a)
group vs | null vs = empty
| otherwise = a `cons` group b where (a, b) = span (== head vs) vs
sort :: Ord a => Vector a -> IO (Vector a)
-- Quick and dirty:
--
-- Modular decomposition of a digraph into a set of subgraphs such that, in
-- each subgraph $s$, from a specific base vertex $b$, there exists a path to
-- every other vertex in $s$, and no path to base vertices in the rest of the
-- supergraph.
import Control.Applicative ((<$>),(<*>))
import Data.Graph.Wrapper (Graph, vertices, vertex, fromVerticesEdges, reachableVertices, edges)
import Data.List (intersect, union, find)