Skip to content

Instantly share code, notes, and snippets.

View eiriktsarpalis's full-sized avatar
🌴
On vacation

Eirik Tsarpalis eiriktsarpalis

🌴
On vacation
View GitHub Profile
@eiriktsarpalis
eiriktsarpalis / retryAsync.fsx
Last active December 17, 2015 22:49
retry async
open System
type RetryPolicy = Policy of (int -> exn -> TimeSpan option)
/// retries given action based on given policy
let retryAsync (Policy p) (f : Async<'T>) =
let rec aux retries =
async {
let! result = Async.Catch f
@eiriktsarpalis
eiriktsarpalis / fsharp-listings.tex
Created August 13, 2013 19:10
LaTeX F# definition for Listings package
\usepackage{listings}
\usepackage{upquote}
\lstdefinelanguage{FSharp}%
{morekeywords={let, new, match, with, rec, open, module, namespace, type, of, member, %
and, for, while, true, false, in, do, begin, end, fun, function, return, yield, try, %
mutable, if, then, else, cloud, async, static, use, abstract, interface, inherit, finally },
otherkeywords={ let!, return!, do!, yield!, use!, var, from, select, where, order, by },
keywordstyle=\color{bluekeywords},
sensitive=true,
@eiriktsarpalis
eiriktsarpalis / fav.fsx
Created October 29, 2013 13:15
My favorite new feature in F# 3.1
type Foo =
| A of value : int
| B of enabled : bool * somethingElse : bool
match B (false, false) with
| B(enabled = true) -> true
| B(somethingElse = x) -> x
| _ -> false
@eiriktsarpalis
eiriktsarpalis / nostrongnames.fsx
Created January 25, 2014 13:14
FsPickler without strong names
open FsPickler
let settings = new CustomPicklerRegistry("noStrongNames")
let nostrongnames =
{
new ITypeNameConverter with
member __.ToDeserializedType tI = tI
member __.OfSerializedType (tI : TypeInfo) =
{
// stackless raise
let inline raise (e : exn) = (# "throw" e : ^T #)
let inline failwith ctor = raise << ctor
let inline failwithf ctor fmt = Printf.ksprintf (raise << ctor) fmt
// 1
exception SomeOtherException of string
with override e.Message = e.Data0
@eiriktsarpalis
eiriktsarpalis / unsound.fsx
Created October 29, 2014 18:33
F# bug reproduction
type Foo<'T>(bar (* add explicit type annotation to fix *), value : 'T) =
member __.Value = value
and Bar () as self =
let create (x : 'T) : Foo<'T> =
printfn "%O" typeof<'T>
new Foo<'T>(self, x)
member __.Create<'T>(x : 'T) : Foo<'T> = create x
namespace Repro
{
[CompilationMapping(SourceConstructFlags.ObjectType)]
[Serializable]
public class Foo<T>
{
internal T value;
public T Value
{
get
@eiriktsarpalis
eiriktsarpalis / groupWhile.fs
Created November 17, 2014 23:38
groupwhile
type Stream<'T> = ('T -> unit) -> unit
module Stream =
let inline groupWhile (pred : 'T -> bool) (source : Stream<'T>) : Stream<'T []> =
fun k ->
let ra = new ResizeArray<'T> ()
let iter (t : 'T) =
if not <| pred t then
k <| ra.ToArray()
ra.Clear()
@eiriktsarpalis
eiriktsarpalis / gist:0d54b7c06909514bd378
Created December 28, 2014 15:01
Functional pipelines & F# member constraints
let inline map (alg : ^Alg) (f : ^t -> ^s) (ts : ^ts) : ^ss =
(^Alg : (member Map : (^t -> ^s) * ^ts -> ^ss) (alg, f, ts))
let inline filter (alg : ^Alg) (f : ^t -> bool) (ts : ^ts) : ^ts =
(^Alg : (member Filter : (^t -> bool) * ^ts -> ^ts) (alg, f, ts))
let inline reduce (alg : ^Alg) (f : ^t -> ^t -> ^t) (ts : ^ts) : ^t =
(^Alg : (member Reduce : (^t -> ^t -> ^t) * ^ts -> ^t) (alg, f, ts))
// type signature is unreadable, machine consumable only
@eiriktsarpalis
eiriktsarpalis / newtype.fs
Last active August 29, 2015 14:14
NewType in F#
// current type abbreviations in F#
type A = Guid
type B = Guid
let foo (x : A) = x.ToString()
let b = B.NewGuid()
foo(b) // type checks
// proposed newtype abbreviations
newtype A = Guid