Skip to content

Instantly share code, notes, and snippets.

@eulerfx
eulerfx / app.fsx
Created March 26, 2014 00:36
F# http service with HyperF
#load "HyperF.fsx"
open HyperF
open Route
let service =
[
Get("/resource/:id") => fun (req,ri) -> "hello world!" |> HttpRes.plainText
@eulerfx
eulerfx / 0_Use.fs
Last active August 29, 2015 13:57
F# random value combinators based on state monad and computation workflows
type Event = {
name : string
date : DateTime
code : int
}
let randEvent : Rand<Event> =
let name = Rand.String (Rand.IntRange 10 15)
let date = Rand.DateTime (DateTime(2014, 3, 26)) (DateTime(2015, 3, 26))
let code = Rand.IntRange 1000 9999
@eulerfx
eulerfx / 0_Use.fs
Last active August 29, 2015 13:58
F# CSV parsing and validation
type MerchantSkuImported with
static member Zero = { MerchantSkuImported.id = null ; merchant_id = null ; merchant_sku = null ; upc = null ; manufacturer = null ; brand = null ; part_no = null ; title = null ; description = null ; bullets = List.empty ; category = 0 ; category_path = List.empty ; multi_pack_quantity = None ; shipping_weight = None ; package_length = None ; package_width = None ; package_height = None ; main_image_url = None ; other_image_url = None ; price = None ; quantity = None ; start_selling_date = None }
static member MerchantSku = (fun (x:MerchantSkuImported) -> x.merchant_sku) |> Lens.create <| fun v x -> { x with merchant_sku = v }
static member UPC = (fun (x:MerchantSkuImported) -> x.upc) |> Lens.create <| fun v x -> { x with upc = v }
static member Title = (fun (x:MerchantSkuImported) -> x.title) |> Lens.create <| fun v x -> { x with title = v }
static member Brand = (fun (x:MerchantSkuImported) -> x.brand) |> Lens.create <| fun v x -> { x with brand = v }
s
@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 -> ().

@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 / 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 / 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 / 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 / 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>>