Skip to content

Instantly share code, notes, and snippets.

@aisamanra
aisamanra / Main.hs
Created November 20, 2014 00:56
sample "convenient main wrapper" code
module Main where
import NiceMain
main = nicemain go where go x y z = print $ (x && y) || z
@aisamanra
aisamanra / decodeOneOf.hs
Last active August 29, 2015 14:11
returning one or the other JSON instance
import Data.Aeson (decode, FromJSON)
import Data.ByteString.Lazy (ByteString)
import Data.Monoid (First(..), (<>))
decodeOneOf :: (FromJSON x, FromJSON y) => ByteString -> Maybe (Either x y)
decodeOneOf bs = getFirst (First (fmap Left (decode bs)) <> First (fmap Right (decode bs)))
-- or more straightforwardly
decodeOneOf' :: (FromJSON x, FromJSON y) => ByteString -> Maybe (Either x y)
@aisamanra
aisamanra / madlibs.rs
Last active August 29, 2015 14:12
Simple MadLibs-ey program as Rust practice
use std::io::File;
use std::io::stdio::{stdin,print};
use std::iter::range;
use std::os;
use std::rand::{Rng,task_rng};
use std::vec::as_vec;
/* This program will take a file that describes a 'mad libs template' as
* argument and ask the user for the correct parts of speech in a random
* order, and then stitch the resulting mad lib together. This was a very
@aisamanra
aisamanra / cabal-wrapper.sh
Created January 23, 2015 22:30
Wrapper for making cabal refuse to work outside of sandboxes
#!/bin/sh
# This is a wrapper around cabal which will fail to execute if
# it's not invoked in a sandbox, although this behaviour can be
# worked around (with a warning message) using the command-line
# flag `--trust-me`.
contains_escape() {
# Find out whether the arguments contain --trust-me
ARR=$@
@aisamanra
aisamanra / ghc-wrapper.sh
Created March 27, 2015 21:05
wrapper script for switching between and installing different GHC versions
#!/bin/bash -e
# ghc wrapper script (for managing installed GHC versions)
# this is a small script I use that allows multiple simultaneous ghc
# installations. This makes the following assumptions about how
# you want to set up your system:
# - GHC version {X} is installed with prefix ~/install/ghc-${X}
# - A file naming the current selected GHC version is placed
# at ~/.current-ghc
# - cabal is configured to point to this script instead of ghc
@aisamanra
aisamanra / gidl-mode.el
Created April 9, 2015 00:21
Major mode for editing GIDL files
;; a simple major mode for editing GIDL files.
(setq gidl-font-lock
'(( "def-\\(enum\\|struct\\|newtype\\|interface\\)"
. font-lock-keyword-face )
( "def-\\(enum\\|struct\\|newtype\\|interface\\) (?\\([A-Za-z0-9_-]*\\)"
2 font-lock-function-name-face )
( "[us]int\\(8\\|16\\|32\\|64\\)_t\\|bool_t\\|float_t\\|double_t"
. font-lock-builtin-face)))
@aisamanra
aisamanra / gidl.vim
Created May 7, 2015 22:19
unfinished gidl mode for vim
" Vim syntax file
" Language: GIDL
" Last Change: 2014 April 17
" Maintainer: Getty Ritter <gdritter@galois.com>
if version < 600
syntax clear
elseif exists("b:current_syntax")
finish
endif
;; guile-utf8.scm
;; A quickly hacked together set of functions to turn
;; utf-8-encoded text (which Guile sees as raw bytes) into
;; ASCII-encoded HTML, with a few other functions for
;; getting the code values out of a UTF-8 string.
;; anon-let is a hacky macro to create a new scope while
;; allowing definitions within it to bind in its enclosing
;; scope. It also allows define-local which only defines
;; within that scope. It is used here to close over constants
@aisamanra
aisamanra / parser_nondet.hs
Last active October 16, 2015 20:34
Terrible non-deterministic parser combinators that you shouldn't use
{-# LANGUAGE ParallelListComp #-}
import Data.Char (isDigit)
import Data.List (nub)
import Data.Monoid ((<>))
parse :: Eq a => Parser tk a -> [tk] -> Maybe [a]
parse p tk = case runParser p tk of
[] -> Nothing
xs -> Just $ nub $ map snd xs
module Main where
import Data.List (nub)
import MonadLib
import UI.TCOD.Console
import UI.TCOD.Console.Types
import UI.TCOD.Color
type Pt = (Int, Int)
type St = (Pt, [Pt])