45 people like it.

Using the 'lazy' Keyword

Demonstrates using the 'lazy' keyword. Show's how a lazy value will only ever be evaluated once.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
let lazySideEffect =
    lazy
        ( let temp = 2 + 2
          printfn "%i" temp
          temp )
          
printfn "Force value the first time: "
let actualValue1 = Lazy.force lazySideEffect
printfn "Force value the second time: "
let actualValue2 = Lazy.force lazySideEffect
val lazySideEffect : Lazy<int>

Full name: Script.lazySideEffect
val temp : int
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val actualValue1 : obj

Full name: Script.actualValue1
Multiple items
active recognizer Lazy: Lazy<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.( |Lazy| )

--------------------
type Lazy<'T> = System.Lazy<'T>

Full name: Microsoft.FSharp.Control.Lazy<_>
val actualValue2 : obj

Full name: Script.actualValue2
Raw view New version

More information

Link:http://fssnip.net/y
Posted:6 years ago
Author:Robert Pickering
Tags: lazy