0
0
Swiftprogramming~10 mins

Async sequences (AsyncSequence) in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an asynchronous sequence using AsyncSequence protocol.

Swift
struct NumberIterator: AsyncIteratorProtocol {
    typealias Element = Int
    var current = 1
    mutating func next() async -> [1] {
        guard current <= 3 else { return nil }
        defer { current += 1 }
        return current
    }
}

struct NumberSequence: AsyncSequence {
    typealias Element = Int
    func makeAsyncIterator() -> NumberIterator {
        return NumberIterator()
    }
}
Drag options to blanks, or click blank then click option'
AInt?
BInt
CString?
DVoid
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-optional return type for next()
Returning a non-async function
2fill in blank
medium

Complete the code to asynchronously iterate over the NumberSequence and print each number.

Swift
let sequence = NumberSequence()
Task {
    var iterator = sequence.makeAsyncIterator()
    while let number = await [1] {
        print(number)
    }
}
Drag options to blanks, or click blank then click option'
Aiterator.next()
Bsequence.next()
Citerator.next
Dsequence.makeAsyncIterator()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling next() on the sequence instead of the iterator
Forgetting to use await
3fill in blank
hard

Fix the error in the async for loop to iterate over the sequence correctly.

Swift
let sequence = NumberSequence()
Task {
    for await [1] in sequence {
        print([1])
    }
}
Drag options to blanks, or click blank then click option'
Avalue
Bnum
Citem
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names inside the loop
Using reserved keywords as variable names
4fill in blank
hard

Fill both blanks to create a filtered async sequence that yields only even numbers.

Swift
struct EvenNumberSequence: AsyncSequence {
    typealias Element = Int
    let baseSequence: NumberSequence
    func makeAsyncIterator() -> Iterator {
        return Iterator(baseIterator: baseSequence.makeAsyncIterator() as! NumberIterator)
    }
    struct Iterator: AsyncIteratorProtocol {
        typealias Element = Int
        var baseIterator: NumberIterator
        mutating func next() async -> [1] {
            while let number = await baseIterator.next() {
                if number [2] 2 == 0 {
                    return number
                }
            }
            return nil
        }
    }
}
Drag options to blanks, or click blank then click option'
AInt?
BInt
C%
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-optional return type for next()
Using equality operator instead of modulo for even check
5fill in blank
hard

Fill all three blanks to create a dictionary from an async sequence where keys are string versions of numbers and values are the numbers themselves, filtering only numbers greater than 1.

Swift
func createDictionary(from sequence: NumberSequence) async -> [String: Int] {
    var result = [String: Int]()
    var iterator = sequence.makeAsyncIterator()
    while let number = await [1] {
        if number [2] 1 {
            result[[3]] = number
        }
    }
    return result
}
Drag options to blanks, or click blank then click option'
Aiterator.next()
B>
CString(number)
Dnumber.next()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling next() on the wrong variable
Using wrong comparison operator
Not converting number to string for dictionary key