Inertia is evaluated

Lazy Evaluation is common in functional programming. Lazy Evaluation is also translated as Lazy Evaluation. Its purpose is to minimize the amount of work a computer has to do. With lazy evaluation, the expression is not evaluated immediately after it is bound to a variable, but when the value is fetched. The advantages of lazy evaluation are obvious:

  • The fact that computations only perform when they are really needed opens up more possibilities for code optimization. Some operations may only be used under a conditional branch. Some of the calculations may cancel out with later calculations, so you can optimize the way you do the calculations. Write pseudocode like this:
let a = 100
let b = 1000
let c = a/b
print(c*b) 
Copy the code

The final evaluation of the expression is a/b*b, so the final output value is a, without the operation of dividing by b and multiplying by b.

  • An infinite data structure can declare an infinite sequence by lazy evaluation because it is only an expression when it is not being used, and only evaluated when it is actually using specific items of data in the program. The following code defines an odd sequence starting with 1:
var nums = sequence(first: 1, next: { num in
    return num + 2
})

// This is when the actual calculation takes place
for n in nums.prefix(5) {
    print(n)
}
Copy the code

Swift, the lazy

Swift’s programming paradigm includes functional programming, but there are still some gaps between Swift and purely functional programming languages such as Haskell. The default evaluation policy for collection operations in Swift is eager, which means evaluate early. The evaluation takes place when an expression is assigned to a variable. To support lazy evaluation, an lazy collection can be obtained in the collection through the lazy attribute.

// a huge collection
let giant = 0..<Int.max
// lazily map it: no work is done yet
let mapped = giant.lazy.map{$0 * 2 }
// sum the first few elements
let sum = mapped.prefix(10).reduce(0, +)
// sum == 90
Copy the code

In the above code, the mapped assignment didn’t happen because the map operation on the LazyCollecion returned by lazy was lazy.

trade-offs

The disadvantage of lazy evaluation is that, just like asynchronous evaluation, you can’t show the order of execution when writing code. In some scenarios it is necessary to evaluate in time. For example, the following code does not need to be executed before the second line in a lazy evaluation system:

System.out.println("Please enter your name: ");
System.in.readLine();
Copy the code

So a programming language can’t just support lazy evaluation. It’s just that in Swift, Apple thinks that most of the time an early evaluation will suffice, requiring explicit lazy fetching of lazy sets. Of course, it is also possible that some of the functional features in the early swift features are not the most core requirements. Look forward to the later Swift features.


reference

  • Inertia is evaluated
  • Conditional Conformance in the Standard Library
  • Functional Programming for Dummies

Welcome to my micro blog: @Zhuo who has no story

Nuggets blog: juejin.cn/user/192600…

If you want to talk to me more closely, you can also join my little secret circle: The Programmer’s Survival Guide