Skip to content

Instantly share code, notes, and snippets.

View dvdsgl's full-sized avatar
🦋
Gliding

David Siegel dvdsgl

🦋
Gliding
View GitHub Profile
fs = require 'fs'
class MyClass
# Trying to create middleware with access to class members.
withData: (cb) ->
return cb @__data if @__data?
fs.readFile 'datafile.json', (err, json) =>
throw err if err?
@__data = JSON.parse json
@dvdsgl
dvdsgl / Procfile
Created February 25, 2012 21:58
Automated Jenkins Comments on GitHub Pull Requests
web: node server.js
findRoots 0 b c = Nothing
findRoots a b c = Just ((-b + d) / 2*a, (-b - d) / 2*a)
where d = sqrt (b^2 - 4*a*c)
main = print (findRoots 1 6 8)
@dvdsgl
dvdsgl / Quadratic.hs
Created March 26, 2012 18:30
Made easier to 'parse' for Non-haskellers
-- findRoots is a function that takes 3 Doubles and maybe returns a tuple of doubles.
-- Here's its signature, which can be omitted because Haskell infers it:
findRoots :: Double -> Double -> Double -> Maybe (Double, Double)
-- In a C-like language with parameterized types, the type signature might look like:
--
-- static Maybe<Tuple<double,double>> findRoots(double a, double b, double c);
--
-- Maybe is Haskell's 'nullable type'; values of type Maybe t can be thought of as lists
-- containing exactly zero or one value of type t. So, a Maybe Double is either zero or
// Register an API key in your AppDelegate's FinsihedLaunching method:
// GMSServices.ProvideAPIKey ("Your API key.");
GMSMapView mapView;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Create the map view, framing Xamarin headquarters
@dvdsgl
dvdsgl / Shell.fs
Created December 16, 2012 02:06
printf-style function for calling external programs with typesafe commandline formatting. Based on Mauricio Scheffer's "Abusing PrintfFormat in F#", which used the same technique for typesafe SQL queries.
// Demo:
//
// let files = sh "ls -a /Users/david" |> lines
// let install = sh "cp -r %s /Applications"
// install "/Volumes/Awareness/Awareness.app"
//
module Campari.Shell
open System
// I see this a lot in F# code:
control.OnClick.Add (fun _ ->
printfn "%A was clicked" control
)
// The entire lamda is parenthesized so it can be
// passed to Add, resulting in an ugly, dangling ')'.
// F# has a reverse pipeline operator called <| that's
type FizzBuzz =
FizzBuzz | Fizz | Buzz | Nat of int
let (|DivisibleBy|_|) by n =
if n % by = 0 then Some () else None
let fizzbuzz n = match n with
| DivisibleBy 3 & DivisibleBy 5 -> FizzBuzz
| DivisibleBy 3 -> Fizz
| DivisibleBy 5 -> Buzz
@dvdsgl
dvdsgl / RectangleExtensions.cs
Created December 18, 2012 06:37
Extension method for succinct RectangleF mutation expressions.
public static class RectangleExtensions
{
public static RectangleF With (this RectangleF self,
float? X = null, float? Y = null,
float? Width = null, float? Height = null)
{
return new RectangleF (X ?? self.X, Y ?? self.Y, Width ?? self.Width, Height ?? self.Height);
}
static T Id<T> (T t) { return t; }
@dvdsgl
dvdsgl / VolareHelloWorld.fsx
Last active December 10, 2015 06:18
Sketching an API for 'Volare', a Sintra-esque web app DSL in F#, with nods to Express and CoffeeKup.
#r "Volare.dll"
open Volare
get "/" <| fun req res ->
"Hello, world!"
get "/:name" <| fun req res ->
html <| fun _ ->
body <| fun _ ->