zksecurity / noname

Noname: a programming language to write zkapps
https://zksecurity.github.io/noname/
161 stars 35 forks source link

support iterators by default #132

Open mimoo opened 1 month ago

mimoo commented 1 month ago

we should support iterating through an array without having to index through it. We should also support zipping arrays together. For example, in the iterator.no example we have:

struct Room {
    holes: Field,
}

fn Room.windows(self) -> Field {
    // a door doesn't count as a window
    return self.holes - 1;
}

struct House {
    rooms: [Room; 2],
}

fn House.room(self, const idx: Field) -> Room {
    return self.rooms[idx];
}

fn House.windows(house: House) -> Field {
    let mut windows = 0;
+    for room in house.rooms {
-    for room_idx in 0..2 {
        let room = house.room(room_idx);
        // ideally: windows +=
        windows = windows + room.windows();
    }

    return windows;
}

fn main(pub bedroom_holes: Field) -> Field {
    let bedroom = Room {
        holes: bedroom_holes,
    };

    let livingroom = Room {
        holes: 4,
    };

    let house = House { rooms: [bedroom, livingroom] };

    return House.windows(house);
}