1 people like it.

Fold functions over static values builder 4

IT'S IMPOSSIBLE TO WRITE PRODUCTION CODE WITHOUT TYPE CLASSES

 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: 
31: 
32: 
open System.Text

type Folder<'State> =
    abstract Invoke<'T> : 'State * 'T -> 'State

type FoldStep<'State> = 'State -> 'State

type FoldBuilder<'Folder, 'State when 'Folder :> Folder<'State>>() =
    member this.Zero(): FoldStep<'State> = id
    
    member inline this.Combine(
        [<InlineIfLambda>] f: FoldStep<'State>, 
        [<InlineIfLambda>] g: FoldStep<'State>)
        : FoldStep<'State> =
        fun state -> g (f state)
    
    member inline this.Delay([<InlineIfLambda>] f: unit -> FoldStep<'State>) = 
        f()
    
    member inline this.Yield(value: 'T): FoldStep<'State> = 
        fun state -> 
            Unchecked.defaultof<'Folder>.Invoke(state, value)

[<Struct>]
type StringBuilderFolder =
    interface Folder<StringBuilder> with
        member _.Invoke(state: StringBuilder, value: 'T) = 
            state.Append(value)

let inline fold<'Folder, 'State when 'Folder :> Folder<'State>> = FoldBuilder<'Folder, 'State>()
let f = (fold<StringBuilderFolder, StringBuilder> { 1; "lol"; 42; 'a'; 5.1 }) (StringBuilder())
printfn "%s" (f.ToString())
namespace System
namespace System.Text
type Folder<'State> =
  interface
    abstract member Invoke : 'State * 'T -> 'State
  end
type FoldStep<'State> = 'State -> 'State
Multiple items
type FoldBuilder<'Folder,'State (requires 'Folder :> Folder<'State>)> =
  new : unit -> FoldBuilder<'Folder,'State>
  member Combine : f:FoldStep<'State> * g:FoldStep<'State> -> FoldStep<'State>
  member Delay : f:(unit -> FoldStep<'State>) -> FoldStep<'State>
  member Yield : value:'T -> FoldStep<'State>
  member Zero : unit -> FoldStep<'State>

--------------------
new : unit -> FoldBuilder<'Folder,'State>
val this : FoldBuilder<#Folder<'State>,'State>
val id : x:'T -> 'T
val f : FoldStep<'State>
val g : FoldStep<'State>
val state : 'State
val f : (unit -> FoldStep<'State>)
type unit = Unit
val value : 'T
module Unchecked

from Microsoft.FSharp.Core.Operators
val defaultof<'T> : 'T
Multiple items
type StructAttribute =
  inherit Attribute
  new : unit -> StructAttribute

--------------------
new : unit -> StructAttribute
Multiple items
type StringBuilder =
  new : unit -> StringBuilder + 5 overloads
  member Append : value:string -> StringBuilder + 23 overloads
  member AppendFormat : format:string * arg0:obj -> StringBuilder + 7 overloads
  member AppendJoin : separator:string * [<ParamArray>] values:obj[] -> StringBuilder + 5 overloads
  member AppendLine : unit -> StringBuilder + 1 overload
  member Capacity : int with get, set
  member Chars : int -> char with get, set
  member Clear : unit -> StringBuilder
  member CopyTo : sourceIndex:int * destination:Span<char> * count:int -> unit + 1 overload
  member EnsureCapacity : capacity:int -> int
  ...
  nested type ChunkEnumerator

--------------------
StringBuilder() : StringBuilder
StringBuilder(capacity: int) : StringBuilder
StringBuilder(value: string) : StringBuilder
StringBuilder(value: string, capacity: int) : StringBuilder
StringBuilder(capacity: int, maxCapacity: int) : StringBuilder
StringBuilder(value: string, startIndex: int, length: int, capacity: int) : StringBuilder
val state : StringBuilder
val value : 'a
StringBuilder.Append(value: System.ReadOnlyMemory<char>) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: System.ReadOnlySpan<char>) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: char []) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: obj) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: uint64) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: uint32) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: uint16) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: decimal) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: float) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: float32) : StringBuilder
   (+0 other overloads)
val fold<'Folder,'State (requires 'Folder :> Folder<'State>)> : FoldBuilder<#Folder<'State>,'State>
val f : StringBuilder
type StringBuilderFolder =
  struct
    interface Folder<StringBuilder>
  end
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
StringBuilder.ToString() : string
StringBuilder.ToString(startIndex: int, length: int) : string
Raw view Test code New version

More information

Link:http://fssnip.net/85X
Posted:2 years ago
Author:Evgeniy Andreev
Tags: haskell