{"action":"created","issue":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/23969","repository_url":"https://api.github.com/repos/JuliaLang/julia","labels_url":"https://api.github.com/repos/JuliaLang/julia/issues/23969/labels{/name}","comments_url":"https://api.github.com/repos/JuliaLang/julia/issues/23969/comments","events_url":"https://api.github.com/repos/JuliaLang/julia/issues/23969/events","html_url":"https://github.com/JuliaLang/julia/issues/23969","id":262210031,"number":23969,"title":"Adding number of elements to return for first and last","user":{"login":"bkamins","id":6187170,"avatar_url":"https://avatars3.githubusercontent.com/u/6187170?v=4","gravatar_id":"","url":"https://api.github.com/users/bkamins","html_url":"https://github.com/bkamins","followers_url":"https://api.github.com/users/bkamins/followers","following_url":"https://api.github.com/users/bkamins/following{/other_user}","gists_url":"https://api.github.com/users/bkamins/gists{/gist_id}","starred_url":"https://api.github.com/users/bkamins/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bkamins/subscriptions","organizations_url":"https://api.github.com/users/bkamins/orgs","repos_url":"https://api.github.com/users/bkamins/repos","events_url":"https://api.github.com/users/bkamins/events{/privacy}","received_events_url":"https://api.github.com/users/bkamins/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":8,"created_at":"2017-10-02T20:06:17Z","updated_at":"2017-11-06T19:38:59Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"Following the discussion in #23765 regarding implementing `first` and `last` for general collections I wanted to clarify implementation assumptions before submitting a PR (or deciding against implementing it).\r\n\r\nCurrent status: `first` is defined for general collections, but `last` is not, as it is only defined for cases where it is possible to retrieve last element in O(1), effectively when `endof` is defined.\r\n\r\nSo now the question is for what class of iterables we want to have `first` and `last`. Assume we want to get `k` elements from collection of size `n`.\r\n* what should be the acceptable cost of `first`: O(1) or O(k)? if it is O(1) - we are restricted to indeaxable types, where something like `x[1:k]` would work; if we accept O(k) for general iterables then we have `IterTools.takestrict` generates an appropriate object;\r\n* what should be the acceptable cost of `last`: O(1) or O(n)? - here the situation is even more complex (in the general case you have to use `next` iterating from the beginning and identify the location from which you have `k` positions till the end and additionally it could fail as some iterables can be infinite)\r\n\r\nThe case why `first` and `last` with `nchar` functions are useful for strings is simple: they are indexable but it was not simple to get the appropriate locations due to variable character width and both operations are executed in O(k) time.\r\n\r\nBut I am not so clear about the general case - either it is already relatively simple to get head and tail of the collection (like in arrays) or each case would have to be treated separately to ensure efficiency.\r\nAdditionally a general solution for `first` already exists in the form of `IterTools.takestrict`. It could be moved to `Base` as `first`. And a general solution for `last` is problematic as iterables may be infinite (e.g. `IterTools.repeatedly`).\r\n\r\n@StefanKarpinski @stevengj @nalimilan: given the above what would you see as the appropriate approach as I do not see a clean general solution (but maybe I am missing something here)?"},"comment":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/comments/342262107","html_url":"https://github.com/JuliaLang/julia/issues/23969#issuecomment-342262107","issue_url":"https://api.github.com/repos/JuliaLang/julia/issues/23969","id":342262107,"user":{"login":"stevengj","id":2913679,"avatar_url":"https://avatars3.githubusercontent.com/u/2913679?v=4","gravatar_id":"","url":"https://api.github.com/users/stevengj","html_url":"https://github.com/stevengj","followers_url":"https://api.github.com/users/stevengj/followers","following_url":"https://api.github.com/users/stevengj/following{/other_user}","gists_url":"https://api.github.com/users/stevengj/gists{/gist_id}","starred_url":"https://api.github.com/users/stevengj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stevengj/subscriptions","organizations_url":"https://api.github.com/users/stevengj/orgs","repos_url":"https://api.github.com/users/stevengj/repos","events_url":"https://api.github.com/users/stevengj/events{/privacy}","received_events_url":"https://api.github.com/users/stevengj/received_events","type":"User","site_admin":false},"created_at":"2017-11-06T19:38:59Z","updated_at":"2017-11-06T19:38:59Z","author_association":"OWNER","body":"Now that #24187 is merged, we can have `last(itr, n) = first(Iterators.reverse(itr), n)`"}}
{"actor":{"display_login":"stevengj"}}