Skip to content

Instantly share code, notes, and snippets.

type [<Measure>] seconds
type Interval = { Name: string; Duration: int<seconds>; Elapsed : int<seconds> }
type Interval with
member x.Remaining = x.Duration - x.Elapsed
type Workout = { Name: string; Intervals : Interval list }
type Workout with
member x.WorkoutTime = x.Intervals |> Seq.sumBy (fun x -> x.Duration)
namespace TibcoMessagePoster.Model
type FixField<'a>(fieldName, fieldId, validator : ('a -> bool)) =
member self.FieldName = fieldName
member self.FieldId = fieldId
member self.Validator = validator
new(fieldName, fieldId) = FixField(fieldName, fieldId, fun _ -> true )
type FixHeaderFields =
static member ApplVerId = FixField("ApplVerId", "1128", fun str -> str = "9")
namespace MvvmCrossTests.Core.FSharp
open System
open Cirrious.MvvmCross.ViewModels
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns
// One way of accessing the name of an expression that can be passed to the RaisePropertyChanged is
(* Maybe Monad *)
(*
Taken from FSharpX:
https://github.com/fsprojects/fsharpx/blob/master/src/FSharpx.Core/ComputationExpressions/Monad.fs
*)
type MaybeBuilder() =
member this.Return(x) = Some x
member this.ReturnFrom(m: 'T option) = m
member this.Zero() = None
member this.Bind(m, f) = Option.bind f m
(* Duck Typing *)
let inline flyAndWalk arg =
let flying = ( ^a : (member Fly : unit -> string) arg)
let walking = ( ^a : (member Walk : unit -> string) arg)
(flying, walking)
type Duck() =
member this.Swim() = "paddling"
member this.Fly() = "flapping"
(* Events *)
type ClassWithEvent() =
let event1 = new Event<_>()
[<CLIEvent>]
member this.Event1 = event1.Publish
let clsWithEvent = ClassWithEvent().Event1.Add ( fun arg -> printfn "Argg" )
let viaReactive = ClassWithEvent().Event1 |> Observable.subscribe( fun arg -> printfn "Arrgg" )
(* Agents *)
type CounterMessage =
| Update of float
| Reset
let inbox = MailboxProcessor.Start(fun agent ->
// Function that implements the body of the agent
let rec loop sum count = async {
// Asynchronously wait for the next message
@andybrackley
andybrackley / ObservableFromEvent_NonStandard.cs
Created March 7, 2014 17:00
C# Convert non standard Event an Observable using Observable.FromEvent and the conversion function
// Demo of how to convert an non-standard event into an Observable Sequence
// by using the Observable.FromEvent overload that uses a conversion Function.
public class SomeObservableClass
{
public delegate void SettingChangedEventHandler(object sender, string setting);
public event SettingChangedEventHandler SettingChanged;
}
void Main()
type IInterface =
abstract theFunc : unit -> string
let init =
{ new IInterface with
member x.theFunc() = "Impl"
}
printfn "%s" <| init.theFunc()
module ComplexTypes =
let backgroundTypes =
dict [ ("Buy", "Blue")
("Sell", "Red")
("My", "Green")
("Their", "Yellow") ]
let stringFormatTypes =
dict [ ("Price", "#.##")
("Qty", "#.0") ]