1 people like it.

Building Strings

When sprintf and String.concat is either too slow or not really what is needed, one can use System.Text.StringBuilder. This snippet makes working with StringBuilder much more convenient and the resulting code more succint.

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

// Too lazy to use ToString().

let Out x = x.ToString()

type Object with

    member this.Out : string =
        this |> Out

// Too lazy to use foo.Append(bar).

let (++) (left : System.Text.StringBuilder) (right : 't) : System.Text.StringBuilder =
    left.Append right

// Too lazy to use foo.Append(bar) |> ignore

let (+=) (left : System.Text.StringBuilder) (right : 't) : unit =
    left ++ right |> ignore

// Let's see what we are left with.

let source =
    let mutable x, y, a, b = (...)
    let foo, bar, baz, waz, zup, bla = (...)
    let s = new StringBuilder()
    // ...
    while x < y do
        if a > b then
            s += bla
        // ...
    // ...
    s
    ++ foo
    ++ bar
    ++ baz
    ++ waz
    ++ zup
    |> Out
namespace System
namespace System.Text
val Out : x:'a -> string

Full name: Script.Out
val x : 'a
Object.ToString() : string
Multiple items
type Object =
  new : unit -> obj
  member Equals : obj:obj -> bool
  member GetHashCode : unit -> int
  member GetType : unit -> Type
  member ToString : unit -> string
  static member Equals : objA:obj * objB:obj -> bool
  static member ReferenceEquals : objA:obj * objB:obj -> bool

Full name: System.Object

--------------------
Object() : unit
val this : Object
member Object.Out : string

Full name: Script.Out
Multiple items
val string : value:'T -> string

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

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
val left : StringBuilder
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
val right : 't
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)
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val source : string

Full name: Script.source
val mutable x : int
val mutable y : int
val mutable a : int
val mutable b : int
(0, 0, 0, 0)
val foo : string
val bar : string
val baz : string
val waz : string
val zup : string
val bla : string
("foo", "bar", "baz", "waz", "zup", "bla")
val s : StringBuilder

More information

Link:http://fssnip.net/by
Posted:12 years ago
Author:Bent Rasmussen
Tags: string , stringbuilder , format , helper