Skip to content

Instantly share code, notes, and snippets.

@adbrowne
adbrowne / FreeAp.hs
Last active August 30, 2016 01:59
Free Applicative Example
module Main where
import Lib
import Data.Monoid
import Data.Maybe
import qualified Data.Map.Strict as Map
import Control.Concurrent (threadDelay)
import Control.Concurrent.Async
import Control.Applicative.Free
@adbrowne
adbrowne / Church.hs
Created February 11, 2016 01:35
Church encoded free monads vs not
-- runs fast
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Control.Monad
import Control.Monad.Free.Church
import Control.Monad.Free.TH
@adbrowne
adbrowne / Database.sql
Last active August 29, 2015 14:23
Bad idea to rely on finding changes using AutoIncrement columns or DateTimes
CREATE DATABASE [OrderedInsert]
GO
ALTER DATABASE [OrderedInsert] SET ALLOW_SNAPSHOT_ISOLATION ON
GO
USE [OrderedInsert]
GO
CREATE TABLE [dbo].[Test](
@adbrowne
adbrowne / gist:5519816
Last active December 17, 2015 00:19
SICP Exercise 1.3
(define (sum-of-squares a b)
(+ (square a) (square b)))
(define (sum-of-squares-of-max-2 a b c)
(if (> a b)
(if (> b c)
(sum-of-squares a b)
(sum-of-squares a c))
(if (> a c)
(sum-of-squares a b)
@adbrowne
adbrowne / SampleViewModel.coffee
Created July 31, 2012 06:20
Sample CoffeeScript ViewModel for KnockoutJS
class SampleViewModel
constructor: (values) ->
this.title = ko.observable()
this.description = ko.observable()
this.isPrivate = ko.observable()
this.status = ko.observable "Active"
this.showApproveReject = ko.computed => @isActive()
this.statusUrl = ko.observable()
this.privacyUrl = ko.observable()
_.extend(this, Backbone.Events)
@adbrowne
adbrowne / ConstantsGenerator.cs
Created March 18, 2012 03:05
Replacing variables in javascript files with Cassette IAssetTransformer
public class ConstantsGenerator : IAssetTransformer
{
private const char ConstantStartToken = '#';
private const char OpeningBraceToken = '{';
private const char ClosingBraceToken = '}';
private readonly Dictionary<string, string> values;
public ConstantsGenerator(Dictionary<string, string> values)
{
this.values = values;
@adbrowne
adbrowne / CassetteConfiguration.cs
Created March 17, 2012 02:01
Configuring Cassette to work with a CDN
var staticDomain = ConfigurationManager.AppSettings["StaticDomain"];
settings.UrlModifier = new StaticDomainUrlModifier(settings.UrlModifier, staticDomain);
@adbrowne
adbrowne / LazyBadges.hs
Created February 21, 2012 11:56
Haskell HaXml Sample of Lazily Reading XML file (in this case badges file from stackoverflow data dump)
import Text.XML.HaXml.ParseLazy as PL
import Text.XML.HaXml
import Text.XML.HaXml.Posn
import System.IO
main = do
inh <- openFile "badges.xml" ReadMode
hSetEncoding inh utf8_bom
inpStr <- hGetContents inh
let (Document _ _ root _) = PL.xmlParse "badges.xml" inpStr
import Data.Char
import System.Random
main = do
randomGen <- newStdGen
let getR = getRandom randomGen
contents <- getContents
outputStory . (generateStory 10 getR) $ contents
putStrLn ""
@adbrowne
adbrowne / Expr.scala
Created May 24, 2011 13:22
Pattern Matching
abstract class Expr
case class Var(name: String) extends Expr
case class Number(num: Double) extends Expr
case class UnOp(operator: String, arg: Expr) extends Expr
case class BinOp(operator: String, left: Expr, right: Expr) extends Expr
class Plus(left: Expr, right: Expr) extends BinOp("+", left, right)
def simplifyTop(expr: Expr): Expr = expr match {
case UnOp("-", UnOp("-", e)) => e
case BinOp("+", e, Number(0)) => e
case BinOp("*", e, Number(1)) => e