Scott Nimrod
Scott Nimrod

Reputation: 11595

How can I interpret property based test code?

How can I interpret property based test code?

I'm struggling to translate the instructions on the following snippet:

let myProperty = Prop.forAll fiveAndThrees <| fun number ->

    let actual = transform number

    let expected = "jbdhjsdhjdsjhsdglsdjlljh"
    expected = actual

Check.QuickThrowOnFailure myProperty

Specifically, I'm struggling with the backwards pipeline operator (i.e. "<|").

Here's the test:

[<Fact>]
let ``FizzBuzz.transform returns FizzBuzz`` () =
    let fiveAndThrees = Arb.generate<int> |> Gen.map ((*) (3 * 5)) 
                                          |> Arb.fromGen

    let myProperty = Prop.forAll fiveAndThrees <| fun number ->

        let actual = transform number

        let expected = "jbdhjsdhjdsjhsdglsdjlljh"
        expected = actual

    Check.QuickThrowOnFailure myProperty

Can someone please guide me step by step on how this code works?

Could this be rewritten using the forward pipe operator (i.e. "|>")?

Upvotes: 1

Views: 77

Answers (1)

Guy Coder
Guy Coder

Reputation: 24976

This answer only covers why <| is used instead of |>.

Here are 5 examples that work toward making use of <| with a large function. The 6th example is to show how the code looks using |> instead of <|. The point is that with the 6th example using |> you have to look into the code to find the primary function funThatNeedsListAndFunc but with <| the primary function funThatNeedsListAndFunc is obvious. So you typically see <| when the last parameter is a function and you want to pass in the function after the primary function for easier comprehension. That's all; don't read more into than that.

After reading the Mark's blog I also learned that <| is useful to remove ( ) around fun. An example using ( ) is given as example 7.

let funThatNeedsListAndFunc list func =
    func list

let func = List.sum
let list = Seq.toList { 0 .. 5}

let result1 = funThatNeedsListAndFunc list func

printfn "result1: %A" result1

let result2 = funThatNeedsListAndFunc list <| func

printfn "result2: %A" result2

let result3 = funThatNeedsListAndFunc list <| List.sum

printfn "result3: %A" result3

let result4 = funThatNeedsListAndFunc list <| 
    fun (list : 'a list) -> List.sum list

printfn "result4: %A" result4

let result5 = funThatNeedsListAndFunc list <| 
    fun (list : 'a list) -> 
        // This will be a long function
        let a = 1
        let b = 2
        let c = a * b
        let result = List.sum list
        let d = "more useless lines"
        let (e,f,g) = ("a", 15, 3.0)
        result

printfn "result5: %A" result5

.

let result6 = 
    fun (list : 'a list) -> 
        // This will be a long function
        let a = 1
        let b = 2
        let c = a * b
        let result = List.sum list
        let d = "more useless lines"
        let (e,f,g) = ("a", 15, 3.0)
        result
    |> funThatNeedsListAndFunc list

printfn "result6: %A" result6

.

let result7 = 
    funThatNeedsListAndFunc list (fun (list : 'a list) -> 
        // This will be a long function
        let a = 1
        let b = 2
        let c = a * b
        let result = List.sum list
        let d = "more useless lines"
        let (e,f,g) = ("a", 15, 3.0)
        result)

printfn "result7: %A" result7

Upvotes: 3

Related Questions