Skip to content

Instantly share code, notes, and snippets.

@MichaelXavier
MichaelXavier / enum.ts
Last active July 16, 2019 03:33
to/from enum converter in TypeScript using generics
enum Status {
Pending,
PaymentReceived,
Shipped
}
class EnumConverter<E> {
toEnum(orig : string) : E {
return this.enumMap()[orig];
}
@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 / maybemonad.hs
Created December 8, 2013 02:46
Contrived example of using the Maybe monad to make inherently unsafe operations safe by representing failures as Nothing. You can turn a sequence of functions that produce Maybes into an imperative chunk of code that only has to handle the success case.
safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:xs) = Just x
safeDiv :: Int -> Int -> Maybe Int
safeDiv _ 0 = Nothing
safeDiv x y = Just (div x y)
divideHeads :: [Int] -> [Int] -> Maybe Int
divideHeads xs ys = do
import Data.List (sort)
import System.Environment (getArgs)
main = do (inFile:outFile:_) <- getArgs
writeFile outFile . unlines . sort . lines =<< readFile inFile
@MichaelXavier
MichaelXavier / Gemfile
Last active December 18, 2015 21:29
Some ruby-based tooling for haskell projects that makes development easier for me. Sandboxing requires cabal >= 0.1.7
source "http://rubygems.org"
gem "guard-shell"
gem "rake"
require 'sinatra'
require 'pp'
delete '/' do
"you DELETED: #{request.body.string}"
end
put '/' do
"you PUTTED: #{request.body.string}"
end