<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/sequence(state:next:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s8sequence5state4nexts14UnfoldSequenceVyxq_Gq__xSgq_zctr0_lF"
  },
  "title" : "sequence(state:next:)"
}
-->

# sequence(state:next:)

Returns a sequence formed from repeated lazy applications of `next` to a
mutable `state`.

```
func sequence<T, State>(state: State, next: @escaping (inout State) -> T?) -> UnfoldSequence<T, State>
```

## Parameters

`state`

The initial state that will be passed to the closure.

`next`

A closure that accepts an `inout` state and returns the
next element of the sequence.

## Return Value

A sequence that yields each successive value from `next`.

## Discussion

The elements of the sequence are obtained by invoking `next` with a mutable
state. The same state is passed to all invocations of `next`, so subsequent
calls will see any mutations made by previous calls. The sequence ends when
`next` returns `nil`. If `next` never returns `nil`, the sequence is
infinite.

This function can be used to replace many instances of `AnyIterator` that
wrap a closure.

Example:

```
// Interleave two sequences that yield the same element type
sequence(state: (false, seq1.makeIterator(), seq2.makeIterator()), next: { iters in
  iters.0 = !iters.0
  return iters.0 ? iters.1.next() : iters.2.next()
})
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)