{"action":"created","issue":{"url":"https://api.github.com/repos/coffeescript6/discuss/issues/11","repository_url":"https://api.github.com/repos/coffeescript6/discuss","labels_url":"https://api.github.com/repos/coffeescript6/discuss/issues/11/labels{/name}","comments_url":"https://api.github.com/repos/coffeescript6/discuss/issues/11/comments","events_url":"https://api.github.com/repos/coffeescript6/discuss/issues/11/events","html_url":"https://github.com/coffeescript6/discuss/issues/11","id":167163655,"number":11,"title":"for-in/for-of","user":{"login":"rattrayalex","id":704302,"avatar_url":"https://avatars.githubusercontent.com/u/704302?v=3","gravatar_id":"","url":"https://api.github.com/users/rattrayalex","html_url":"https://github.com/rattrayalex","followers_url":"https://api.github.com/users/rattrayalex/followers","following_url":"https://api.github.com/users/rattrayalex/following{/other_user}","gists_url":"https://api.github.com/users/rattrayalex/gists{/gist_id}","starred_url":"https://api.github.com/users/rattrayalex/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rattrayalex/subscriptions","organizations_url":"https://api.github.com/users/rattrayalex/orgs","repos_url":"https://api.github.com/users/rattrayalex/repos","events_url":"https://api.github.com/users/rattrayalex/events{/privacy}","received_events_url":"https://api.github.com/users/rattrayalex/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/coffeescript6/discuss/labels/feature%20discussion","name":"feature discussion","color":"0052cc"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-07-23T02:59:28Z","updated_at":"2016-07-28T20:03:25Z","closed_at":null,"body":"CoffeeScript and ES6 have very different takes on these features. CoffeeScript's version is arguably more useful, but also more complicated – and non-standard. \r\n\r\nWhat should we do?"},"comment":{"url":"https://api.github.com/repos/coffeescript6/discuss/issues/comments/236008668","html_url":"https://github.com/coffeescript6/discuss/issues/11#issuecomment-236008668","issue_url":"https://api.github.com/repos/coffeescript6/discuss/issues/11","id":236008668,"user":{"login":"carlsmith","id":7561668,"avatar_url":"https://avatars.githubusercontent.com/u/7561668?v=3","gravatar_id":"","url":"https://api.github.com/users/carlsmith","html_url":"https://github.com/carlsmith","followers_url":"https://api.github.com/users/carlsmith/followers","following_url":"https://api.github.com/users/carlsmith/following{/other_user}","gists_url":"https://api.github.com/users/carlsmith/gists{/gist_id}","starred_url":"https://api.github.com/users/carlsmith/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carlsmith/subscriptions","organizations_url":"https://api.github.com/users/carlsmith/orgs","repos_url":"https://api.github.com/users/carlsmith/repos","events_url":"https://api.github.com/users/carlsmith/events{/privacy}","received_events_url":"https://api.github.com/users/carlsmith/received_events","type":"User","site_admin":false},"created_at":"2016-07-28T20:03:25Z","updated_at":"2016-07-28T20:03:25Z","body":"So, in Python, you only have one kind of for-loop, and it always iterates over a sequence. You can use it on strings, lists and sets.\r\n\r\n~~~ python\r\n$ for n in [1, 2, 3]: print n\r\n1\r\n2\r\n3\r\n~~~\r\n\r\nYou can also use it on a dictionary, where it will iterate over the keys.\r\n\r\n~~~ python\r\n$ for key in {\"foo\": 1, \"bar\": 2}: print key\r\nfoo\r\nbar\r\n~~~\r\n\r\nIf you want to iterate over the values, just call `values` to get a list of them:\r\n\r\n~~~ python\r\n$ for value in {\"foo\": 1, \"bar\": 2}.values(): print value\r\n1\r\n2\r\n~~~\r\n\r\nPython also supports unpacking sequences in assignment statements. It's a lot like CoffeeScript's version, but doesn't require any brackets.\r\n\r\n~~~ python\r\n$ x, y, z = [1, 2, 3]\r\n$ print x + y + z\r\n6\r\n~~~\r\n\r\nPython combines the two ideas, and supports unpacking a sequence inside of a for-loop.\r\n\r\n~~~ python\r\n$ sequence_of_tuples = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\r\n$ for x, y, z in sequence_of_tuples: print x + y + z\r\n6\r\n15\r\n24\r\n~~~\r\n\r\nWhile this doesn't seem that helpful at first, it is very powerful once you have some functions and methods that feed into it. For example, CoffeeScript has its magical fudge for getting the index:\r\n\r\n~~~ coffee\r\n# (console.log index, char) for char, index in \"abc\"\r\n0 a\r\n1 b\r\n2 c\r\n~~~\r\n\r\nIn Python, there's no magic involved. You just use the `enumerate` function  that takes a sequence, and returns an enumeration of the sequence, which is essentially a list of index-item tuples.\r\n\r\n~~~ python\r\n$ print list(enumerate(\"abc\"))\r\n[(0, 'a'), (1, 'b'), (2, 'c')]\r\n~~~\r\n\r\nSo we can then do this to get the same effect as CoffeeScript, without any magic:\r\n\r\n~~~\r\n$ for index, character in enumerate(\"abc\"): print index, character\r\n0 a\r\n1 b\r\n2 c\r\n~~~\r\n\r\nThe main advantage of not using magic is that we can use the same feature to iterate over anything we like, not just index-item tuples.\r\n\r\n~~~ python\r\n$ for key, value in {\"foo\": 1, \"bar\": 2}.items(): print key, value\r\nfoo 1\r\nbar 2\r\n~~~\r\n\r\n~~~ python\r\n$ colors = [(225, 0, 60), (0, 255, 100)]\r\n$ for r, g, b in colors: use(r, g, b)\r\n~~~\r\n\r\nAnd so on. There's lots of ways to break a structure into a sequence of pairs or triples or n-length tuples.\r\n\r\nPython doesn't need to distinguish between for-in and for-of. A for-loop is a for-loop. It also doesn't need magic to get the index. Python just combines its concept of a basic for-loop and its concept of unpacking a sequence, and everything else flows naturally from there.\r\n\r\nPython actually takes iteration much further, but it's all possible because they got their for-loops right, and could build on them."}}
{"actor":{"display_login":"carlsmith"}}