3 people like it.

Computation expression over StringBuilder

A computation expression builder which simplify StringBuilder usage.

 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: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
[<AutoOpen>]
module StringBuffer =
    open System.Text

    type StringBuffer = StringBuilder -> unit

    type StringBufferBuilder () =
        member inline __.Yield (txt: string) = fun (b: StringBuilder) -> b.Append txt |> ignore
        member inline __.Yield (c: char) = fun (b: StringBuilder) -> b.Append c |> ignore
        member inline __.Yield (strings: #seq<string>) =
            fun (b: StringBuilder) -> for s in strings do s |> b.AppendLine |> ignore
        member inline __.YieldFrom (f: StringBuffer) = f
        member __.Combine (f, g) = fun (b: StringBuilder) -> f b; g b
        member __.Delay f = fun (b: StringBuilder) -> (f()) b
        member __.Zero () = ignore
        
        member __.For (xs: 'a seq, f: 'a -> StringBuffer) =
            fun (b: StringBuilder) ->
                let e = xs.GetEnumerator ()
                while e.MoveNext() do
                    (f e.Current) b
        
        member __.While (p: unit -> bool, f: StringBuffer) =
            fun (b: StringBuilder) -> while p () do f b
            
        member __.Run (f: StringBuffer) =
            let b = StringBuilder()
            do f b
            b.ToString()

    let stringBuffer = new StringBufferBuilder ()

// Usage

let bytes2hex (bytes: byte array) : string =
    stringBuffer {
        for byte in bytes -> sprintf "%02x" byte
        yield "foo"
    }

let s = Array.zeroCreate 10 |> bytes2hex
Multiple items
type AutoOpenAttribute =
  inherit Attribute
  new : unit -> AutoOpenAttribute
  new : path:string -> AutoOpenAttribute
  member Path : string

Full name: Microsoft.FSharp.Core.AutoOpenAttribute

--------------------
new : unit -> AutoOpenAttribute
new : path:string -> AutoOpenAttribute
namespace System
namespace System.Text
type StringBuffer = StringBuilder -> unit

Full name: Script.StringBuffer.StringBuffer
Multiple items
type StringBuilder =
  new : unit -> StringBuilder + 5 overloads
  member Append : value:string -> StringBuilder + 18 overloads
  member AppendFormat : format:string * arg0:obj -> StringBuilder + 4 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:char[] * destinationIndex:int * count:int -> unit
  member EnsureCapacity : capacity:int -> int
  member Equals : sb:StringBuilder -> bool
  ...

Full name: System.Text.StringBuilder

--------------------
StringBuilder() : unit
StringBuilder(capacity: int) : unit
StringBuilder(value: string) : unit
StringBuilder(value: string, capacity: int) : unit
StringBuilder(capacity: int, maxCapacity: int) : unit
StringBuilder(value: string, startIndex: int, length: int, capacity: int) : unit
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
Multiple items
type StringBufferBuilder =
  new : unit -> StringBufferBuilder
  member Combine : f:(StringBuilder -> unit) * g:(StringBuilder -> 'c) -> (StringBuilder -> 'c)
  member Delay : f:(unit -> StringBuilder -> 'b) -> (StringBuilder -> 'b)
  member For : xs:seq<'a> * f:('a -> StringBuffer) -> (StringBuilder -> unit)
  member Run : f:StringBuffer -> string
  member While : p:(unit -> bool) * f:StringBuffer -> (StringBuilder -> unit)
  member Yield : txt:string -> (StringBuilder -> unit)
  member Yield : c:char -> (StringBuilder -> unit)
  member Yield : strings:#seq<string> -> (StringBuilder -> unit)
  member YieldFrom : f:StringBuffer -> StringBuffer
  ...

Full name: Script.StringBuffer.StringBufferBuilder

--------------------
new : unit -> StringBufferBuilder
member StringBufferBuilder.Yield : txt:string -> (StringBuilder -> unit)

Full name: Script.StringBuffer.StringBufferBuilder.Yield
val txt : 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 b : StringBuilder
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)
StringBuilder.Append(value: int64) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: int) : StringBuilder
   (+0 other overloads)
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val __ : StringBufferBuilder
member StringBufferBuilder.Yield : c:char -> (StringBuilder -> unit)

Full name: Script.StringBuffer.StringBufferBuilder.Yield
val c : char
Multiple items
val char : value:'T -> char (requires member op_Explicit)

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

--------------------
type char = System.Char

Full name: Microsoft.FSharp.Core.char
member StringBufferBuilder.Yield : strings:#seq<string> -> (StringBuilder -> unit)

Full name: Script.StringBuffer.StringBufferBuilder.Yield
val strings : #seq<string>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

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

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val s : string
StringBuilder.AppendLine() : StringBuilder
StringBuilder.AppendLine(value: string) : StringBuilder
member StringBufferBuilder.YieldFrom : f:StringBuffer -> StringBuffer

Full name: Script.StringBuffer.StringBufferBuilder.YieldFrom
val f : StringBuffer
member StringBufferBuilder.Combine : f:(StringBuilder -> unit) * g:(StringBuilder -> 'c) -> (StringBuilder -> 'c)

Full name: Script.StringBuffer.StringBufferBuilder.Combine
val f : (StringBuilder -> unit)
val g : (StringBuilder -> 'c)
member StringBufferBuilder.Delay : f:(unit -> StringBuilder -> 'b) -> (StringBuilder -> 'b)

Full name: Script.StringBuffer.StringBufferBuilder.Delay
val f : (unit -> StringBuilder -> 'b)
member StringBufferBuilder.Zero : unit -> ('a -> unit)

Full name: Script.StringBuffer.StringBufferBuilder.Zero
member StringBufferBuilder.For : xs:seq<'a> * f:('a -> StringBuffer) -> (StringBuilder -> unit)

Full name: Script.StringBuffer.StringBufferBuilder.For
val xs : seq<'a>
val f : ('a -> StringBuffer)
val e : System.Collections.Generic.IEnumerator<'a>
System.Collections.Generic.IEnumerable.GetEnumerator() : System.Collections.Generic.IEnumerator<'a>
System.Collections.IEnumerator.MoveNext() : bool
property System.Collections.Generic.IEnumerator.Current: 'a
member StringBufferBuilder.While : p:(unit -> bool) * f:StringBuffer -> (StringBuilder -> unit)

Full name: Script.StringBuffer.StringBufferBuilder.While
val p : (unit -> bool)
type bool = System.Boolean

Full name: Microsoft.FSharp.Core.bool
member StringBufferBuilder.Run : f:StringBuffer -> string

Full name: Script.StringBuffer.StringBufferBuilder.Run
StringBuilder.ToString() : string
StringBuilder.ToString(startIndex: int, length: int) : string
val stringBuffer : StringBufferBuilder

Full name: Script.StringBuffer.stringBuffer
val bytes2hex : bytes:byte array -> string

Full name: Script.bytes2hex
val bytes : byte array
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)

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

--------------------
type byte = System.Byte

Full name: Microsoft.FSharp.Core.byte
type 'T array = 'T []

Full name: Microsoft.FSharp.Core.array<_>
Multiple items
val byte : byte

--------------------
type byte = System.Byte

Full name: Microsoft.FSharp.Core.byte
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val s : string

Full name: Script.s
module Array

from Microsoft.FSharp.Collections
val zeroCreate : count:int -> 'T []

Full name: Microsoft.FSharp.Collections.Array.zeroCreate

More information

Link:http://fssnip.net/7WR
Posted:4 months ago
Author:Sehnsucht
Tags: computation expression