Skip to content

Instantly share code, notes, and snippets.

@davenportw15
Created December 18, 2015 03:50
Show Gist options
  • Save davenportw15/3c28c5b713475d43271a to your computer and use it in GitHub Desktop.
Save davenportw15/3c28c5b713475d43271a to your computer and use it in GitHub Desktop.
//
// LazySquares.swift
// SwiftBook
//
// Created by William Davenport on 12/10/15.
// Copyright © 2015 William Davenport. All rights reserved.
//
import Foundation
class SquareContainer {
let maximum: Int
lazy var squares: [Int] = (1...self.maximum).map { n in
print(n)
return n * n
}
init(withMaximum maximum: Int) {
self.maximum = maximum
}
}
let squareContainer = SquareContainer(withMaximum: 100)
print("squareContainer.squares not yet calculated")
print(squareContainer.squares)
print("squareContainer.squares now calculated")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment