Skip to content

Instantly share code, notes, and snippets.

#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 / 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 / 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
// --------
@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
@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 / isNull.fsx
Created June 15, 2018 10:25
Efficiently constrained isNull
type A = class end
type B = class inherit A end
type C = class inherit B end
type D = class inherit C end
type IsNull =
inherit D
static member inline ($) (_:A , x :'a when 'a : not struct) = System.Object.ReferenceEquals(x, null)
static member inline ($) (_:B , x :'a when 'a : null ) = match x with null -> true | _ -> false
static member inline ($) (_:C , _:D) = false
@gusty
gusty / io-monad.fsx
Last active May 28, 2019 07:20 — forked from isaacabraham/io-monad.fsx
F# port of the first half of John De Goes "FP to the max" (https://www.youtube.com/watch?v=sxudIMiOo68)
#load @".paket\load\net452\FSharpPlus.fsx"
open FSharpPlus
open System
[<AutoOpen>]
module SideEffects =
let private r = Random()
let printLn text = async { printfn "%s" text }
let readLn() = async { return Console.ReadLine() }
@gusty
gusty / curry.fsx
Created July 9, 2019 22:13
Polyvariadic curry / uncurry
#nowarn "0042"
let inline retype (x: 'T) : 'U = (# "" x: 'U #)
open System
type Curry =
static member inline Invoke f =
let inline call_2 (a: ^a, b: ^b) = ((^a or ^b) : (static member Curry: _*_ -> _) b, a)
call_2 (Unchecked.defaultof<Curry>, Unchecked.defaultof<'t>) (f: 't -> 'r) : 'args
+--------------------+--------------------+-------------------------+--------------------+
|Operation           | F#+ / F#           |F#+ Haskell Compatibility|Haskell             |
+====================+====================+=========================+====================+
|List.append         | @                  |                         | ++                 |
+--------------------+--------------------+-------------------------+--------------------+
|Function composition| f << g             | f . (g)                 | f . g              |
+--------------------+--------------------+-------------------------+--------------------+
|                    | <|                 | $                       | $                  |
+--------------------+--------------------+-------------------------+--------------------+