Skip to content

Instantly share code, notes, and snippets.

@carymrobbins
carymrobbins / resource.hs
Last active August 29, 2015 14:05
I was under the impression that ResourceT prevents accessing released resources with the type system. Maybe I am not using it correctly...
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Resource
import System.IO
main = runResourceT $ do
(releaseO, output) <- allocate (openFile "output.txt" WriteMode) hClose
lift $ hPutStrLn output "hallo!"
release releaseO
(releaseI, input) <- allocate (openFile "output.txt" ReadMode) hClose
release releaseI
@carymrobbins
carymrobbins / get_css.js
Created August 23, 2014 17:54
Get all CSS attributes for an element.
var getCss = function(el) {
var style = window.getComputedStyle(el);
return Object.keys(style).reduce(function(acc, k) {
var name = style[k],
value = style.getPropertyValue(name);
if (value !== null) {
acc[name] = value;
}
return acc;
}, {});
@carymrobbins
carymrobbins / debug_metaclass.py
Created September 2, 2014 15:37
Metaclass to debug your methods
# Debugging metaclass
# Put this in your class definition.
def __metaclass__(name, bases, attrs):
from functools import wraps
try:
import ipdb as pdb
except ImportError:
import pdb
def debug_wrapper(f):
/**
* Adapted from http://github.com/JetBrains/intellij-community
* xml/xml-psi-impl/src/com/intellij/lexer/_HtmlLexer.flex
*/
package com.haskforce.yesod.shakespeare.hamlet.highlighting;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.intellij.lexer.FlexLexer;
import com.intellij.psi.tree.IElementType;
{-# LANGUAGE FlexibleInstances, FunctionalDependencies, MultiParamTypeClasses #-}
import Control.Monad
import Control.Monad.Loops
import Control.Monad.State
import Data.Char
class Iterator a b r | a -> b r where
next :: (Monad m) => b -> StateT a m (Maybe r)
@carymrobbins
carymrobbins / parse_version.py
Created September 17, 2014 21:25
Parse version number from python source. Really, this could be more generalized to parse any variables (given obvious restrictions) from source.
import ast
def parse_version(source, name='__version__'):
for exp in ast.parse(source).body:
if isinstance(exp, ast.Assign) and name in (t.id for t in exp.targets):
if len(exp.targets) != 1:
raise ValueError('Cannot parse assignment unpacking.')
if isinstance(exp.value, ast.Str):
return exp.value.s
@carymrobbins
carymrobbins / BootstrapCheckBoxFieldHack.hs
Last active March 13, 2016 20:51
Hack to get a nice looking checkbox field with renderBootstrap3
import Control.Applicative ((<$>), (<*>))
import Data.Text (Text)
import Text.Blaze (ToMarkup)
import Yesod
import Yesod.Form.Bootstrap3 (BootstrapFormLayout(..), renderBootstrap3, bfs)
bootstrapCheckBoxField :: (ToMarkup a, RenderMessage (HandlerSite m) FormMessage, Monad m) => a -> Field m Bool
bootstrapCheckBoxField label = checkBoxField
{ fieldView = \theId name attrs val _ -> [whamlet|\
$newline never
@carymrobbins
carymrobbins / runDBDev.hs
Created September 26, 2014 18:42
Analogous to runDB for cabal repl.
import Prelude
import Control.Monad.Logger (LoggingT, runStdoutLoggingT)
import Control.Monad.Trans.Resource (ResourceT, runResourceT)
import qualified Database.Persist
import Database.Persist.Sql (SqlPersistT, runSqlPool)
import qualified Settings
import Yesod.Default.Config (DefaultEnv(Development), withYamlEnvironment)
runDBDev :: SqlPersistT (LoggingT (ResourceT IO)) a -> IO a
runDBDev m = do
@carymrobbins
carymrobbins / HLint.hs
Last active August 29, 2015 14:07
Customized hints for HLint
-- This file should go either in your current working directory or your cabal data-dir.
-- You can also reference it directly via: hlint --hint=path/to/HLint.hs
-- http://community.haskell.org/~ndm/darcs/hlint/hlint.htm#customization
error = fromJust ==> error
error = undefined ==> error
error = error ==> error
main = putStrLn "Hello world!"