{"action":"created","issue":{"url":"https://api.github.com/repos/stedolan/jq/issues/1283","repository_url":"https://api.github.com/repos/stedolan/jq","labels_url":"https://api.github.com/repos/stedolan/jq/issues/1283/labels{/name}","comments_url":"https://api.github.com/repos/stedolan/jq/issues/1283/comments","events_url":"https://api.github.com/repos/stedolan/jq/issues/1283/events","html_url":"https://github.com/stedolan/jq/issues/1283","id":192816785,"number":1283,"title":"Streams evaluation order (feature or bug?)","user":{"login":"fadado","id":7008746,"avatar_url":"https://avatars.githubusercontent.com/u/7008746?v=3","gravatar_id":"","url":"https://api.github.com/users/fadado","html_url":"https://github.com/fadado","followers_url":"https://api.github.com/users/fadado/followers","following_url":"https://api.github.com/users/fadado/following{/other_user}","gists_url":"https://api.github.com/users/fadado/gists{/gist_id}","starred_url":"https://api.github.com/users/fadado/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fadado/subscriptions","organizations_url":"https://api.github.com/users/fadado/orgs","repos_url":"https://api.github.com/users/fadado/repos","events_url":"https://api.github.com/users/fadado/events{/privacy}","received_events_url":"https://api.github.com/users/fadado/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-12-01T11:12:33Z","updated_at":"2016-12-01T19:09:32Z","closed_at":null,"body":"The evaluation order for streams (generators) in **jq** is different for cases like these\r\n\r\n    $ jq -cn '[(1,3) + (2,3,4,5)]'\r\n    [3,5,4,6,5,7,6,8]\r\n    $ jq -cn 'def sum(a;b): a + b; [sum((1,3); (2,3,4,5))]'\r\n    [3,5,4,6,5,7,6,8]\r\n\r\nand for pipes\r\n\r\n    $ jq -cn '[(1,3) as $x | (2,3,4,5) as $y | $x + $y]'\r\n    [3,4,5,6,5,6,7,8]\r\n\r\nIn the [Icon language](https://www.cs.arizona.edu/icon/docs/docs.htm), one of the direct inspirations for **jq**, the order is always the same (left to rigth). This paragraph from one [Icon paper](https://www.cs.arizona.edu/icon/ftp/doc/tr82_8a.pdf)  describes the Icon behavior:\r\n\r\n>In the absence of control structures, the arguments of a function call are\r\n>evaluated from left to right. If evaluation of any argument expression fails,\r\n>the last argument expression to be evaluated is resumed to produce another\r\n>result. If it is depleted and does not produce a result, the next previous\r\n>argument expression is resumed, and so on. When a resumed argument expression\r\n>produces a result, the remaining argument expressions to the right are\r\n>evaluated again. If the first argument expression fails to produce a result,\r\n>the function is not invoked and the entire expression fails. If all the\r\n>arguments produce a result, the function is invoked with those argument values.\r\n>If the function fails for these argument values, the last argument expression\r\n>is resumed for another result and evaluation of the argument expressions\r\n>proceeds as described above. Thus generators are resumed in a last-in,\r\n>first-out manner.\r\n\r\nMy question is, the dual **jq** behavior is a design feature or not?\r\nI like very much the development of Icon ideas inside a pure functional framework, and I think the inspiration must be deepened, for example including the missing _dot product_ (`zip`) to **jq**.\r\n"},"comment":{"url":"https://api.github.com/repos/stedolan/jq/issues/comments/264264004","html_url":"https://github.com/stedolan/jq/issues/1283#issuecomment-264264004","issue_url":"https://api.github.com/repos/stedolan/jq/issues/1283","id":264264004,"user":{"login":"fadado","id":7008746,"avatar_url":"https://avatars.githubusercontent.com/u/7008746?v=3","gravatar_id":"","url":"https://api.github.com/users/fadado","html_url":"https://github.com/fadado","followers_url":"https://api.github.com/users/fadado/followers","following_url":"https://api.github.com/users/fadado/following{/other_user}","gists_url":"https://api.github.com/users/fadado/gists{/gist_id}","starred_url":"https://api.github.com/users/fadado/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fadado/subscriptions","organizations_url":"https://api.github.com/users/fadado/orgs","repos_url":"https://api.github.com/users/fadado/repos","events_url":"https://api.github.com/users/fadado/events{/privacy}","received_events_url":"https://api.github.com/users/fadado/received_events","type":"User","site_admin":false},"created_at":"2016-12-01T19:09:32Z","updated_at":"2016-12-01T19:09:32Z","body":"Yes, I know, but **jq** `transpose`  expects an array of arrays as input, not streams. **jq**, like Icon, lacks dot product on streams; only cross product is available using pipes. The Icon solution is weird and uses co-expressions; for **jq** I will like a kind of parallel  `reduce` (_fold_) and/or `foreach` (_scan_), but a family of _zip_ and _zipWith_ functions operating on streams  like in Haskell will be also ok.\r\n\r\nI can define a zip on streams like\r\n\r\n```\r\ndef zip(g; h):\r\n    [[g], [h]] as $pair\r\n    | ($pair | map(length) | max) as $longest\r\n    | range($longest)\r\n    | [$pair[0][.], $pair[1][.]]\r\n;\r\n```\r\n\r\nbut only works on finite streams and has to build the whole arrays in memory!\r\nAs an illustration, the following example has to convert streams to arrays for\r\nparallel display of equivalent codes:\r\n\r\n```\r\n#!/usr/bin/jq -nRrf\r\n\r\ndef chars: (./\"\")[];\r\n\r\ndef octcode:\r\n    (\"01\"|chars) as $d2 |\r\n    (\"01234567\"|chars) as $d1 |\r\n    (\"01234567\"|chars) as $d0 |\r\n    $d2+$d1+$d0\r\n;\r\n\r\ndef deccode:\r\n    label $pipe |\r\n    (\"01\"|chars) as $d2 |\r\n    (\"0123456789\"|chars) as $d1 |\r\n    (\"0123456789\"|chars) as $d0 |\r\n    if $d2 == \"1\" and $d1 == \"2\" and $d0 == \"8\"\r\n    then break $pipe\r\n    else $d2+$d1+$d0 end\r\n;\r\n\r\ndef hexcode:\r\n    (\"01234567\"|chars) as $d1 |\r\n    (\"0123456789ABCDEF\"|chars) as $d0 |\r\n    $d1+$d0\r\n;\r\n\r\n[ [octcode], [deccode], [hexcode] ] | transpose\r\n```\r\n\r\nIcon can process in parallel the three streams, as shown in\r\nhttps://www.cs.arizona.edu/icon/ftp/doc/tr82_8a.pdf (page 17).\r\n\r\n"}}
{"actor":{"display_login":"fadado"}}