Skip to content

Instantly share code, notes, and snippets.

Avatar

David Siegel dvdsgl

View GitHub Profile
@dvdsgl
dvdsgl / PhotoViewController.fs
Created Jun 10, 2014
Cirrious.FluentLayout makes iOS Auto Layout... fun! Especially in F#.
View PhotoViewController.fs
open System
open MonoTouch.UIKit
open Cirrious.FluentLayouts.Touch
type PhotoViewController() =
inherit UIViewController()
let imageViewSize = 200.0f
let imageView =
let view = UIImageView(
View OnPlatform.fs
Device.OnPlatform <| function
| iOS, Tablet ->
this.Padding <- Thickness(0, 0, 0, 0)
| _ -> ()
@dvdsgl
dvdsgl / Writer.fs
Last active Aug 29, 2015
Implement Writer monad with custom keyword.
View Writer.fs
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)
View Trello.purs
module Trello
( BoardId()
, Credentials()
, Trello()
, runTrello
, Board()
, getBoard
) where
import Data.Maybe
@dvdsgl
dvdsgl / Handler.purs
Created Mar 3, 2015
Error in declaration monadEffHandlerM
View Handler.purs
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
View ShootOutAnimator.fs
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 () ->
View gist:153825
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)
View gist:155094
# 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)
View gist:155095
- (NSArray*)mergeSortArrayUsingSelector:(SEL)comparator
{
id buf[[self count]];
[self getObjects:buf];
mergesort_(buf, [self count], comparator);
return [NSArray arrayWithObjects:buf count:[self count]];
}
View gist:155103
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.