mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-11-13 21:40:46 -05:00
dev.1711 - switched to multi-object-for-loops
This commit is contained in:
parent
f9b3d50824
commit
e7326dc5f9
9 changed files with 18 additions and 16 deletions
|
@ -42,7 +42,7 @@ Verify the installation and build number of `zig` like so:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ zig version
|
$ zig version
|
||||||
0.11.0-dev.1650+xxxxxxxxx
|
0.11.0-dev.11711+xxxxxxxxx
|
||||||
```
|
```
|
||||||
|
|
||||||
Clone this repository with Git:
|
Clone this repository with Git:
|
||||||
|
@ -82,7 +82,8 @@ about input:
|
||||||
|
|
||||||
### Version Changes
|
### Version Changes
|
||||||
|
|
||||||
Version-0.11.0-dev.1650+xxxxxxxxx
|
Version-0.11.0-dev.11711+xxxxxxxxx
|
||||||
|
* *2023-02-21* zig 0.11.0-dev.1711 - changes in `for loops` - new: Multi Object For Loops + Struct-of-Arrays
|
||||||
* *2023-02-12* zig 0.11.0-dev.1638 - changes in `std.Build` cache_root now returns a directory struct
|
* *2023-02-12* zig 0.11.0-dev.1638 - changes in `std.Build` cache_root now returns a directory struct
|
||||||
* *2023-02-04* zig 0.11.0-dev.1568 - changes in `std.Build` (combine `std.build` and `std.build.Builder` into `std.Build`)
|
* *2023-02-04* zig 0.11.0-dev.1568 - changes in `std.Build` (combine `std.build` and `std.build.Builder` into `std.Build`)
|
||||||
* *2023-01-14* zig 0.11.0-dev.1302 - changes in `@addWithOverflow` (now returns a tuple) and `@typeInfo`; temporary disabled async functionality
|
* *2023-01-14* zig 0.11.0-dev.1302 - changes in `@addWithOverflow` (now returns a tuple) and `@typeInfo`; temporary disabled async functionality
|
||||||
|
|
|
@ -8,7 +8,7 @@ const print = std.debug.print;
|
||||||
// When changing this version, be sure to also update README.md in two places:
|
// When changing this version, be sure to also update README.md in two places:
|
||||||
// 1) Getting Started
|
// 1) Getting Started
|
||||||
// 2) Version Changes
|
// 2) Version Changes
|
||||||
const needed_version = std.SemanticVersion.parse("0.11.0-dev.1650") catch unreachable;
|
const needed_version = std.SemanticVersion.parse("0.11.0-dev.1711") catch unreachable;
|
||||||
|
|
||||||
const Exercise = struct {
|
const Exercise = struct {
|
||||||
/// main_file must have the format key_name.zig.
|
/// main_file must have the format key_name.zig.
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
//
|
//
|
||||||
// For loops also let you store the "index" of the iteration - a
|
// For loops also let you use the "index" of the iteration, a number
|
||||||
// number starting with 0 that counts up with each iteration:
|
// that counts up with each iteration. To access the index of iteration,
|
||||||
|
// specify a second condition as well as a second capture value.
|
||||||
//
|
//
|
||||||
// for (items) |item, index| {
|
// for (items, 0..) |item, index| {
|
||||||
//
|
//
|
||||||
// // Do something with item and index
|
// // Do something with item and index
|
||||||
//
|
//
|
||||||
|
@ -23,8 +24,8 @@ pub fn main() void {
|
||||||
// Now we'll convert the binary bits to a number value by adding
|
// Now we'll convert the binary bits to a number value by adding
|
||||||
// the value of the place as a power of two for each bit.
|
// the value of the place as a power of two for each bit.
|
||||||
//
|
//
|
||||||
// See if you can figure out the missing piece:
|
// See if you can figure out the missing pieces:
|
||||||
for (bits) |bit, ???| {
|
for (bits, ???) |bit, ???| {
|
||||||
// Note that we convert the usize i to a u32 with
|
// Note that we convert the usize i to a u32 with
|
||||||
// @intCast(), a builtin function just like @import().
|
// @intCast(), a builtin function just like @import().
|
||||||
// We'll learn about these properly in a later exercise.
|
// We'll learn about these properly in a later exercise.
|
||||||
|
|
|
@ -44,7 +44,7 @@ pub fn main() void {
|
||||||
// it do and why?
|
// it do and why?
|
||||||
|
|
||||||
// Printing all RPG characters in a loop:
|
// Printing all RPG characters in a loop:
|
||||||
for (chars) |c, num| {
|
for (chars, 0..) |c, num| {
|
||||||
std.debug.print("Character {} - G:{} H:{} XP:{}\n", .{
|
std.debug.print("Character {} - G:{} H:{} XP:{}\n", .{
|
||||||
num + 1, c.gold, c.health, c.experience,
|
num + 1, c.gold, c.health, c.experience,
|
||||||
});
|
});
|
||||||
|
|
|
@ -86,7 +86,7 @@ pub fn main() void {
|
||||||
aliens_alive = 0;
|
aliens_alive = 0;
|
||||||
|
|
||||||
// Loop through every alien by reference (* makes a pointer capture value)
|
// Loop through every alien by reference (* makes a pointer capture value)
|
||||||
for (aliens) |*alien| {
|
for (&aliens) |*alien| {
|
||||||
|
|
||||||
// *** Zap the alien with the heat ray here! ***
|
// *** Zap the alien with the heat ray here! ***
|
||||||
???.zap(???);
|
???.zap(???);
|
||||||
|
|
|
@ -239,7 +239,7 @@ const HermitsNotebook = struct {
|
||||||
// We'll often want to find an entry by Place. If one is not
|
// We'll often want to find an entry by Place. If one is not
|
||||||
// found, we return null.
|
// found, we return null.
|
||||||
fn getEntry(self: *HermitsNotebook, place: *const Place) ?*NotebookEntry {
|
fn getEntry(self: *HermitsNotebook, place: *const Place) ?*NotebookEntry {
|
||||||
for (self.entries) |*entry, i| {
|
for (&self.entries, 0..) |*entry, i| {
|
||||||
if (i >= self.end_of_entries) break;
|
if (i >= self.end_of_entries) break;
|
||||||
|
|
||||||
// Here's where the hermit got stuck. We need to return
|
// Here's where the hermit got stuck. We need to return
|
||||||
|
|
|
@ -106,7 +106,7 @@ pub fn main() void {
|
||||||
const meal = food_loop: for (menu) |food| {
|
const meal = food_loop: for (menu) |food| {
|
||||||
|
|
||||||
// Now look at each required ingredient for the Food...
|
// Now look at each required ingredient for the Food...
|
||||||
for (food.requires) |required, required_ingredient| {
|
for (food.requires, 0..) |required, required_ingredient| {
|
||||||
|
|
||||||
// This ingredient isn't required, so skip it.
|
// This ingredient isn't required, so skip it.
|
||||||
if (!required) continue;
|
if (!required) continue;
|
||||||
|
|
|
@ -102,7 +102,7 @@ const HermitsNotebook = struct {
|
||||||
end_of_entries: u8 = 0,
|
end_of_entries: u8 = 0,
|
||||||
|
|
||||||
fn getEntry(self: *HermitsNotebook, place: *const Place) ?*NotebookEntry {
|
fn getEntry(self: *HermitsNotebook, place: *const Place) ?*NotebookEntry {
|
||||||
for (self.entries) |*entry, i| {
|
for (&self.entries, 0..) |*entry, i| {
|
||||||
if (i >= self.end_of_entries) break;
|
if (i >= self.end_of_entries) break;
|
||||||
if (place == entry.*.?.place) return &entry.*.?;
|
if (place == entry.*.?.place) return &entry.*.?;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
27c27
|
28c28
|
||||||
< for (bits) |bit, ???| {
|
< for (bits, ???) |bit, ???| {
|
||||||
---
|
---
|
||||||
> for (bits) |bit, i| {
|
> for (bits, 0..) |bit, i| {
|
||||||
|
|
Loading…
Reference in a new issue