Skip to content

Instantly share code, notes, and snippets.

@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
type First =
{ Name: string
Items: string list }
type Second =
{ Name: string
Numbers: int list }
open FSharpPlus
@battermann
battermann / free.fsx
Last active February 17, 2022 23:26
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>
@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
// 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#.
@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
@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
#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
@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
// Install http://www.nuget.org/packages/Fleece
open Fleece
open FSharpPlus
let json = """{
"1": "one",
"2": "two",
"3": "three"
}"""