Skip to content

Instantly share code, notes, and snippets.

@gatlin
gatlin / gist:d89ee5442c10ca59ac9a8b621392ed10
Last active May 10, 2020 17:49
Oleg's CK machine macro system - ever so slightly easier to find cross device here
#lang r5rs
; Composable syntax-rules macros via the CK abstract machine
;
; We demonstrate (mutually-) recursive, higher-order applicative
; macros with clausal definitions, defined in the style that looks very
; much like that of ML or (strict) Haskell.
; We write composable, call-by-value--like macros without
; resorting to the continuation-passing-style and thus requiring no
; macro-level lambda. The syntax remains direct-style, with
; nested applications.
@gatlin
gatlin / twitch
Created April 26, 2020 22:53
Stream arbitrary video files to twitch via gstreamer
#!/usr/bin/env bash
###
# Usage
###
# twitch <path-to-file>
PATH_TO_TWITCH_KEY="$HOME/.config/twitch.key"
if [ ! -f "$HOME/.config/twitch.key" ]; then
echo "Please write your twitch key to $PATH_TO_TWITCH_KEY"
@gatlin
gatlin / ot.hs
Last active January 24, 2020 17:03
{- |
Operational transformation in Haskell using (co)free (co)monads.
-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
import Data.List (splitAt)
import Control.Monad
class cont:
def __init__(self, fn):
self.fn = fn
def __call__(self, *args, **kwargs):
return (lambda: self.fn(*args, self, **kwargs))
class tailrec:
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
/* Helper function which writes a string to a file descriptor.
*/
static gboolean
write_to_fd (int fd, const gchar *message) {
gsize bw;
GIOChannel *channel = g_io_channel_unix_new (fd);
GString *message_str = g_string_new (message);
g_string_append (message_str, "\n");
g_io_channel_write_chars (channel,
message_str->str,
@gatlin
gatlin / monads.lhs
Created November 25, 2012 21:07
My explanation of monads
It's said that all Haskell programmers eventually write a blog post explaining
Monads. Today I fulfill my destiny. The basic format is ripped from "Learn You A
Haskell For Great Good," but these are my own crappy examples.
This blog post is literate Haskell; you can get it
[here](https://gist.github.com/4145341). You can import it into ghci and type
the functions to see their output (`ghci monads.lhs`). For this to work, we'll
need to import one module for use later:
> import Control.Applicative
@gatlin
gatlin / yet-another-conway.hs
Last active August 20, 2018 04:21
Yet Another Comonadic Game of Life implementation in Haskell, in response to a Reddit thing. Much love to those who have done similar things previously. I'm really just a messenger here.
{-# LANGUAGE DeriveFunctor #-}
import Control.Applicative
import Control.Comonad
import Control.Monad (forM_)
-- These two typeclasses probably make some people groan.
class LeftRight t where
left :: t a -> t a
Writing a billing system in Haskell, using Stripe and Heroku
===
*This post is written in literate Haskell, meaning the source can be turned
into a blogpost or a working program. You can view the [source][source].
[source]: https://gist.github.com/gatlin/7754289
Normally clients pay me with checks, but recently one informed me that they
really really really like *really really* super prefer some kind of online
{-# LANGUAGE RankNTypes #-}
import Prelude hiding (IO, getLine)
import qualified Prelude as P
import System.IO.Unsafe
-- * The Foreign Function Interface
-- | FFI values permit interfacing with foreign functions, such as low-level IO
-- operations, memory management operations, or bindings to other user-level