{"action":"created","comment":{"url":"https://api.github.com/repos/apple/swift/pulls/comments/64693906","id":64693906,"diff_hunk":"@@ -0,0 +1,113 @@\n+//===----------------------------------------------------------------------===//\n+//\n+// This source file is part of the Swift.org open source project\n+//\n+// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors\n+// Licensed under Apache License v2.0 with Runtime Library Exception\n+//\n+// See http://swift.org/LICENSE.txt for license information\n+// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors\n+//\n+//===----------------------------------------------------------------------===//\n+\n+/// Returns a sequence formed from `first` and repeated lazy applications of\n+/// `next`.\n+///\n+/// The first value returned by the sequence is always `first`, and each\n+/// successive value is the result of invoking `next` with the previous value.\n+/// The sequence ends when `next` returns `nil`. If `next` never returns `nil`,\n+/// the sequence is infinite.\n+///\n+/// This function can be used to replace many cases that were previously handled\n+/// using C-style `for` loops.\n+///\n+/// Example:\n+///\n+///     // Walk the elements of a tree from a node up to the root\n+///     for node in sequence(first: leaf, next: { $0.parent }) {\n+///         // node is leaf, then leaf.parent, then leaf.parent.parent, etc.\n+///     }\n+///\n+///     // Iterate over all powers of two (ignoring overflow)\n+///     for value in sequence(first: 1, next: { $0 * 2 }) {\n+///         // value is 1, then 2, then 4, then 8, etc.\n+///     }\n+///\n+/// - Parameter first: The first value to be returned from the sequence.\n+/// - Parameter next: A closure that accepts the previous sequence value and\n+///   returns the next value.\n+/// - Returns: A sequence that starts with `first` and continues with every\n+///   value returned by passing the previous value to `next`.\n+///\n+/// - SeeAlso: `sequence(state:next:)`\n+public func sequence<T>(first: T, next: (T) -> T?) -> UnfoldFirstSequence<T> {\n+    // The trivial implementation where the state is the next value to return\n+    // has the downside of being unnecessarily eager (it evaluates `next` one\n+    // step in advance). We solve this by using a boolean value to disambiguate\n+    // between the first value (that's computed in advance) and the rest.\n+    return sequence(state: (first, true), next: { (state: inout (T?, Bool)) -> T? in\n+        switch state {\n+        case (let value, true):\n+            state.1 = false\n+            return value\n+        case (let value?, _):\n+            let nextValue = next(value)\n+            state.0 = nextValue\n+            return nextValue\n+        case (nil, _):\n+            return nil\n+        }\n+    })\n+}\n+\n+/// Returns a sequence formed from repeated lazy applications of `next` to a\n+/// mutable `state`.\n+///\n+/// Every value returned by the sequence is the result of invoking `next` with a\n+/// mutable state. The same state is passed to all invocations of `next`, so\n+/// subsequent calls will see any mutations made by previous calls. The sequence\n+/// ends when `next` returns `nil`. If `next` never returns `nil`, the sequence\n+/// is infinite.\n+///\n+/// This function can be used to replace many instances of `AnyIterator` that\n+/// wrap a closure.\n+///\n+/// Example:\n+///\n+///     // Interleave two sequences that yield the same element type\n+///     sequence(state: (false, seq1.makeIterator(), seq2.makeIterator()), next: { iters in\n+///         iters.0 = !iters.0\n+///         return iters.0 ? iters.1.next() : iters.2.next()\n+///     })\n+///\n+/// - Parameter state: The initial state that will be passed to each closure.\n+/// - Parameter next: A closure that accepts an `inout` state and returns the\n+///   next element of the sequence.\n+/// - Returns: A sequence that yields each successive value from `next.\n+///\n+/// - SeeAlso: `sequence(first:next:)`\n+public func sequence<T, State>(state: State, next: (inout State) -> T?) -> UnfoldSequence<T, State> {","path":"stdlib/public/core/UnfoldSequence.swift","position":89,"original_position":89,"commit_id":"1ec085f67376dc445901b6cc68e56c1f7a0368bc","original_commit_id":"1ec085f67376dc445901b6cc68e56c1f7a0368bc","user":{"login":"kballard","id":714,"avatar_url":"https://avatars.githubusercontent.com/u/714?v=3","gravatar_id":"","url":"https://api.github.com/users/kballard","html_url":"https://github.com/kballard","followers_url":"https://api.github.com/users/kballard/followers","following_url":"https://api.github.com/users/kballard/following{/other_user}","gists_url":"https://api.github.com/users/kballard/gists{/gist_id}","starred_url":"https://api.github.com/users/kballard/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kballard/subscriptions","organizations_url":"https://api.github.com/users/kballard/orgs","repos_url":"https://api.github.com/users/kballard/repos","events_url":"https://api.github.com/users/kballard/events{/privacy}","received_events_url":"https://api.github.com/users/kballard/received_events","type":"User","site_admin":false},"body":"Regarding `T` => `Element`, I checked before writing this and our functions like `map` still use type parameters that look like `T`. My impression was that we renamed `T` to `Element`, etc when used as parameters to types themselves, but left them as `T` on the functions that return the types, because the type here is not defined as \"the element of the sequence\", but is instead defined as e.g. \"the return value of the closure\", and then the resulting sequence uses that return value for its elements.","created_at":"2016-05-26T05:40:05Z","updated_at":"2016-05-26T05:40:05Z","html_url":"https://github.com/apple/swift/pull/2717#discussion_r64693906","pull_request_url":"https://api.github.com/repos/apple/swift/pulls/2717","_links":{"self":{"href":"https://api.github.com/repos/apple/swift/pulls/comments/64693906"},"html":{"href":"https://github.com/apple/swift/pull/2717#discussion_r64693906"},"pull_request":{"href":"https://api.github.com/repos/apple/swift/pulls/2717"}}},"pull_request":{"url":"https://api.github.com/repos/apple/swift/pulls/2717","id":71501353,"html_url":"https://github.com/apple/swift/pull/2717","diff_url":"https://github.com/apple/swift/pull/2717.diff","patch_url":"https://github.com/apple/swift/pull/2717.patch","issue_url":"https://api.github.com/repos/apple/swift/issues/2717","number":2717,"state":"open","locked":false,"title":"[Stdlib] Implement sequence(first:next:) and sequence(state:next:)","user":{"login":"kballard","id":714,"avatar_url":"https://avatars.githubusercontent.com/u/714?v=3","gravatar_id":"","url":"https://api.github.com/users/kballard","html_url":"https://github.com/kballard","followers_url":"https://api.github.com/users/kballard/followers","following_url":"https://api.github.com/users/kballard/following{/other_user}","gists_url":"https://api.github.com/users/kballard/gists{/gist_id}","starred_url":"https://api.github.com/users/kballard/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kballard/subscriptions","organizations_url":"https://api.github.com/users/kballard/orgs","repos_url":"https://api.github.com/users/kballard/repos","events_url":"https://api.github.com/users/kballard/events{/privacy}","received_events_url":"https://api.github.com/users/kballard/received_events","type":"User","site_admin":false},"body":"#### What's in this pull request?\r\n\r\nThis implements [SE-0094](https://github.com/apple/swift-evolution/blob/master/proposals/0094-sequence-function.md) (`sequence(first:next:)` and `sequence(state:next:)`).\r\n\r\n#### Resolved bug number: ([SR-1622](https://bugs.swift.org/browse/SR-1622))\r\n\r\n* * * *\r\n\r\n<!-- This selection should only be completed by Swift admin -->\r\nBefore merging this pull request to apple/swift repository:\r\n- [ ] Resolve the unit test errors (see PR comments).\r\n- [ ] Test pull request on Swift continuous integration.\r\n\r\n<details>\r\n  <summary>Triggering Swift CI</summary>\r\n\r\nThe swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:\r\n\r\n**Smoke Testing**\r\n\r\n        Platform     | Comment\r\n        ------------ | -------------\r\n        All supported platforms     | @swift-ci Please smoke test\r\n        All supported platforms     | @swift-ci Please smoke test and merge\r\n        OS X platform               | @swift-ci Please smoke test OS X platform\r\n        Linux platform              | @swift-ci Please smoke test Linux platform\r\n\r\n**Validation Testing**\r\n\r\n        Platform     | Comment\r\n        ------------ | -------------\r\n        All supported platforms     | @swift-ci Please test\r\n        All supported platforms     | @swift-ci Please test and merge\r\n        OS X platform               | @swift-ci Please test OS X platform\r\n        OS X platform               | @swift-ci Please benchmark\r\n        Linux platform              | @swift-ci Please test Linux platform\r\n\r\n\r\n**Lint Testing**\r\n\r\n        Language     | Comment\r\n        ------------ | -------------\r\n        Python       | @swift-ci Please Python lint\r\n\r\nNote: Only members of the Apple organization can trigger swift-ci.\r\n</details>","created_at":"2016-05-26T05:17:09Z","updated_at":"2016-05-26T05:40:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":"2e1ba80b89d467443986ec6aa3630013af56764f","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apple/swift/pulls/2717/commits","review_comments_url":"https://api.github.com/repos/apple/swift/pulls/2717/comments","review_comment_url":"https://api.github.com/repos/apple/swift/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apple/swift/issues/2717/comments","statuses_url":"https://api.github.com/repos/apple/swift/statuses/1ec085f67376dc445901b6cc68e56c1f7a0368bc","head":{"label":"kballard:sequence-first-state-next","ref":"sequence-first-state-next","sha":"1ec085f67376dc445901b6cc68e56c1f7a0368bc","user":{"login":"kballard","id":714,"avatar_url":"https://avatars.githubusercontent.com/u/714?v=3","gravatar_id":"","url":"https://api.github.com/users/kballard","html_url":"https://github.com/kballard","followers_url":"https://api.github.com/users/kballard/followers","following_url":"https://api.github.com/users/kballard/following{/other_user}","gists_url":"https://api.github.com/users/kballard/gists{/gist_id}","starred_url":"https://api.github.com/users/kballard/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kballard/subscriptions","organizations_url":"https://api.github.com/users/kballard/orgs","repos_url":"https://api.github.com/users/kballard/repos","events_url":"https://api.github.com/users/kballard/events{/privacy}","received_events_url":"https://api.github.com/users/kballard/received_events","type":"User","site_admin":false},"repo":{"id":47383591,"name":"swift","full_name":"kballard/swift","owner":{"login":"kballard","id":714,"avatar_url":"https://avatars.githubusercontent.com/u/714?v=3","gravatar_id":"","url":"https://api.github.com/users/kballard","html_url":"https://github.com/kballard","followers_url":"https://api.github.com/users/kballard/followers","following_url":"https://api.github.com/users/kballard/following{/other_user}","gists_url":"https://api.github.com/users/kballard/gists{/gist_id}","starred_url":"https://api.github.com/users/kballard/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kballard/subscriptions","organizations_url":"https://api.github.com/users/kballard/orgs","repos_url":"https://api.github.com/users/kballard/repos","events_url":"https://api.github.com/users/kballard/events{/privacy}","received_events_url":"https://api.github.com/users/kballard/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/kballard/swift","description":"The Swift Programming Language","fork":true,"url":"https://api.github.com/repos/kballard/swift","forks_url":"https://api.github.com/repos/kballard/swift/forks","keys_url":"https://api.github.com/repos/kballard/swift/keys{/key_id}","collaborators_url":"https://api.github.com/repos/kballard/swift/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/kballard/swift/teams","hooks_url":"https://api.github.com/repos/kballard/swift/hooks","issue_events_url":"https://api.github.com/repos/kballard/swift/issues/events{/number}","events_url":"https://api.github.com/repos/kballard/swift/events","assignees_url":"https://api.github.com/repos/kballard/swift/assignees{/user}","branches_url":"https://api.github.com/repos/kballard/swift/branches{/branch}","tags_url":"https://api.github.com/repos/kballard/swift/tags","blobs_url":"https://api.github.com/repos/kballard/swift/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/kballard/swift/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/kballard/swift/git/refs{/sha}","trees_url":"https://api.github.com/repos/kballard/swift/git/trees{/sha}","statuses_url":"https://api.github.com/repos/kballard/swift/statuses/{sha}","languages_url":"https://api.github.com/repos/kballard/swift/languages","stargazers_url":"https://api.github.com/repos/kballard/swift/stargazers","contributors_url":"https://api.github.com/repos/kballard/swift/contributors","subscribers_url":"https://api.github.com/repos/kballard/swift/subscribers","subscription_url":"https://api.github.com/repos/kballard/swift/subscription","commits_url":"https://api.github.com/repos/kballard/swift/commits{/sha}","git_commits_url":"https://api.github.com/repos/kballard/swift/git/commits{/sha}","comments_url":"https://api.github.com/repos/kballard/swift/comments{/number}","issue_comment_url":"https://api.github.com/repos/kballard/swift/issues/comments{/number}","contents_url":"https://api.github.com/repos/kballard/swift/contents/{+path}","compare_url":"https://api.github.com/repos/kballard/swift/compare/{base}...{head}","merges_url":"https://api.github.com/repos/kballard/swift/merges","archive_url":"https://api.github.com/repos/kballard/swift/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/kballard/swift/downloads","issues_url":"https://api.github.com/repos/kballard/swift/issues{/number}","pulls_url":"https://api.github.com/repos/kballard/swift/pulls{/number}","milestones_url":"https://api.github.com/repos/kballard/swift/milestones{/number}","notifications_url":"https://api.github.com/repos/kballard/swift/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/kballard/swift/labels{/name}","releases_url":"https://api.github.com/repos/kballard/swift/releases{/id}","deployments_url":"https://api.github.com/repos/kballard/swift/deployments","created_at":"2015-12-04T05:37:18Z","updated_at":"2016-02-29T23:08:05Z","pushed_at":"2016-05-26T05:12:59Z","git_url":"git://github.com/kballard/swift.git","ssh_url":"git@github.com:kballard/swift.git","clone_url":"https://github.com/kballard/swift.git","svn_url":"https://github.com/kballard/swift","homepage":"","size":101341,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apple:master","ref":"master","sha":"ad6de9b64509baf3093f99a128ad7ac57ed3d3bd","user":{"login":"apple","id":10639145,"avatar_url":"https://avatars.githubusercontent.com/u/10639145?v=3","gravatar_id":"","url":"https://api.github.com/users/apple","html_url":"https://github.com/apple","followers_url":"https://api.github.com/users/apple/followers","following_url":"https://api.github.com/users/apple/following{/other_user}","gists_url":"https://api.github.com/users/apple/gists{/gist_id}","starred_url":"https://api.github.com/users/apple/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apple/subscriptions","organizations_url":"https://api.github.com/users/apple/orgs","repos_url":"https://api.github.com/users/apple/repos","events_url":"https://api.github.com/users/apple/events{/privacy}","received_events_url":"https://api.github.com/users/apple/received_events","type":"Organization","site_admin":false},"repo":{"id":44838949,"name":"swift","full_name":"apple/swift","owner":{"login":"apple","id":10639145,"avatar_url":"https://avatars.githubusercontent.com/u/10639145?v=3","gravatar_id":"","url":"https://api.github.com/users/apple","html_url":"https://github.com/apple","followers_url":"https://api.github.com/users/apple/followers","following_url":"https://api.github.com/users/apple/following{/other_user}","gists_url":"https://api.github.com/users/apple/gists{/gist_id}","starred_url":"https://api.github.com/users/apple/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apple/subscriptions","organizations_url":"https://api.github.com/users/apple/orgs","repos_url":"https://api.github.com/users/apple/repos","events_url":"https://api.github.com/users/apple/events{/privacy}","received_events_url":"https://api.github.com/users/apple/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apple/swift","description":"The Swift Programming Language","fork":false,"url":"https://api.github.com/repos/apple/swift","forks_url":"https://api.github.com/repos/apple/swift/forks","keys_url":"https://api.github.com/repos/apple/swift/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apple/swift/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apple/swift/teams","hooks_url":"https://api.github.com/repos/apple/swift/hooks","issue_events_url":"https://api.github.com/repos/apple/swift/issues/events{/number}","events_url":"https://api.github.com/repos/apple/swift/events","assignees_url":"https://api.github.com/repos/apple/swift/assignees{/user}","branches_url":"https://api.github.com/repos/apple/swift/branches{/branch}","tags_url":"https://api.github.com/repos/apple/swift/tags","blobs_url":"https://api.github.com/repos/apple/swift/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apple/swift/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apple/swift/git/refs{/sha}","trees_url":"https://api.github.com/repos/apple/swift/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apple/swift/statuses/{sha}","languages_url":"https://api.github.com/repos/apple/swift/languages","stargazers_url":"https://api.github.com/repos/apple/swift/stargazers","contributors_url":"https://api.github.com/repos/apple/swift/contributors","subscribers_url":"https://api.github.com/repos/apple/swift/subscribers","subscription_url":"https://api.github.com/repos/apple/swift/subscription","commits_url":"https://api.github.com/repos/apple/swift/commits{/sha}","git_commits_url":"https://api.github.com/repos/apple/swift/git/commits{/sha}","comments_url":"https://api.github.com/repos/apple/swift/comments{/number}","issue_comment_url":"https://api.github.com/repos/apple/swift/issues/comments{/number}","contents_url":"https://api.github.com/repos/apple/swift/contents/{+path}","compare_url":"https://api.github.com/repos/apple/swift/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apple/swift/merges","archive_url":"https://api.github.com/repos/apple/swift/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apple/swift/downloads","issues_url":"https://api.github.com/repos/apple/swift/issues{/number}","pulls_url":"https://api.github.com/repos/apple/swift/pulls{/number}","milestones_url":"https://api.github.com/repos/apple/swift/milestones{/number}","notifications_url":"https://api.github.com/repos/apple/swift/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apple/swift/labels{/name}","releases_url":"https://api.github.com/repos/apple/swift/releases{/id}","deployments_url":"https://api.github.com/repos/apple/swift/deployments","created_at":"2015-10-23T21:15:07Z","updated_at":"2016-05-26T05:02:57Z","pushed_at":"2016-05-26T05:36:56Z","git_url":"git://github.com/apple/swift.git","ssh_url":"git@github.com:apple/swift.git","clone_url":"https://github.com/apple/swift.git","svn_url":"https://github.com/apple/swift","homepage":"https://swift.org/","size":155598,"stargazers_count":30631,"watchers_count":30631,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":4173,"mirror_url":null,"open_issues_count":96,"forks":4173,"open_issues":96,"watchers":30631,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apple/swift/pulls/2717"},"html":{"href":"https://github.com/apple/swift/pull/2717"},"issue":{"href":"https://api.github.com/repos/apple/swift/issues/2717"},"comments":{"href":"https://api.github.com/repos/apple/swift/issues/2717/comments"},"review_comments":{"href":"https://api.github.com/repos/apple/swift/pulls/2717/comments"},"review_comment":{"href":"https://api.github.com/repos/apple/swift/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apple/swift/pulls/2717/commits"},"statuses":{"href":"https://api.github.com/repos/apple/swift/statuses/1ec085f67376dc445901b6cc68e56c1f7a0368bc"}}}}
{"actor":{"display_login":"kballard"}}