Skip to content

Instantly share code, notes, and snippets.

View tcsavage's full-sized avatar
🦡

Tom Savage tcsavage

🦡
View GitHub Profile
@tcsavage
tcsavage / MonadTransformersExample.hs
Last active March 25, 2022 19:44
Example of ReaderT monad transformer
module Main where
import Control.Monad (replicateM)
import Control.Monad.Trans (lift)
import Control.Monad.Reader (ReaderT, ask, runReaderT)
import Control.Monad.Random (Rand, getRandomR, evalRand)
import System.Random (getStdGen, StdGen)
import Text.Printf
op :: ReaderT Int (Rand StdGen) String
@tcsavage
tcsavage / Maybe.cpp
Created December 12, 2012 16:09 — forked from anonymous/Maybe.cpp
Added `bind` function using C++11 lambdas.
#include <iostream>
#include <functional>
// Polymorphic Maybe type.
template <typename T>
class Maybe
{
public:
// Returns True if Maybe contains a value.
virtual bool isJust() = 0;
@tcsavage
tcsavage / gist:4078346
Created November 15, 2012 12:10
Sphere intersection snippet
{-
Minimum distance for ray intersection to be considered important.
This is due to limited numerical precision with floating point numbers. A ray could be reflected and then be immediately registered as intersecting with the object it just bounced off.
-}
epsilon :: Scalar
epsilon = 0.1
{-|
Defines a mathematical sphere.
-}
-- | Instantiates the WebCore singleton with a set of configuration parameters.
initialize :: Config -> IO ()
initialize conf
| conf == defaultConfig = awe_webcore_initialize_default
| otherwise = go conf
where
go :: Config -> IO ()
go = do
-- Operating in the (->) monad.
a <- fmap fromBool pluginsEnabled
{-# LANGUAGE OverloadedStrings #-}
module BlazeHelpers where
import Text.Blaze ((!))
import Text.Blaze.Html5
import qualified Text.Blaze.Html5 as H
import Text.Blaze.Html5.Attributes
import qualified Text.Blaze.Html5.Attributes as A
import Text.Blaze.Renderer.Pretty
@tcsavage
tcsavage / setup.sh
Created July 19, 2012 01:01
Pangolin setup
#! /bin/bash
# Clone the repo
git clone https://github.com/tcsavage/pangolin.git .
# Ensure we're on uopcomputing
git checkout uopcomputing
# Setup config
cp config.sample.php config.php
@tcsavage
tcsavage / req.php
Created July 11, 2012 11:03
Get HTTP request headers
<?php
header("Content-type: text/plain");
foreach (apache_request_headers() as $k => $v)
{
echo("$k: $v\n");
}
?>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DataSource = Enum.GetValues(typeof(MyEnum));
@tcsavage
tcsavage / gist:2002113
Created March 8, 2012 17:09
enumliststring.cs
string outputString = "";
foreach (var e in enumList)
{
outputString += e.ToString() + ", ";
}
@tcsavage
tcsavage / reverse.hs
Created February 13, 2012 16:58
Simple Haskell script for UNIX-like systems. Commented for education.
#! /usr/bin/runhaskell
module Main where
-- As with many programming languages, the main function is the entry point for execution.
main :: IO ()
main = do -- "do notation" is a way of sequencing functions in haskell to simulate imperitive programming.
inp <- getContents -- Here we bind the resulting thunk from getContents to the name 'inp'.
putStrLn $ reverse inp -- Here we reverse the evaluated value of 'inp' and send it to stdout.