5 people like it.
Like the snippet!
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:
|
[<AutoOpen>]
module StringBuffer =
open System.Text
type StringBuffer = StringBuilder -> unit
type StringBufferBuilder () =
member inline __.Yield (txt: string) = fun (b: StringBuilder) -> Printf.bprintf b "%s" txt
member inline __.Yield (c: char) = fun (b: StringBuilder) -> Printf.bprintf b "%c" c
member inline __.Yield (strings: #seq<string>) =
fun (b: StringBuilder) -> for s in strings do Printf.bprintf b "%s\n" s
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) ->
use 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 ()
type StringBufferBuilder with
member inline __.Yield (b: byte) = fun (sb: StringBuilder) -> Printf.bprintf sb "%02x " b
// Usage
let bytes2hex (bytes: byte array) : string =
stringBuffer {
for b in bytes -> b
}
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 Yield : b:byte -> (StringBuilder -> unit)
...
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
module Printf
from Microsoft.FSharp.Core
val bprintf : builder:StringBuilder -> format:Printf.BuilderFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.Printf.bprintf
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
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
val ignore : value:'T -> unit
Full name: Microsoft.FSharp.Core.Operators.ignore
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
member StringBufferBuilder.Yield : b:byte -> (StringBuilder -> unit)
Full name: Script.StringBuffer.StringBufferBuilder.Yield
val b : byte
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
val sb : StringBuilder
val bytes2hex : bytes:byte array -> string
Full name: Script.bytes2hex
val bytes : byte array
type 'T array = 'T []
Full name: Microsoft.FSharp.Core.array<_>
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