Skip to content

Instantly share code, notes, and snippets.

View dvdsgl's full-sized avatar
🦋
Gliding

David Siegel dvdsgl

🦋
Gliding
View GitHub Profile
@dvdsgl
dvdsgl / PhotoViewController.fs
Created June 10, 2014 16:51
Cirrious.FluentLayout makes iOS Auto Layout... fun! Especially in F#.
open System
open MonoTouch.UIKit
open Cirrious.FluentLayouts.Touch
type PhotoViewController() =
inherit UIViewController()
let imageViewSize = 200.0f
let imageView =
let view = UIImageView(
Device.OnPlatform <| function
| iOS, Tablet ->
this.Padding <- Thickness(0, 0, 0, 0)
| _ -> ()
@dvdsgl
dvdsgl / Writer.fs
Last active August 29, 2015 14:04
Implement Writer monad with custom keyword.
type Writer<'Result, 'Output> =
| W of 'Result * seq<'Output>
type WriterBuilder() =
member this.Zero() = W ((), Seq.empty)
member this.Yield(x) = W (x, Seq.empty)
member this.Return(x) = this.Yield(x)
member this.Bind(W (a, xs), k) =
let (W (b, ys)) = k a
W (b, Seq.append xs ys)
module Trello
( BoardId()
, Credentials()
, Trello()
, runTrello
, Board()
, getBoard
) where
import Data.Maybe
@dvdsgl
dvdsgl / Handler.purs
Created March 3, 2015 21:56
Error in declaration monadEffHandlerM
type ExpressM a = forall e. Eff (express :: Express | e) a
data HandlerM a = HandlerM (Request -> Response -> ExpressM Unit -> ExpressM a)
instance monadEffHandlerM :: MonadEff eff HandlerM where
liftEff act = HandlerM \_ _ _ -> unsafeInterleaveEff act
type ShootOutAnimator(referenceView: UIView) =
let speed = 1300.0f
let animator = UIDynamicAnimator(referenceView)
...
member this.ShootOut(view: UIView, direction: Direction) =
let x, y = direction.UnitVector
let shoot = UIDynamicItemBehavior(view, AngularResistance=2.0f)
shoot.AddLinearVelocityForItem(PointF(x * speed, y * speed), view)
shoot.AddAngularVelocityForItem(x * 3.0f, view)
shoot.Action <- fun () ->
import System (system)
import System.Posix (sleep)
import Text.Printf (printf)
import Flickr.Monad
import Flickr.Types
import Flickr.Photos
import Flickr.URLs (photoSourceURL)
import Flickr.Groups.Pools (getPhotos)
# Lists is an array of sorted lists (arrays):
# [ [...], [...], … ]
def ListMerge(Lists):
# The number of elements awaiting merge in each list.
sizes = [len(L) for L in Lists]
# Create a heap with a slot for each list.
heap = range(len(Lists))
for i in heap:
# Heap elements are (key, value) pairs of (element, List index)
heap[i] = (Lists[i][0], i)
- (NSArray*)mergeSortArrayUsingSelector:(SEL)comparator
{
id buf[[self count]];
[self getObjects:buf];
mergesort_(buf, [self count], comparator);
return [NSArray arrayWithObjects:buf count:[self count]];
}
void mergesort_(id* objs, size_t count, SEL cmp)
{
// Base case.
if (count < 2) return;
size_t left_count = count / 2;
size_t right_count = count – left_count;
id left_objs[left_count];
id right_objs[right_count];
// Function pointer for comparison IMP.