Skip to content

Instantly share code, notes, and snippets.

@eulerfx
eulerfx / HopacRx.fs
Last active August 29, 2015 14:09
Push sequences with F# Hopac
namespace Marvel.Hopac
open System
open Hopac
open Hopac.Extra
open Hopac.Job.Infixes
/// An effectful observer.
type RxObs<'a> = 'a option -> Job<unit>
@eulerfx
eulerfx / HopacSeq.fs
Last active August 29, 2015 14:09
Hopac-based seq merge
namespace Marvel.Hopac
open Hopac
open Hopac.Extra
open Hopac.Job.Infixes
open Hopac.Alt.Infixes
/// Different representation of EagerSeq<'a>
type HopacSeq<'a> = Job<HopacSeqStep<'a>>
@eulerfx
eulerfx / AsyncSeqVsScalaz-stream.md
Last active March 4, 2016 21:48
Informal proof that F# AsyncSeq is equal in expressive power to scalaz-stream Process when specialized to Task

There is a great purely functional streaming/IO library in Scala called scalaz-stream. It is itself based upon a Haskell library called machines. They provide powerful abstractions to express compositional effectful computations. These libraries rely on certain type system features in both Scala and Haskell which are unavailable in F# - namely existential types and higher kinds. Higher kinds allow the monad representing the side-effects to be abstracted over. If however we specialize this to a specific monad, we can get around the lack of existentials as well.

Tomas Petricek created a type called asynchronous sequence which provides similar capabilities. The F# AsyncSeq type is declared as follows:

type AsyncSeq<'a> = Async<AsyncSeqInner<'a>>

and AsyncSeqInner<'a> = 
    | Nil
 | Cons of 'a * AsyncSeq&lt;'a&gt;
@eulerfx
eulerfx / 0_Usage.fs
Last active August 29, 2015 14:05
Imperative style update syntax for FSharp.Data JsonValue using lenses
let jsonStr = """
{
"itemSize": {
"width": "hello"
},
"tags": ["foo","bar"]
}
"""
let json = JsonValue.Parse jsonStr
@eulerfx
eulerfx / EventSourcingMonoids.fs
Last active April 8, 2024 10:48
F# event-sourcing with monoids
type Monoid<'a> = {
unit : 'a
op : 'a -> 'a -> 'a
}
let endo<'a> =
{ unit = id
op = fun (f:'a -> 'a) (g:'a -> 'a) -> f << g }
@eulerfx
eulerfx / 0_LensStateMutation_Usage.fs
Last active August 29, 2015 14:03
Imperative-style update syntax with F# lenses and state/costate.
type MerchantSku = {
name : string
price : decimal
} with
static member Name = Lens.create (fun x -> x.name) (fun v x -> { x with name = v})
static member Price = Lens.create (fun x -> x.price) (fun v x -> { x with price = v})
let sku = { name = "Shaving Cream" ; price = 10m }
@eulerfx
eulerfx / EventStore.fsi
Last active August 29, 2015 14:03
F# EventStore API
type Stream = string
type EventType = string
type ExpectedVersion = int
type EventData = byte[]
type EventMetadata = byte[]
type ResolveLinks = bool
type From = int
type BatchSize = int
type BufferSize = int
type CheckpointStore = (unit -> Async<int option>) * (int -> Async<unit>)
@eulerfx
eulerfx / JsonValueCodecSyntax.fs
Created June 23, 2014 21:51
F# FSharp.Data.JsonValue codec syntax
/// port of https://github.com/mausch/Fleece
type ToJsonClass = ToJsonClass
type FromJsonClass = FromJsonClass
type ParseResult<'a> = Choice<'a, string>
module ParseResult =
@eulerfx
eulerfx / JsonCursor.fs
Created June 23, 2014 13:37
FSharp.Data JsonValue zipper
type JsonPath =
| Top /// The root of the JsonValue.
| Node of i:int * parent:JsonValue * path:JsonPath /// A node within a JsonValue: a record property or an array item.
type JsonZipper = JsonZipper of current:JsonValue * path:JsonPath
module JsonCursor =
let private expectRecord() = failwith "Invalid cursor state: JsonValue.Record expected!"
@eulerfx
eulerfx / AsyncIO.md
Last active August 29, 2015 14:02
Rx effectful observers

Synchronous

  • () -> (() -> a) - sequence (IEnumerable)

with arrows reversed:

  • (a -> ()) -> () - observable (IObservable)

Everything is fine and we have duality between pull and push. In particular, () -> a is dual to a -> ().