Skip to content

Instantly share code, notes, and snippets.

@MichaelXavier
MichaelXavier / hedis_read_write.hs
Created April 5, 2018 18:30
WIP idea on read/write splitting wrapper over hedis
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
-------------------------------------------------------------------------------
import Control.Monad
import Data.ByteString (ByteString)
import Data.List.NonEmpty
import Data.Semigroup
@MichaelXavier
MichaelXavier / Main.hs
Created June 29, 2017 02:58
A "stepped" free monad that can fast-forward to resume a job, saves state between steps
{-# OPTIONS_GHC -Wall -Werror #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
module Main
( main
) where
-------------------------------------------------------------------------------
import Control.Monad (void)
@MichaelXavier
MichaelXavier / Lossy.hs
Created March 24, 2017 23:48
a thing like a prism but non-composable and has error types
import Control.Lens
-- from Control.Error
hush :: Either e a -> Maybe a
hush (Left _) = Nothing
hush (Right a) = Just a
-------------------------------------------------------------------------------
data Lossy e a b = Lossy {
@MichaelXavier
MichaelXavier / PerPage.hs
Created March 10, 2017 19:01
Customizable per page smart constructor using type-level naturals
module PerPage
( PerPage -- don't export the constructor, only constructable via smart constructor prism
, perPage
) where
import qualified Control.Lens as L
import qualified GHC.TypeLits as TL
import Data.Proxy (Proxy(..))
-- Represents a per page parameter you may accept on a web server, API, etc for pagination.
@MichaelXavier
MichaelXavier / stack_deathgrips.sh
Last active December 3, 2016 18:49
Hook stack to play deathgrips noises on success/failure
#!/bin/bash
# alias stack='~/stack.sh'
SUCCESS=${SUCCESS:=~/Dropbox/Music/YUH.wav}
FAILURE=${FAILURE:=~/Dropbox/Music/AUGH.wav}
stack "$@"
status=$?
if [[ $status -eq 0 ]]
@MichaelXavier
MichaelXavier / loop_functions.js
Created December 15, 2014 15:39
reasonable use of defining functions in a loop?
function OOPShim() {
}
function proxyMethods(names) {
for (var i = 0; i < names.length; i++) {
var name = names[i];
OOPShim.prototype[name] = function(a) {
var args = Array.prototype.slice.call(arguments, 0);
args.unshift(this.state);
return FunctionalAPI[name].apply(this, args)();
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Control.Applicative
import Data.Monoid
import Test.QuickCheck
data JoinList m a = Empty
| Single m a
| Append m (JoinList m a) (JoinList m a)
{-# LANGUAGE OverloadedStrings #-}
data GUIEvent = KeyDownEvent { key_down__ctrl :: !Bool
, key_down__shift :: !Bool
, key_down__code :: !Int }
| MouseEvent { mouse__x :: !Int
, mouse__y ::!Int }
deriving (Show)
instance FromJSON GUIEvent where
parseJSON = withObject "GUIObject" parseGUIEvent
@MichaelXavier
MichaelXavier / age.hs
Created June 26, 2014 00:43
Age in context
{-# LANGUAGE FlexibleContexts #-}
module Main (main) where
import Control.Monad.Reader
type Year = Int
-- a person record has a birth year
data Person = Person { birthYear :: Year }
@MichaelXavier
MichaelXavier / gist:9488102
Created March 11, 2014 15:26
rvm failing to build new rubies
compiling callback-2.c
make[2]: Leaving directory '/home/michael/.rvm/src/ruby-2.1.1/ext/date'
make[2]: Entering directory '/home/michael/.rvm/src/ruby-2.1.1/ext/mathn/complex'
compiling complex.c
installing default complex libraries
linking shared-object mathn/complex.so
make[2]: Leaving directory '/home/michael/.rvm/src/ruby-2.1.1/ext/mathn/complex'
compiling callback-3.c
installing default parser libraries
linking shared-object json/ext/parser.so