5 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: 
42: 
43: 
44: 
45: 
module StringBuffer =
    open System.Text

    [<Struct; NoComparison; NoEquality>]
    type StringBuffer = StringBuffer of (StringBuilder -> unit)

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

    let stringBuffer = new StringBufferM ()

// Usage

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

let hexString = Array.zeroCreate 10 |> bytes2hex
namespace System
namespace System.Text
Multiple items
type StructAttribute =
  inherit Attribute
  new : unit -> StructAttribute

Full name: Microsoft.FSharp.Core.StructAttribute

--------------------
new : unit -> StructAttribute
Multiple items
type NoComparisonAttribute =
  inherit Attribute
  new : unit -> NoComparisonAttribute

Full name: Microsoft.FSharp.Core.NoComparisonAttribute

--------------------
new : unit -> NoComparisonAttribute
Multiple items
type NoEqualityAttribute =
  inherit Attribute
  new : unit -> NoEqualityAttribute

Full name: Microsoft.FSharp.Core.NoEqualityAttribute

--------------------
new : unit -> NoEqualityAttribute
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
val private zero : obj

Full name: Script.StringBuffer.zero
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val x : 'a
Multiple items
type StringBufferM =
  new : unit -> StringBufferM
  member Combine : f:'i * g:'j -> 'k
  member Delay : f:'g -> 'h
  member For : xs:seq<'a> * f:('a -> 'e) -> 'f
  member Run : 'a -> 'b
  member While : p:(unit -> bool) * f:'c -> 'd
  member Yield : txt:string -> 'p
  member Yield : c:char -> 'o
  member Yield : strings:#seq<string> -> 'n
  member YieldFrom : f:'l -> 'l
  ...

Full name: Script.StringBuffer.StringBufferM

--------------------
new : unit -> StringBufferM
val zero : obj
member StringBufferM.Yield : txt:string -> 'p

Full name: Script.StringBuffer.StringBufferM.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 __ : StringBufferM
member StringBufferM.Yield : c:char -> 'o

Full name: Script.StringBuffer.StringBufferM.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 StringBufferM.Yield : strings:#seq<string> -> 'n

Full name: Script.StringBuffer.StringBufferM.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<_>
member StringBufferM.YieldFrom : f:'l -> 'l

Full name: Script.StringBuffer.StringBufferM.YieldFrom
val f : 'l
member StringBufferM.Combine : f:'i * g:'j -> 'k

Full name: Script.StringBuffer.StringBufferM.Combine
val f : 'i
val g : 'j
member StringBufferM.Delay : f:'g -> 'h

Full name: Script.StringBuffer.StringBufferM.Delay
val f : 'g
member StringBufferM.Zero : unit -> obj

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

Full name: Script.StringBuffer.StringBufferM.For
val xs : seq<'a>
val f : ('a -> 'e)
System.Collections.Generic.IEnumerable.GetEnumerator() : System.Collections.Generic.IEnumerator<'a>
member StringBufferM.While : p:(unit -> bool) * f:'c -> 'd

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

Full name: Microsoft.FSharp.Core.bool
val f : 'c
member StringBufferM.Run : 'a -> 'b

Full name: Script.StringBuffer.StringBufferM.Run
val _arg1 : 'a
val stringBuffer : StringBufferM

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<_>
val hexString : string

Full name: Script.hexString
module Array

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

Full name: Microsoft.FSharp.Collections.Array.zeroCreate
Next Version Raw view Test code New version

More information

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