Skip to content

Instantly share code, notes, and snippets.

View robrix's full-sized avatar
🌊
every dot and stroke I paint will be alive

Rob Rix robrix

🌊
every dot and stroke I paint will be alive
View GitHub Profile
@robrix
robrix / Deriving.hs
Last active September 28, 2020 15:28
Deriving of Functor instances via Applicative, and Functor & Applicative instances via Monad, using DerivingVia
module Deriving
( ApplicativeInstance(..)
, MonadInstance(..)
) where
import Control.Applicative (liftA, liftA2)
import Control.Monad (ap, liftM, liftM2)
-- | 'Functor' instances derivable via an 'Applicative' instance, for use with @-XDerivingVia@.
--
@robrix
robrix / Mendler.hs
Created September 3, 2020 15:02
Mendler-style iteration in Haskell
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE RankNTypes #-}
module Mendler where
class Iter b t | t -> b where
iter :: (forall x . (x -> a) -> b x -> a) -> t -> a
data ListF a b = Nil | Cons a b
instance Iter (ListF a) [a] where
@robrix
robrix / FoldableN.hs
Created June 20, 2020 15:10
Single-pass folding of multiple structures.
module Data.FoldableN where
import Control.Applicative -- for ZipList
import Linear.V1 -- for V1, an identity functor
import Linear.V2 -- for V2, data V2 a = V2 a a
class Foldable t => FoldableN t where
-- | Fold multiple structures into a 'Monoid'.
--
-- @
__attribute__((format_arg(__NSString__, 2)))
NSString *ಠ_ಠ_message(const char *severity, const char *format) {
return [NSString stringWithFormat:@"%s: %s", severity, format];
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-nonliteral"
__attribute__((format(printf, 1, 2)))
void ಠ_ಠ_warning(const char *format, ...) {
va_list ಠ_ಠ;
va_start(ಠ_ಠ, format);
@robrix
robrix / NNat.hs
Last active January 25, 2020 11:36
N is for Natural; zero, or more
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE UndecidableInstances #-}
module NNat where
@robrix
robrix / diffused-effects.txt
Created September 22, 2019 22:08
Results of compiling a benchmark against fused-effects & diffused-effects with -dshow-passes
compile: input file benchmark/Send/Send10.hs
*** Checking old interface for Send.Send10 (use -ddump-hi-diffs for more details):
*** Parser [Send.Send10]:
!!! Parser [Send.Send10]: finished in 0.55 milliseconds, allocated 0.957 megabytes
*** Renamer/typechecker [Send.Send10]:
!!! Renamer/typechecker [Send.Send10]: finished in 186.13 milliseconds, allocated 77.516 megabytes
*** Desugar [Send.Send10]:
Result size of Desugar (before optimization)
= {terms: 195, types: 4,554, coercions: 6,170, joins: 0/7}
Result size of Desugar (after optimization)
@robrix
robrix / gist:7831048
Created December 6, 2013 19:48
herpderpicons
⚆︗⨀
⚆‿⨀
⚆␣⨀
⚆˾⨀
⚆⫎⨀
⚆⩌⨀
@robrix
robrix / Parametricity.txt
Last active June 12, 2018 20:13
Higgledy Piggledy — Parametricity
Higgledy piggledy
parametricity’s
quite a nice property
functions can use;
Enforcing the absence of
state or identity
up to the type level
promotes reuse.
@robrix
robrix / init.js
Last active May 8, 2018 13:41
select-outside-brackets and select-scope commands, built using Bracket Matcher’s select-inside-brackets.
'use babel';
import {Range} from 'atom';
atom.commands.add('atom-text-editor', 'editor:select-outside-brackets', function () {
const editor = atom.workspace.getActiveTextEditor();
const initial = editor && editor.getSelectedBufferRange().freeze();
atom.commands.dispatch(editor.element, 'bracket-matcher:select-inside-brackets');
@robrix
robrix / Rollable.hs
Last active March 11, 2018 03:37
Deriving a nonrecursive base functor from a recursive datatype using GHC.Generics
{-# LANGUAGE AllowAmbiguousTypes, DataKinds, DefaultSignatures, DeriveAnyClass, DeriveFunctor, DeriveGeneric, FlexibleContexts, FlexibleInstances, MultiParamTypeClasses, ScopedTypeVariables, TypeApplications, TypeFamilies, TypeOperators #-}
module Rollable where
import Data.Functor.Foldable
import GHC.Generics
data Tree = Empty | Node Tree Int Tree
deriving (Eq, Generic, Ord, Rollable, Show)
depth :: Tree -> Int