0 people like it.

Fold functions over static values builder 3

Build strings using powerful higher-rank encoding!

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

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

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

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

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

--------------------
new : unit -> FoldBuilder
val this : FoldBuilder
val state : 'State
val f : FoldStep<'State>
val g : FoldStep<'State>
val folder : Folder<'State>
val f : (unit -> FoldStep<'State>)
type unit = Unit
val value : 'T
abstract member Folder.Invoke : 'State * 'T -> 'State
val append : Folder<StringBuilder>
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 : FoldBuilder
val f : StringBuilder
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/85W
Posted:6 months ago
Author:Evgeniy Andreev
Tags: monads