Skip to content

Instantly share code, notes, and snippets.

@gusty
gusty / zipIndex.fsx
Last active August 29, 2015 14:01
Solve stack overflow in zipIndex function.
#r @"c:/packages/FsControl.1.0.9/lib/net40/FsControl.Core.dll"
open FsControl.Core.TypeMethods
open FsControl.Core.Types
open FsControl.Operators
type MonadBuilder() =
member inline b.Return(x) = result x
member inline b.Bind(p,rest) = p >>= rest
member b.Let (p,rest) = rest p
@gusty
gusty / trampoline.fsx
Last active February 27, 2024 08:04
Trampolines in F#
let trampoline f c n =
let rec loop = function
| Choice1Of2 x -> x
| Choice2Of2 x -> loop (f x)
loop (Choice2Of2 (c,n))
// Test
let factorial n =
let rec factorialT (current, n) =
if n = bigint 0 then Choice1Of2 current
#r @"C:\packages\FsControl.1.0.8\lib\net40\FsControl.Core.dll"
type Eval = Eval of int
let eval (Eval x) = x
// To define a Type Method we need to add at least 2 instances
type Eval' = Eval' of int
let eval' (Eval' x) = x
module ExpAlg =
@gusty
gusty / Functors and Applicatives.fsx
Last active December 29, 2015 06:39
Functors and Applicatives.
// FUNCTORS AND APPLICATIVES
//--------------------------
// You may run this script step-by-step
// The order of execution has to be respected since there are redefinitions of functions and operators
// --------
// FUNCTORS
// --------
@gusty
gusty / Program.fsx
Last active December 28, 2015 12:39 — forked from zecl/Program.fsx
#r @"bin\Debug\FsControl.Core.dll" // from https://github.com/gmpl/FsControl
let inline return' x = Inline.instance FsControl.Core.TypeMethods.Applicative.Pure x
let inline (>>=) x (f:_->'R) : 'R = Inline.instance (FsControl.Core.TypeMethods.Monad.Bind, x) f
let inline mzero () = Inline.instance FsControl.Core.TypeMethods.MonadPlus.Mzero ()
let inline mplus (x:'a) (y:'a) : 'a = Inline.instance (FsControl.Core.TypeMethods.MonadPlus.Mplus, x) y
module Monad =
open FsControl.Core.TypeMethods
@gusty
gusty / Validation.fsx
Last active January 6, 2018 10:18 — forked from mausch/Validation.fsx
Applicative Validation easy with F#+
#r @"c:/packages/FSharpPlus.1.0.0-CI00099/lib/net45/FSharpPlus.dll"
open System
open FSharpPlus
// Validation definition
type Validation<'a,'e> = Success of 'a | Failure of 'e
with
// Validation is an instance of Applicative
static member inline Return x = Success x