What Does Apple Mean by Sequences Having Destructive Behavior? [closed]

There seems to be no formal definition of what Apple means by destructive behavior when referring to the expectations that developers should have when calling a for in loop on a sequence more than one time. While the example alluded to in the documentation refers to a Countdown sequence which counts down from 3 to 0, there is an additional case which is not considered where Countdown is initialised with a negative value. I have researched the meaning of destructive behaviour of a Sequence and came across an article which decrements the value of a startId property of a sequence using an iterator’s next() method which is marked as mutating. When a while let statement is used to iterate over the sequence over a second pass, the statement does not return any property. This seems to imply that the original break condition has been met when calling the while let the first time and that the final startId value has been returned which means that the sequence has ended. That article can be found here. Moreover, there is a discussion on the Swift.org site which talks about the idea of separating concepts like “finiteness” from “destructibility” but doesn’t really go into much detail to describe what “destructive” means. Can someone please explain this?

1

“Destructive” isn’t a formally defined term, but in this instance, it means that some sequences produce iterators that modify the sequence itself, meaning that you can “use up” the elements from one iteration, and make them unavailable to any future iterations.

Consider this OneShotSequence class:

class OneShotSequence {
    private var current: Int = 0
    private let end: Int

    init(end: Int) { self.end = end }
}

extension OneShotSequence: IteratorProtocol {
    func next() -> Int? {
        guard current < end else { return nil }
        defer { current += 1 }
        return current
    }
}

extension OneShotSequence: Sequence {
    func makeIterator() -> OneShotSequence {
        // An OSS object is its own iterator,
        // meaning that iteration mutates the original OSS object.
        // This only happens if OSS is a class, because if it were a struct,
        // this return value would be copied, and not have this issue.
        return self
    }
}

It behaves really similarly to a range:

print(Array(0...<5)) // => [0, 1, 2, 3, 4]
print(Array(OneShotSequence(end: 5))) // => [0, 1, 2, 3, 4]

But there’s one huge distinction: an OneShotSequence object is is own iterator. This means that as you call next() on the iterator, it modifies the actual sequence object, consuming its elements:

var sequence = OneShotSequence(end: 5)

// First iteration
for value in sequence {
    print(value) // prints 0, 1, 2, 3, 4
}

// Second iteration doesn't print anything, since it has already been "spent"
for value in sequence {
    print(value) // never called
}

These kinds of sequences are rare, but do crop up from time to time. Consider a sequence that models the messages coming in from a message queue. They might be buffered (so that new messages can be received while you’re still working through previous ones). Once you consume a message with next(), if you don’t save it, it’s gone. Of course, Sequence could have been implemented in a way that automatically retains all the previous elements indefinitely, but that would be a memory hog in cases where it isn’t necessary.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

What Does Apple Mean by Sequences Having Destructive Behavior? [closed]

There seems to be no formal definition of what Apple means by destructive behavior when referring to the expectations that developers should have when calling a for in loop on a sequence more than one time. While the example alluded to in the documentation refers to a Countdown sequence which counts down from 3 to 0, there is an additional case which is not considered where Countdown is initialised with a negative value. I have researched the meaning of destructive behaviour of a Sequence and came across an article which decrements the value of a startId property of a sequence using an iterator’s next() method which is marked as mutating. When a while let statement is used to iterate over the sequence over a second pass, the statement does not return any property. This seems to imply that the original break condition has been met when calling the while let the first time and that the final startId value has been returned which means that the sequence has ended. That article can be found here. Moreover, there is a discussion on the Swift.org site which talks about the idea of separating concepts like “finiteness” from “destructibility” but doesn’t really go into much detail to describe what “destructive” means. Can someone please explain this?

1

“Destructive” isn’t a formally defined term, but in this instance, it means that some sequences produce iterators that modify the sequence itself, meaning that you can “use up” the elements from one iteration, and make them unavailable to any future iterations.

Consider this OneShotSequence class:

class OneShotSequence {
    private var current: Int = 0
    private let end: Int

    init(end: Int) { self.end = end }
}

extension OneShotSequence: IteratorProtocol {
    func next() -> Int? {
        guard current < end else { return nil }
        defer { current += 1 }
        return current
    }
}

extension OneShotSequence: Sequence {
    func makeIterator() -> OneShotSequence {
        // An OSS object is its own iterator,
        // meaning that iteration mutates the original OSS object.
        // This only happens if OSS is a class, because if it were a struct,
        // this return value would be copied, and not have this issue.
        return self
    }
}

It behaves really similarly to a range:

print(Array(0...<5)) // => [0, 1, 2, 3, 4]
print(Array(OneShotSequence(end: 5))) // => [0, 1, 2, 3, 4]

But there’s one huge distinction: an OneShotSequence object is is own iterator. This means that as you call next() on the iterator, it modifies the actual sequence object, consuming its elements:

var sequence = OneShotSequence(end: 5)

// First iteration
for value in sequence {
    print(value) // prints 0, 1, 2, 3, 4
}

// Second iteration doesn't print anything, since it has already been "spent"
for value in sequence {
    print(value) // never called
}

These kinds of sequences are rare, but do crop up from time to time. Consider a sequence that models the messages coming in from a message queue. They might be buffered (so that new messages can be received while you’re still working through previous ones). Once you consume a message with next(), if you don’t save it, it’s gone. Of course, Sequence could have been implemented in a way that automatically retains all the previous elements indefinitely, but that would be a memory hog in cases where it isn’t necessary.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật