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 / LoremIpsum.cs
Last active December 22, 2015 13:08
Need some placeholder text in your Xamarin app?
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Gibberish
{
public static class LoremIpsum
{
const string Corpus = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent ornare id erat a pellentesque. Integer tincidunt velit odio, molestie varius lorem volutpat at. Sed ultricies ligula orci, congue condimentum libero sollicitudin non. Morbi eget nisl facilisis, semper enim in, lacinia ligula. Etiam felis leo, malesuada non consequat ut, dignissim vitae arcu. Quisque a turpis sodales, dignissim nunc posuere, ornare risus. Nunc vitae ligula porttitor, consectetur odio ut, condimentum ipsum.
let trace format = flip Printf.ksprintf format <| fun line ->
if Environment.GetEnvironmentVariable "DEBUG" = "true" then
Console.WriteLine line
module MonkeyBot.Plugins.WhatsForLunch
open System
open DDay.iCal
open MonkeyBot
let ZeroCaterId = "..."
type LunchResponse =
| Lunch of string
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace HexagonApp {
[Register ("AppDelegate")]
class AppDelegate : UIApplicationDelegate {
UIWindow window;
public override bool FinishedLaunching (UIApplication app, NSDictionary options) {
@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
@dvdsgl
dvdsgl / Monads for a C# dev.md
Last active October 10, 2024 20:26
Monads explained (sort of) to a C# developer

A monad is a fancy word for a generic type of the form MyMonad<T> (a generic type of arity 1).

A monad is special because it adds 'special powers' to the T that it wraps. These 'special powers' won't sound very special to an imperative programmer, so you have to squint to see them but bear with me.

  • IEnumerable<T> is a monad that gives values of type T the special power of nondeterminism, or the ability to 'be' multiple values at once.
  • Nullable<T> is a monad that gives values of type T the special power of nullability, or the ability to be absent.
  • Task<T> is a monad that gives values of type T the special power of asynchronicity, or the ability to be used before they are computed.

The trick with monads comes when you want to play with the T values, because they are inside another type. C# introduced language changes to make dealing with values inside these monads easier: