{"action":"created","issue":{"url":"https://api.github.com/repos/micropython/micropython-lib/issues/41","labels_url":"https://api.github.com/repos/micropython/micropython-lib/issues/41/labels{/name}","comments_url":"https://api.github.com/repos/micropython/micropython-lib/issues/41/comments","events_url":"https://api.github.com/repos/micropython/micropython-lib/issues/41/events","html_url":"https://github.com/micropython/micropython-lib/issues/41","id":102974360,"number":41,"title":"itertools.cycle fails for generators","user":{"login":"stinos","id":1902997,"avatar_url":"https://avatars.githubusercontent.com/u/1902997?v=3","gravatar_id":"","url":"https://api.github.com/users/stinos","html_url":"https://github.com/stinos","followers_url":"https://api.github.com/users/stinos/followers","following_url":"https://api.github.com/users/stinos/following{/other_user}","gists_url":"https://api.github.com/users/stinos/gists{/gist_id}","starred_url":"https://api.github.com/users/stinos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stinos/subscriptions","organizations_url":"https://api.github.com/users/stinos/orgs","repos_url":"https://api.github.com/users/stinos/repos","events_url":"https://api.github.com/users/stinos/events{/privacy}","received_events_url":"https://api.github.com/users/stinos/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-08-25T08:22:07Z","updated_at":"2015-08-25T13:12:27Z","closed_at":null,"body":"The cycle implementation does not make a copy of the elements iterated over and as such only works for proper sequences like lists, but not for all types of iterables:\r\n\r\n```\r\nfrom itertools import cycle\r\n\r\ndef reverse(l):\r\n    while l:\r\n        yield l.pop()\r\n\r\nfor x in cycle(reverse([1, 2, 3])):\r\n    print(x)\r\n```\r\n\r\noutput:\r\n\r\n```\r\n3\r\n2\r\n1\r\n\r\n```\r\n\r\nProposed fix: just copy the equivalent python implementation?\r\n\r\n```\r\ndef cycle(iterable):\r\n    saved = []\r\n    for element in iterable:\r\n        yield element\r\n        saved.append(element)\r\n    while saved:\r\n        for element in saved:\r\n              yield element\r\n```"},"comment":{"url":"https://api.github.com/repos/micropython/micropython-lib/issues/comments/134580219","html_url":"https://github.com/micropython/micropython-lib/issues/41#issuecomment-134580219","issue_url":"https://api.github.com/repos/micropython/micropython-lib/issues/41","id":134580219,"user":{"login":"dpgeorge","id":6187689,"avatar_url":"https://avatars.githubusercontent.com/u/6187689?v=3","gravatar_id":"","url":"https://api.github.com/users/dpgeorge","html_url":"https://github.com/dpgeorge","followers_url":"https://api.github.com/users/dpgeorge/followers","following_url":"https://api.github.com/users/dpgeorge/following{/other_user}","gists_url":"https://api.github.com/users/dpgeorge/gists{/gist_id}","starred_url":"https://api.github.com/users/dpgeorge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dpgeorge/subscriptions","organizations_url":"https://api.github.com/users/dpgeorge/orgs","repos_url":"https://api.github.com/users/dpgeorge/repos","events_url":"https://api.github.com/users/dpgeorge/events{/privacy}","received_events_url":"https://api.github.com/users/dpgeorge/received_events","type":"User","site_admin":false},"created_at":"2015-08-25T13:12:27Z","updated_at":"2015-08-25T13:12:27Z","body":"That definition almost mandates the given implementation.  And, as it says, it's expected to use lots of memory."}}