winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
5.05k stars 196 forks source link

For Statement #118

Closed staycoolcall911 closed 1 year ago

staycoolcall911 commented 2 years ago

https://github.com/winglang/wing/blob/main/docs/04-reference/winglang-spec.md#27-for

@yoav-steinberg: Partial support exists. Still missing many elements:

staycoolcall911 commented 2 years ago

@yoav-steinberg: Partial support exists. Still missing many elements:

marciocadev commented 1 year ago

The for statement example is weird 2.7 for statement

the wing code looks like it will print from 0 to 100 (or 99)

for item in 0..100 {
  print(item);
}

but the Typescript equivalent print from 99 to 0

// calling 0..100 does not allocate, just returns an iterator
function* iterator(lim) { let i = lim; while (i--) yield i; }
const iter = iterator(100);
for (const val of iter) {
  console.log(val);
}

I think it would be better if

for item in 0..100 iterates from 0 to 99

function* iterator(start, end) {
  let i = start;
  while (i < end) yield i++;
  while (i > end) yield--;
}
const iter = iterator(100, 0);
for (const val of iter) {
  console.log(val);
}

and for item in 100..0 iterates from 100 to 1

function* iterator(start, end) {
  let i = start;
  while (i < end) yield i++;
  while (i > end) yield i--;
}
const iter = iterator(0, 100);
for (const val of iter) {
  console.log(val);
}
Chriscbr commented 1 year ago

@marciocadev Good catch, this looks to me like a bug in the spec. 🙂 Your proposal makes sense. would you like to submit a fix?

staycoolcall911 commented 1 year ago

@MarkMcCulloh added type checking for iterable in #1412, still left to support the non in format of for loops.

marciocadev commented 1 year ago

Wow, iterator works now? Amazing

monadabot commented 1 year ago

Congrats! :rocket: This was released in Wing 0.8.35.