Last active
August 29, 2015 14:24
-
-
Save milesrout/b72afd9594d81ea6e7f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace std { | |
| using numeric_range = struct { min,max,stride: int }; | |
| // provide an implementation of the Range concept for numeric_range | |
| impl Range<numeric_range> { | |
| using iterator = struct { min,stride: int }; | |
| using sentinel = struct { max: int }; | |
| let begin(nr: numeric_range) => iterator{nr.min, nr.stride}; | |
| let end(nr: numeric_range) => sentinel{nr.max}; | |
| let next(iter: iterator) => iterator{iter.min + iter.stride, iter.stride}; | |
| let current(iter: iterator) => iter.min; | |
| let __neq__(iter: iterator, sent: sentinel) => (iter.min < sent.max); | |
| }; | |
| let range(min,max,stride=1: int) => numeric_range{min, max, stride}; | |
| } | |
| // prints "1\n2\n3\n4\n5\n6\n7\n8\n9\n10" | |
| let main(args: []string) { | |
| for x in range(0,10).map(i => i + 1) { | |
| "%d\n".printf(x); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rangeis a concept, and concepts useCamelCasenames, as do type parameters.nrwas used then I changed it.implindicates that a particular type (in this case,numeric_range) fulfils the contract of a given concept (in this caseRange).Ranges are things that you can call
beginandendon to get an iterator and a sentinel respectively, which might be the same type. e.g. for a raw array, the begin is a raw pointer to the first element and end is a raw pointer to the last element. for a string, the begin is a pointer to the first element and the length seen so far, while the end is just the length.mapis a generic function that works the same for allRanges:or similar.