3 people like it.

MOИOIД

Playing around Fable's experimental implicits

 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: 
// https://docs.scala-lang.org/tour/implicit-parameters.html

open Fable.Core
open Fable.Core.Experimental

type Monoid<'a> =
    abstract member add: 'a -> 'a -> 'a
    abstract member unit : 'a

type ImplicitTest =
    static member sum (xs: 'a list, [<Inject; Implicit>] ?m : Monoid<'a>) =
        let m = m.Value // unwrap instance
        xs |> List.fold m.add m.unit

let [<Implicit>] intMonoid = 
    { new Monoid<int> with
        member this.add (x: int) (y: int) = x + y
        member this.unit = 0 }

let [<Implicit>] stringMonoid = 
    { new Monoid<string> with
        member this.add (x: string) (y: string) = x + y
        member this.unit = "" }      

printfn "%d" (ImplicitTest.sum [1; 2; 3]) // 6
printfn "%s" (ImplicitTest.sum ["a"; "b"; "c"]) // abc
namespace Microsoft.FSharp.Core
Multiple items
type ExperimentalAttribute =
  inherit Attribute
  new : message:string -> ExperimentalAttribute
  member Message : string

Full name: Microsoft.FSharp.Core.ExperimentalAttribute

--------------------
new : message:string -> ExperimentalAttribute
type Monoid<'a> =
  interface
    abstract member add : 'a -> 'a -> 'a
    abstract member unit : 'a
  end

Full name: Script.Monoid<_>
abstract member Monoid.add : 'a -> 'a -> 'a

Full name: Script.Monoid`1.add
Multiple items
abstract member Monoid.unit : 'a

Full name: Script.Monoid`1.unit

--------------------
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
type ImplicitTest =
  static member sum : xs:'a list * ?m:Monoid<'a> -> 'a

Full name: Script.ImplicitTest
static member ImplicitTest.sum : xs:'a list * ?m:Monoid<'a> -> 'a

Full name: Script.ImplicitTest.sum
val xs : 'a list
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val m : Monoid<'a> option
val m : Monoid<'a>
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member GetSlice : startIndex:int option * endIndex:int option -> 'T list
  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 fold : folder:('State -> 'T -> 'State) -> state:'State -> list:'T list -> 'State

Full name: Microsoft.FSharp.Collections.List.fold
abstract member Monoid.add : 'a -> 'a -> 'a
property Monoid.unit: 'a
val intMonoid : Monoid<int>

Full name: Script.intMonoid
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 this : Monoid<int>
val x : int
val y : int
property Monoid.unit: int
val stringMonoid : Monoid<string>

Full name: Script.stringMonoid
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 this : Monoid<string>
val x : string
val y : string
property Monoid.unit: string
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
static member ImplicitTest.sum : xs:'a list * ?m:Monoid<'a> -> 'a
Raw view Test code New version

More information

Link:http://fssnip.net/7VA
Posted:6 years ago
Author:Evgeniy Andreev
Tags: implicits , fable