{"id":1854,"date":"2012-10-18T20:00:25","date_gmt":"2012-10-18T19:00:25","guid":{"rendered":"https:\/\/www.reenigne.org\/blog\/?p=1854"},"modified":"2012-09-23T09:16:27","modified_gmt":"2012-09-23T08:16:27","slug":"sequence-types-in-alfe","status":"publish","type":"post","link":"https:\/\/www.reenigne.org\/blog\/sequence-types-in-alfe\/","title":{"rendered":"Sequence types in ALFE"},"content":{"rendered":"<p>This is part of the <a href=\"https:\/\/www.reenigne.org\/blog\/the-alfe-type-system\">ALFE types series<\/a>.<\/p>\n<p><strong>[Foo]<\/strong> is the type of an immutable sequence of <strong>Foo<\/strong> values (the sequence itself is immutable but the values in it are not necessarily immutable). It is syntactic sugar for <strong>Sequence&lt;Foo&gt;<\/strong>. It has methods \"Boolean isEmpty()\", \"Foo first()\" and \"[Foo] rest()\". As usual the actual implementation is up to the compiler (and may even be different for the same sequence in different parts of the same program). Values like <strong>[]<\/strong>, <strong>[foo]<\/strong> and <strong>[foo1, foo2]<\/strong> are allowed.<\/p>\n<p>There is also a special operator for creating sequences of consecutive integers: \"..\". It's closed on the left and open on the right, so <strong>0..5<\/strong> is equivalent to <strong>[0, 1, 2, 3, 4]<\/strong>. Infinite sequences like <strong>0..<\/strong> are also allowed, since there's no need in the <strong>Sequence&lt;&gt;<\/strong> interface to know how many elements there are up front. I'd like to have consecutive sequences that count up in steps other than 1, but I haven't decided on a good syntax for that yet. Python's <a href=\"http:\/\/docs.python.org\/release\/2.3.5\/whatsnew\/section-slices.html\">extended slicing syntax<\/a> is quite nice, but I think \"..\" is more natural than \":\", and I already have quite a lot of meanings for the latter.<\/p>\n<p>The sequence type can also be used as the return type for a generator function:<\/p>\n<pre lang=\"c\">\r\n[Int] primes()\r\n{\r\n    Primes = Class : [Int] {\r\n        construct(Int m = 2) { n = m; }\r\n        Boolean isEmpty() { return false; }\r\n        Int first() { return n; }\r\n        [Int] rest()\r\n        {\r\n            for (Int m in (n+1)..) {\r\n                Int i = 2;\r\n                while (m\/i >= i) {\r\n                    if (m % i == 0)\r\n                        break;\r\n                    ++i;\r\n                }\r\n                done\r\n                    return Primes(m);\r\n            }\r\n        }\r\n        Int n;\r\n    };\r\n    return Primes;\r\n}\r\n<\/pre>\n<p>Disregarding the usage of trial division instead of a more efficient sieve of Eratosthenes, it may seem terribly inefficient to create a whole new <strong>Primes<\/strong> object each time we get the next element of the sequence. However, it is the expectation that the compiler should inline the entire <strong>rest<\/strong> function into the calling loop so that all the object creation and destruction is optimized away.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is part of the ALFE types series. [Foo] is the type of an immutable sequence of Foo values (the sequence itself is immutable but the values in it are not necessarily immutable). It is syntactic sugar for Sequence&lt;Foo&gt;. It has methods \"Boolean isEmpty()\", \"Foo first()\" and \"[Foo] rest()\". As usual the actual implementation is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,27],"tags":[],"class_list":["post-1854","post","type-post","status-publish","format-standard","hentry","category-computer","category-language"],"_links":{"self":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/1854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/comments?post=1854"}],"version-history":[{"count":5,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/1854\/revisions"}],"predecessor-version":[{"id":1856,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/1854\/revisions\/1856"}],"wp:attachment":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/media?parent=1854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/categories?post=1854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/tags?post=1854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}