8 people like it.

Scrap Your Boilerplate (with class)

A typecast free experiment in Generic Programming. Inspired by "Scrap Your Boilerplate (with class)".

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
// http://homepages.cwi.nl/~ralf/syb3/

let inline gmap f g  (x : ^R) : ^R = (f ? (g) <- x) 

type Data = Data with
    static member inline ($)(f : ^F, x : ^A) = gmap Data f x

    static member inline (?<-)(Data, f, x : int) = x
    static member inline (?<-)(Data, f, x : string) = x
    static member inline (?<-)(Data, f, x : bool) = x
    static member inline (?<-)(Data, f : _, xs : _ option) = 
        xs |> Option.map (fun x -> f $ x)
    static member inline (?<-)(Data, f : _, (x, y) : (_ * _)) = 
        (f $ x, f $ y)
    static member inline (?<-)(Data, f : _, xs : _ list) = 
        xs |> List.map (fun x -> f $ x)

type Inc = Inc with
    static member inline ($)(f : _, x : _) = gmap Data f x
    static member inline (?<-)(Data, _ : Inc, x : int) = x + 1


type Upper = Upper with
    static member inline ($)(f : _, x : _) = gmap Data f x
    static member inline (?<-)(Data, _ : Upper, x : string) = x.ToUpper()

// Example
gmap Data Data [("nick", 1)] // [("nick", 1)]
gmap Data Inc [("nick", 1)] // [("nick", 2)]
gmap Data Upper [("nick", 1)] // [("NICK", 1)]
val gmap : f:'a -> g:'b -> x:'R -> 'R (requires member ( ?<- ))

Full name: Script.gmap
val f : 'a (requires member ( ?<- ))
val g : 'b (requires member ( ?<- ))
val x : 'R (requires member ( ?<- ))
Multiple items
union case Data.Data: Data

--------------------
namespace Microsoft.FSharp.Data

--------------------
type Data =
  | Data
  static member ( $ ) : f:'F * x:'A -> 'A (requires member ( ?<- ))
  static member ( ?<- ) : Data:Data * f:'a * x:int -> int
  static member ( ?<- ) : Data:Data * f:'a * x:string -> string
  static member ( ?<- ) : Data:Data * f:'a * x:bool -> bool
  static member ( ?<- ) : Data:Data * f:'a * xs:'b option -> '_arg10 option (requires member ( $ ))
  static member ( ?<- ) : Data:Data * f:'a * ('b * 'c) -> '_arg13 * '_arg16 (requires member ( $ ) and member ( $ ))
  static member ( ?<- ) : Data:Data * f:'a * xs:'b list -> '_arg19 list (requires member ( $ ))

Full name: Script.Data
val f : 'F (requires member ( ?<- ))
val x : 'A (requires member ( ?<- ))
val f : 'a
val x : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val x : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val x : bool
type bool = System.Boolean

Full name: Microsoft.FSharp.Core.bool
val f : 'a (requires member ( $ ))
val xs : 'b option (requires member ( $ ))
type 'T option = Option<'T>

Full name: Microsoft.FSharp.Core.option<_>
module Option

from Microsoft.FSharp.Core
val map : mapping:('T -> 'U) -> option:'T option -> 'U option

Full name: Microsoft.FSharp.Core.Option.map
val x : 'b (requires member ( $ ))
val f : 'a (requires member ( $ ) and member ( $ ))
val x : 'b (requires member ( $ ) and member ( $ ))
val y : 'c (requires member ( $ ) and member ( $ ))
val xs : 'b list (requires member ( $ ))
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
Multiple items
union case Inc.Inc: Inc

--------------------
type Inc =
  | Inc
  static member ( $ ) : f:'a * x:'b -> 'b (requires member ( ?<- ))
  static member ( ?<- ) : Data:Data * Inc * x:int -> int

Full name: Script.Inc
val x : 'b (requires member ( ?<- ))
Multiple items
union case Upper.Upper: Upper

--------------------
type Upper =
  | Upper
  static member ( $ ) : f:'a * x:'b -> 'b (requires member ( ?<- ))
  static member ( ?<- ) : Data:Data * Upper * x:string -> string

Full name: Script.Upper
System.String.ToUpper() : string
System.String.ToUpper(culture: System.Globalization.CultureInfo) : string
Raw view Test code New version

More information

Link:http://fssnip.net/bU
Posted:12 years ago
Author:Nick Palladinos
Tags: generic programming , haskell