Skip to content

Instantly share code, notes, and snippets.

module Event =
open System.Threading
let concat (source: IEvent<IEvent<_,_>>) =
let event = new Event<_>()
source.Add (Event.listen event.Trigger)
event.Publish
let collect f =
Event.map f >> concat
@panesofglass
panesofglass / CircularBuffer.fsx
Created January 20, 2012 17:31
Circular Buffer in F#
#r "System.dll"
open System
open System.Diagnostics
// NOTE: A special version for primitives would increase
// performance for primitive types, especially for I/O,
// byte-based operations.
// [snippet: Circular Buffer]
type CircularBuffer<'a> (bufferSize: int) =
@ramnathv
ramnathv / gh-pages.md
Created March 28, 2012 15:37
Creating a clean gh-pages branch

Creating a clean gh-pages branch

This is the sequence of steps to follow to create a root gh-pages branch. It is based on a question at [SO]

cd /path/to/repo-name
git symbolic-ref HEAD refs/heads/gh-pages
rm .git/index
git clean -fdx
echo "My GitHub Page" &gt; index.html
@zeux
zeux / diaenum.fs
Created April 22, 2012 22:34
F# generic enumeration helper
// helpers to iterate IDiaEnum* objects
let inline toSeq (o: ^T) =
let e = { new IEnumerable with member this.GetEnumerator () = (^T: (member GetEnumerator: unit -> _) (o)) }
if true then
Seq.cast e
else
// Dummy expression to constrain return type to that of o.Item
seq [| (^T: (member Item: _ -> _) (o, Unchecked.defaultof<_>)) |]
@mausch
mausch / gist:3188428
Created July 27, 2012 14:43
Async exception handling in F#
open System
open System.Net
// exception handling in async using Async.Catch
let fetchAsync (name, url:string) =
async {
let uri = new System.Uri(url)
let webClient = new WebClient()
let! html = Async.Catch (webClient.AsyncDownloadString(uri))
match html with
@mausch
mausch / gist:4260932
Created December 11, 2012 18:40
Lenses as category in F#
#r @"bin\debug\FsControl.Core.dll" // from https://github.com/gmpl/FsControl
open System
open FsControl.Core.Abstractions
// generic composition operators for all categories
let inline (>>) g f = Inline.instance (Category.Comp, f) g
let inline (<<) f g = Inline.instance (Category.Comp, f) g
// Lens definition
#r @"bin\debug\FsControl.Core.dll" // from https://github.com/gmpl/FsControl
open System
open FsControl.Core.Abstractions
// generic semigroup operator
let inline mappend x y = Inline.instance (Monoid.Mappend, x) y
// generic functor operators
let inline fmap f x = Inline.instance (Functor.Fmap, x) f
@zecl
zecl / Program.fsx
Created March 31, 2013 13:03
FsControl (https://github.com/gmpl/FsControl) を拡張してお遊び
#r @"bin\Debug\FsControl.Core.dll" // from https://github.com/gmpl/FsControl
module Monad =
open FsControl.Core.Abstractions
let do' = new Monad.DoNotationBuilder()
module MonadPlus =
open Monad
open FsControl.Core.Abstractions.MonadPlus
@thinkbeforecoding
thinkbeforecoding / Zip computation
Created January 20, 2014 23:30
This is an example of the Zip (applicative functor) experimental extension to Computation Expression. using for .. and ... do, sources are merges using the .Merge(xs,f) method. If Select is defined and the rest of the expression is a simple projections, the result is Select(Merge(xs,ys), projection) else it is Bind(Merge(xs,ys), rest of the expr…
open System
type ZipBuilder() =
member t.Zip(xs,ys) = List.zip xs ys
member t.For(xs,f) = List.collect f xs
member t.Yield x = [x]
member t.Select(xs,f) =
List.map f xs
member t.Zero() = []
// Install http://www.nuget.org/packages/Fleece
open Fleece
open FSharpPlus
let json = """{
"1": "one",
"2": "two",
"3": "three"
}"""