2 people like it.

Replace Elements

Replace elements

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
open FsUnit
open Xunit

[<Fact>]
let Transform() =
    let inline replace1 list a b = list |> Seq.map (fun x -> if x = a then b else x)
    let inline replace2 list func = list |> Seq.map func

    let inline m3 tr el time = 
        let mv = tr el 
        if mv = el then [el] else [for x in 1..time -> mv]
    let inline replace3 list tr time =  
        let db = List.foldBack(fun el acc -> (m3 tr el time)::acc) list [];
        [for x in db do yield! x]

    replace1 "hello, how are you?" 'e' 'a' |> should equal "hallo, how ara you?"
    replace1 [1;2;3;4;5;4;3;2;1] 3 9 |> should equal [1;2;9;4;5;4;9;2;1]

    replace2 "hello, how are you?" (fun x -> if x = 'e' then 'a' else x) |> should equal "hallo, how ara you?"
    replace2 [1;2;3;4;5;4;3;2;1] (fun x -> if x = 3 then 9 else x) |> should equal  [1;2;9;4;5;4;9;2;1]
    
    replace3 (List.ofSeq "hello, how are you?") (fun x -> if x = 'e' then 'a' else x)  3 |> should equal "haaallo, how araaa you?"
    replace3 [1;2;3;4;5;4;3;2;1] (fun x -> if x = 3 then 9 else x) 3 |> should equal [1;2;9;9;9;4;5;4;9;9;9;2;1] 
namespace FsUnit
val Transform : unit -> unit

Full name: Script.Transform
val replace1 : (seq<'a> -> 'a -> 'a -> seq<'a>) (requires equality)
Multiple items
val list : seq<'a> (requires equality)

--------------------
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val a : 'a (requires equality)
val b : 'a (requires equality)
module Seq

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val x : 'a (requires equality)
val replace2 : (seq<'a> -> ('a -> 'b) -> seq<'b>)
Multiple items
val list : seq<'a>

--------------------
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val func : ('a -> 'b)
val m3 : (('a -> 'a) -> 'a -> int -> 'a list) (requires equality)
val tr : ('a -> 'a) (requires equality)
val el : 'a (requires equality)
val time : int
val mv : 'a (requires equality)
val x : int
val replace3 : ('a list -> ('a -> 'a) -> int -> 'a list) (requires equality)
Multiple items
val list : 'a list (requires equality)

--------------------
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val db : 'a list list (requires equality)
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val foldBack : folder:('T -> 'State -> 'State) -> list:'T list -> state:'State -> 'State

Full name: Microsoft.FSharp.Collections.List.foldBack
val acc : 'a list list (requires equality)
val x : 'a list (requires equality)
val should : f:('a -> #NUnit.Framework.Constraints.Constraint) -> x:'a -> y:obj -> unit

Full name: FsUnit.TopLevelOperators.should
val equal : x:'a -> EqualsConstraint

Full name: FsUnit.TopLevelOperators.equal
val x : char
val ofSeq : source:seq<'T> -> 'T list

Full name: Microsoft.FSharp.Collections.List.ofSeq

More information

Link:http://fssnip.net/qV
Posted:9 years ago
Author:Pitch Rock
Tags: replace