Skip to content

Instantly share code, notes, and snippets.

Gustavo Leon gusty

Block or report user

Report or block gusty

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@gusty
gusty / curry.fsx
Created Jul 9, 2019
Polyvariadic curry / uncurry
View curry.fsx
#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
@gusty
gusty / io-monad.fsx
Last active May 28, 2019 — 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)
View io-monad.fsx
#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 / isNull.fsx
Created Jun 15, 2018
Efficiently constrained isNull
View isNull.fsx
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
View fsharpplus_haskell_compatibility.md
+--------------------+--------------------+-------------------------+--------------------+
|Operation           | F#+ / F#           |F#+ Haskell Compatibility|Haskell             |
+====================+====================+=========================+====================+
|List.append         | @                  |                         | ++                 |
+--------------------+--------------------+-------------------------+--------------------+
|Function composition| f << g             | f . (g)                 | f . g              |
+--------------------+--------------------+-------------------------+--------------------+
|                    | <|                 | $                       | $                  |
+--------------------+--------------------+-------------------------+--------------------+
@gusty
gusty / memoization.fsx
Last active Mar 20, 2018
Polyvariadic Memoization
View memoization.fsx
open System.Collections.Concurrent
type T = T with static member getOrAdd (cd:ConcurrentDictionary<_,'t>) (f:_ -> _) k = cd.GetOrAdd (k, f)
let inline memoize (f:'``(T1 -> T2 -> ... -> Tn)``): '``(T1 -> T2 -> ... -> Tn)`` = (T $ Unchecked.defaultof<'``(T1 -> T2 -> ... -> Tn)``>) f
type T with
static member ($) (_:obj, _: 'a -> 'b) = T.getOrAdd (ConcurrentDictionary ())
static member inline ($) (T, _:'t -> 'a -> 'b) = T.getOrAdd (ConcurrentDictionary ()) << (<<) memoize
@gusty
gusty / QueenSongs.linq
Created Nov 4, 2016
Sample WebLinq query. All Queen songs from Wikipedia.
View QueenSongs.linq
(* Namespaces
TryParsers
WebLinq
WebLinq.Collections
WebLinq.Html
WebLinq.Sys
WebLinq.Text
WebLinq.Xml
WebLinq.Xsv
@gusty
gusty / tuple.fsx
Last active Nov 2, 2016
Generic Tuple functions
View tuple.fsx
// 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 / polyvariadic.fsx
Last active Dec 20, 2018
Polyvariadic functions in F#
View polyvariadic.fsx
// 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
View genericCDF.fsx
#r "FsControl.Core.dll"
#r "FSharpPlus.dll"
open FSharpPlus
open GenericMath
open FsControl.Core.Types
open FsControl.Core.Types.Ratio
module WillBeExposedInTheNextReleaseOfFSharpPlus =
@gusty
gusty / zipIndex.fsx
Last active Aug 29, 2015
Solve stack overflow in zipIndex function.
View zipIndex.fsx
#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
You can’t perform that action at this time.