Skip to content

Instantly share code, notes, and snippets.

View chessai's full-sized avatar

chessai chessai

View GitHub Profile
@chessai
chessai / ci.sh
Last active January 30, 2019 15:28
# recursively replace all occurrences of something.
find dir -type f -name '*.hs' -print0 | \\
xargs -0 sed -i 's/Find/Replace/g'
# recursively remove all trailing whitespace
find dir -type f -print0 | \\
xargs -0 sed -i 's/[ \t]*S//'
# recursively add newlines to the end of files that don't have them.
find dir -type f -print0 | xargs -0 sed -i -e '$a\'
-- | JSON parse helper.
--
-- data Foo = Foo { fooIntBar :: Int, fooStringBar :: String }
-- deriving (Generic)
--
-- -- This will make 'Foo''s fields get parsed as
-- -- 'int_bar', 'string_bar', etc.
-- instance FromJSON Foo where
-- parseJSON = parseJSONFielded
parseJSONFielded :: forall a. (Generic a, Typeable a, AE.GFromJSON AE.Z
fibs10 :: [NUI]
fibs10 = take 10 fibs
fibs :: [NUI]
fibs = unPolynomial $ star (Polynomial [NUI 0, NUI 1, NUI 1])
-- | \mathbb{N} \bigcup \infty
data NUI = NUI Word | Inf
deriving (Eq, Show)
@chessai
chessai / ByteArrayToByteString.hs
Created January 18, 2019 22:30
convert a (pinned) bytearray to a bytestring without copying
byteArrayToByteString :: ByteArray -> ByteString
byteArrayToByteString (ByteArray b#) =
let contents# = byteArrayContents# b#
fp = ForeignPtr contents# (PlainPtr (byteArrayToMutableByteArray b#))
len = lenAddr contents#
in PS fp 0 len
byteArrayToMutableByteArray :: ByteArray# -> MutableByteArray# RealWorld
byteArrayToMutableByteArray = unsafeCoerce#
[nix-shell:~/Development/containers]$ ./dist/build/sequence-benchmarks/sequence-benchmarks
benchmarking splitAt/append/10
time 691.1 μs (690.4 μs .. 692.4 μs)
1.000 R² (1.000 R² .. 1.000 R²)
mean 693.1 μs (691.5 μs .. 696.5 μs)
std dev 7.658 μs (2.571 μs .. 12.73 μs)
benchmarking splitAt/append/100
time 4.301 ms (4.295 ms .. 4.308 ms)
1.000 R² (1.000 R² .. 1.000 R²)
[chessai@chessai-kudu:~/Development/summoner/summoner-cli]$ cat default.nix
{ compiler ? "ghc863" }:
with rec {
pkgs = (import ./nix/nixpkgs.nix {
inherit compiler;
});
drv = pkgs.haskellPackages.summoner-cli;
};
@chessai
chessai / ParseFooA.core
Created January 9, 2019 18:11
fixedparser core inspection
[1 of 1] Compiling ParseFooA ( foo.hs, foo.o )
==================== Tidy Core ====================
Result size of Tidy Core
= {terms: 816, types: 382, coercions: 40, joins: 0/9}
-- RHS size: {terms: 1, types: 0, coercions: 0, joins: 0/0}
$trModule4
$trModule4 = "main"#
@chessai
chessai / foo.core
Last active January 9, 2019 17:37
fixedparser core output example
-- | ghc foo.hs -ddump-simpl -dsuppress-all -fforce-recomp -O2
[1 of 1] Compiling ParseFoo ( foo.hs, foo.o )
==================== Tidy Core ====================
Result size of Tidy Core
= {terms: 260, types: 105, coercions: 10, joins: 0/3}
-- RHS size: {terms: 1, types: 0, coercions: 0, joins: 0/0}
$trModule4
owner = "chessai"
fullName = "chessai"
email = "[email protected]"
license = "BSD3"
lib = true
exe = false
test = true
bench = false
github = true
private = false
@chessai
chessai / rank2catmaybes.hs
Last active December 13, 2018 15:05
catMaybes for any rank2-traversable type that contains a maybe
{-# LANGUAGE RankNTypes #-}
module Rank2CatMaybes (catMaybes) where
import Data.Coerce (coerce)
import Data.Functor.Identity (Identity(Identity))
import qualified Rank2
liftId :: (forall x. x -> Identity x)
liftId = coerce