Skip to content

Instantly share code, notes, and snippets.

@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
@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];
}
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
@MichaelXavier
MichaelXavier / 204client.hs
Last active December 16, 2015 18:08
http-client seems to hang indefinitely waiting on the content from some servers when they return 204 no content.
{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
import Network.Http.Client
main :: IO ()
main = get ("http://localhost:4568/") printStatus
-- prints the status then hangs forever
where printStatus resp stream = print $ getStatusCode resp
@MichaelXavier
MichaelXavier / aborting_composed_method.rb
Last active December 12, 2015 06:59
aborting a composed method early on failure
# is this the best way to do it?
# in this way, each method must raise if something goes wrong,
# but this behavior isn't necessarily exceptional. composed_method
# uses this exception for control flow because its contract is that
# it returns true on success, false on failure.
# I've cargo culted the phrase "don't use exceptions for control flow" but I realize I don't know why...
def composed_method
frobulate_widgets
refrobulate_widgets
confribulate_frobulations
@MichaelXavier
MichaelXavier / development.log
Created November 30, 2012 22:37
incredibly high mysql latency
Connecting to database specified by database.yml [1286/9658]
Starting the New Relic Agent.
NewRelic Agent Developer Mode enabled.
To view performance information, go to http://localhost:3000/newrelic
Installed New Relic Browser Monitoring middleware
(0.3ms) BEGIN
ProductType Load (35.7ms) SELECT `product_types`.* FROM `product_types` WHERE `product_types`.`name` = 'Music' LIMIT 1
Category Load (0.6ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`name` = 'Music' LIMIT 1
Descriptor Load (0.3ms) SELECT `descriptors`.* FROM `descriptors` WHERE `descriptors`.`product_type_id` = 1 AND `descriptors`.`name` = 'Discogs ID' ORDER BY name LIMIT 1
Descriptor Load (0.4ms) SELECT `descriptors`.* FROM `descriptors` WHERE `descriptors`.`product_type_id` = 1 AND `descriptors`.`name` = 'Artists' ORDER BY name LIMIT 1
require 'celluloid'
require 'stringio'
class Producer
include Celluloid
include Celluloid::Notifications
attr_reader :io, :next_pool
def initialize(io, next_pool)
require 'celluloid'
require './lib/discog_downloader/product'
module DiscogDownloader
class Converter
include Celluloid
def initialize(queue)
@queue = queue
end