Skip to content

Instantly share code, notes, and snippets.

@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
@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 / 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
// --------
#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 / 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
@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 / genericCDF.fsx
Last active November 17, 2022 05:15
Generic CDF
#r "nuget: FSharpPlus,1.2"
open FSharpPlus
open FSharpPlus.Math.Generic
type Ratio =
struct
val Numerator : bigint
val Denominator : bigint
new (numerator: bigint, denominator: bigint) = {Numerator = numerator; Denominator = denominator}
@gusty
gusty / polyvariadic.fsx
Last active July 20, 2023 15:19
Polyvariadic functions in F#
// Unfortunatelly it stopped working in F# 4.1 after this PR https://github.com/Microsoft/visualfsharp/pull/1650
// Will ask to revert it
type FoldArgs<'t> = FoldArgs of ('t -> 't -> 't)
let inline foldArgs f (x:'t) (y:'t) :'rest = (FoldArgs f $ Unchecked.defaultof<'rest>) x y
type FoldArgs<'t> with
static member inline ($) (FoldArgs f, _:'t-> 'rest) = fun (a:'t) -> f a >> foldArgs f
static member ($) (FoldArgs f, _:'t ) = f
@gusty
gusty / tuple.fsx
Last active March 26, 2021 10:10
Generic Tuple functions
// Based on http://nut-cracker.azurewebsites.net/blog/2011/11/07/functions-for-n-tuples/
// Warning: This script has long compile times
// but as from F# 4.1 will work fine
// due to this fix: https://github.com/Microsoft/visualfsharp/pull/1682 in the compiler
open System
type Infinite<'a> = Infinite of 'a
module TupleInternalValues =
@gusty
gusty / QueenSongs.linq
Created November 4, 2016 12:36
Sample WebLinq query. All Queen songs from Wikipedia.
(* Namespaces
TryParsers
WebLinq
WebLinq.Collections
WebLinq.Html
WebLinq.Sys
WebLinq.Text
WebLinq.Xml
WebLinq.Xsv