Wednesday, October 10, 2012

Combinators over Records and Unions

In the previous post, I discussed designing combinator libraries that compose some property over unions. It is only fitting to throw records in the mix.

type U =
    | A of int
    | B of float
    | C of string

let UFormat =
    (
        UnionCase A IntFormat <<
        UnionCase B FloatFormat <<
        UnionCase C StringFormat
    )
    |> Union (fun a b c x ->
        match x with
        | A x -> a x
        | B x -> b x
        | C x -> c x)

type R =
    {
        A : int
        B : float
        C : string
    }

let RFormat : Format<R> =
    (
        RecordField (fun r -> r.A) IntFormat <<
        RecordField (fun r -> r.B) FloatFormat <<
        RecordField (fun r -> r.C) StringFormat
    )
    |> Record (fun a b c -> { A = a; B = b; C = c })

With some simplifications, here is the code:

1 comment:

  1. Hotel and Casino Las Vegas Map & Floor Plans - MapYRO
    A Casino Las 광양 출장샵 Vegas Floor Plan. 경상북도 출장샵 1. 1. Mapyro. 문경 출장마사지 3. Hotel 원주 출장샵 and 경산 출장안마 Casino Las Vegas, NV (South Tower)

    ReplyDelete