Skip to content

Instantly share code, notes, and snippets.

;; 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 / untyped.rs
Last active April 10, 2018 23:45
Basic Rust implementation of an interpreter for the untyped lambda calculus
// A basic intepreter for the untyped lambda calculus
enum Term
{ Num(int)
, Var(~str)
, Lam(~str, ~Term)
, App(~Term, ~Term)
, Let(~str, ~Term, ~Term)
}
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])
@aisamanra
aisamanra / jsonfs.hs
Created November 13, 2014 07:17
Quick-and-dirty program to mount JSON as a read-only file system
{-# LANGUAGE OverloadedStrings #-}
-- WARNING! This is very bad, quickly-written code, and should not be
-- trusted to do anything right! It does not support writing, and still
-- has several problems even for reading. Also it's ugly and bad.
import qualified Data.ByteString as BSS
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy as BS
import Data.Char (isDigit)
@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 / callbacks.rs
Last active May 3, 2024 20:24
Creating a HashMap of closures in Rust
#![feature(unboxed_closures)]
#![feature(core)]
#![feature(io)]
use std::old_io::stdio::{stdin};
use std::collections::HashMap;
// This is our toy state example.
#[derive(Debug)]
struct State {
@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