{"action":"created","issue":{"url":"https://api.github.com/repos/vermaseren/form/issues/187","repository_url":"https://api.github.com/repos/vermaseren/form","labels_url":"https://api.github.com/repos/vermaseren/form/issues/187/labels{/name}","comments_url":"https://api.github.com/repos/vermaseren/form/issues/187/comments","events_url":"https://api.github.com/repos/vermaseren/form/issues/187/events","html_url":"https://github.com/vermaseren/form/issues/187","id":229967955,"number":187,"title":"What is the fastest equivalent of Foreach in FORM?","user":{"login":"tueda","id":1481665,"avatar_url":"https://avatars3.githubusercontent.com/u/1481665?v=3","gravatar_id":"","url":"https://api.github.com/users/tueda","html_url":"https://github.com/tueda","followers_url":"https://api.github.com/users/tueda/followers","following_url":"https://api.github.com/users/tueda/following{/other_user}","gists_url":"https://api.github.com/users/tueda/gists{/gist_id}","starred_url":"https://api.github.com/users/tueda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tueda/subscriptions","organizations_url":"https://api.github.com/users/tueda/orgs","repos_url":"https://api.github.com/users/tueda/repos","events_url":"https://api.github.com/users/tueda/events{/privacy}","received_events_url":"https://api.github.com/users/tueda/received_events","type":"User","site_admin":false},"labels":[{"id":55951994,"url":"https://api.github.com/repos/vermaseren/form/labels/question","name":"question","color":"cc317c","default":true}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2017-05-19T13:01:14Z","updated_at":"2017-05-19T15:31:45Z","closed_at":null,"body":"\"Modern\" programming languages (C++, Java, Bourne Shell, AWK, Perl, Python, Ruby, Rust, Mathematica etc.) have [Foreach](https://en.wikipedia.org/wiki/Foreach_loop)-like loop statements that iterate over a list or group of objects:\r\n```c++\r\nfor (const auto &element : my_list) {\r\n  // do something for each element...\r\n}\r\n```\r\n\r\nSuppose we want to do a similar thing for terms stored in a $-variable. For definiteness, we consider finding one element matching to some condition. Is there any good way to do this?\r\n\r\nIf there would be a special `do-in` statement and `breakdo` statement, I could write\r\n```\r\n$list = x*y + y*z + z*x;\r\n$result = 0;\r\ndo $n in $list;\r\n  if (<some condition>);\r\n    $result = <result>;\r\n    breakdo;\r\n  endif;\r\nenddo;\r\n```\r\n\r\nBut for now I may write\r\n```\r\n$list = x*y + y*z + z*x;\r\n$result = 0;\r\n$list_copy = $list;\r\nlabel loop;\r\n$n = termsin_($list_copy);\r\nif ($n);\r\n  $element = firstterm_($list_copy);\r\n  if (<some condition>);\r\n    $result = <result>;\r\n  else;\r\n    $list_copy = $list_copy - $element;\r\n    goto loop;\r\n  endif;\r\nendif;\r\n```\r\nwhich looks rather miserable, especially when `$list` has many terms ~ O(10^3-10^4). Probably a code with a `do`-loop will be messier."},"comment":{"url":"https://api.github.com/repos/vermaseren/form/issues/comments/302735265","html_url":"https://github.com/vermaseren/form/issues/187#issuecomment-302735265","issue_url":"https://api.github.com/repos/vermaseren/form/issues/187","id":302735265,"user":{"login":"tueda","id":1481665,"avatar_url":"https://avatars3.githubusercontent.com/u/1481665?v=3","gravatar_id":"","url":"https://api.github.com/users/tueda","html_url":"https://github.com/tueda","followers_url":"https://api.github.com/users/tueda/followers","following_url":"https://api.github.com/users/tueda/following{/other_user}","gists_url":"https://api.github.com/users/tueda/gists{/gist_id}","starred_url":"https://api.github.com/users/tueda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tueda/subscriptions","organizations_url":"https://api.github.com/users/tueda/orgs","repos_url":"https://api.github.com/users/tueda/repos","events_url":"https://api.github.com/users/tueda/events{/privacy}","received_events_url":"https://api.github.com/users/tueda/received_events","type":"User","site_admin":false},"created_at":"2017-05-19T15:31:45Z","updated_at":"2017-05-19T15:31:45Z","body":"This direction may be not a FORM way; the following code is too slow. Maybe I have to give up and go for another (old and hard) way...\r\n```\r\n#-\r\nS x,x1,...,x20;\r\nCF f,distrib;\r\n\r\nL F = ranperm_(f,x1,...,x20);\r\nid f(?a$args) = 1;\r\n\r\nP \"input: %$\", $args;\r\n\r\n* The number of the arguments.\r\n$n = nargs_($args);\r\n\r\n* Introduce the order as f(n,x).\r\n$tmp1 = f(f(1),$args);\r\ninside $tmp1;\r\n  repeat id f(?a,f(x1?),x2?,?b) = f(?a,f(x1,x2),f(x1+1),?b);\r\n  if (match(f(?a$args,x?)) == 0) exit;\r\nendinside;\r\n\r\n* Try for all combinations in lexicographical order: k-elements from n.\r\n$k = 1;\r\nlabel loop1;\r\n  if ($k < $n);\r\n    $list = distrib(1,$k,f,dummy_,$args) * replace_(distrib,distrib_);\r\nlabel loop2;\r\n      $tmp1 = termsin_($list);\r\n      if ($tmp1);\r\n*       Get a combination.\r\n        $tmp1 = firstterm_($list);\r\n        $list = $list - $tmp1;\r\n        inside $tmp1;\r\n          argument f;\r\n            id f(x1?,x2?) = x2;\r\n          endargument;\r\n          if (match(f(?a$elem)) == 0) exit;\r\n          P \"%$\", $elem;\r\n*         if (<some condition>);\r\n*           <break the outer loop>;\r\n*         endif;\r\n        endinside;\r\n        goto loop2;\r\n      endif;\r\n    $k = $k + 1;\r\n    goto loop1;\r\n  endif;\r\n.end\r\n```"}}
{"actor":{"display_login":"tueda"}}