Skip to content

Instantly share code, notes, and snippets.

View gusty's full-sized avatar

Gus gusty

  • Switzerland
View GitHub Profile
@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
#I @"g:\prg\Fleece\Fleece\bin\Release\"
#r @"FsControl.Core.dll"
#r @"FSharpPlus.dll"
#r @"System.Json.dll"
#r @"ReadOnlyCollectionsInterfaces.dll"
#r @"ReadOnlyCollectionsExtensions.dll"
#r @"Fleece.dll"
open System
open System.Collections.Generic
@atifaziz
atifaziz / MSubstitute.bas
Last active June 29, 2016 12:40
SUBSTITUTE-like VBA function for Excel except works with a range of texts to find and replace
Option Explicit
' MSubstitute is like the SUBSTITUTE function of Excel except it allows
' multiple substitutions at once. It replaces all occurrences of texts in the
' first column of SubstitutionsRange in Text with the corresponding text in
' second column.
Public Function MSubstitute(ByVal Text As String, _
ByVal SubstitutionsRange As Range, _
Optional ByVal IgnoreCase As Boolean = True) As String
@dsyme
dsyme / gist:bfed2eed788c7ba58ccc
Last active July 4, 2022 22:23
Naked type aliases can add and name constraints
// This F# language suggestion wants a way to name collections of constraints:
// http://fslang.uservoice.com/forums/245727-f-language/suggestions/8509687-add-constraints-as-a-language-construct
//
// This is a type alias X<T> = T, so X<int> = int etc.
type X<'T> = 'T
// This is a type alias X<T> = T which adds a constraint
type WithStruct<'T when 'T : struct> = 'T
// So called van Laarhoven lenses, named after their discoverer, have a number
// of nice properties as explained by Russell O'Connor:
//
// http://r6.ca/blog/20120623T104901Z.html
//
// Unfortunately their typing (in Haskell)
//
// type Lens s t a b = forall f. Functor f => (a -> f b) -> (s -> f t)
//
// seems to be well outside of what can be achieved in F#.
@rgrempel
rgrempel / Main.purs
Created June 30, 2016 22:41
Alternative approach to effect types in Purescript
module Control.Monad.Eff.Alt where
import Control.Bind (bind)
import Control.Monad.Eff.Exception (Error)
import Partial.Unsafe (unsafeCrashWith)
import Prelude (liftA1, class Functor, class Applicative, class Monad, ap, class Apply, class Bind, class Show, Unit)
-- This illustrates an alternative way to use the type system to model
-- effects, where the effects type is a "regular" type, and the effects
@battermann
battermann / free.fsx
Last active November 9, 2024 13:13
Free Monad like pattern in F#
#load @"paket-files/fsprojects/Chessie/src/Chessie/ErrorHandling.fs"
type Continuation<'output, 'next> = 'output -> 'next
module TerminalDsl =
open Chessie.ErrorHandling
type Terminal<'next> =
| WriteLine of string * Continuation<unit, 'next>
| ReadLine of unit * Continuation<string, 'next>
type First =
{ Name: string
Items: string list }
type Second =
{ Name: string
Numbers: int list }
open FSharpPlus
@szoio
szoio / EventSourcing.scala
Created February 23, 2017 11:17
Event Sourcing with Free Monads
package eventsourcing
import java.time.Instant
import cats._
import cats.data.Coproduct
import cats.free.{Free, Inject}
import cats.implicits._
import doobie.imports._
import fs2.Stream
@panesofglass
panesofglass / apply.fsx
Last active August 21, 2017 22:34
F# async applicative using a custom operation
open System
type FSharp.Control.AsyncBuilder with
[<CustomOperation("and!", IsLikeZip=true)>]
member __.Merge(x, y, f) =
async {
let! token = Async.CancellationToken
let! x' = Async.StartChildAsTask x
let! y' = Async.StartChildAsTask y
do System.Threading.Tasks.Task.WaitAll([|x';y'|], cancellationToken = token)