all repos — h3rald @ b344d2df0d1e247f6eea6ec07a51b774ddf6e6fb

The sources of https://h3rald.com

Fixed zig example
Fabio Cevasco h3rald@h3rald.com
Sat, 22 Dec 2018 13:10:24 +0100
commit

b344d2df0d1e247f6eea6ec07a51b774ddf6e6fb

parent

4e4239aa4af32ac66aaad972134bf1d083f58d6f

1 files changed, 12 insertions(+), 13 deletions(-)

jump to
M contents/articles/10-more-programming-languages.mdcontents/articles/10-more-programming-languages.md

@@ -334,25 +334,24 @@

#### Example FizzBuzz Implementation ``` -fn fizzbuzz(n: usize) -> []u8 { - var result: var = []u8{}; - {var i: usize = 1; while (i <= 100; i += 1) { - if (i % 3 == 0 && i % 5 == 0) { - result = result ++ "FizzBuzz\n"; +const std = @import("std"); + +pub fn main() void { + var i: usize = 1; + while (i <= 100) : (i += 1) { + if (i % 3 == 0 and i % 5 == 0) { + std.debug.warn("FizzBuzz\n"); } else if (i % 3 == 0) { - result = result ++ "Fizz\n"; + std.debug.warn("Fizz\n"); } else if (i % 5 == 0) { - result = result ++ "Buzz\n"; + std.debug.warn("Buzz\n"); } else { - var buf: [20]u8 = undefined; - const len = io.buf_print_unsigned(usize, buf[0...], i); - result = result ++ buf[0...len] ++ "\n"; + std.debug.warn("{}\n", i); } - }} - return result; + } } ``` -_(adapted from [this gist](https://gist.github.com/andrewrk/6780fa252b693169897686e907c9da2a) by Andrew Kelly)_ +_(updated based on [this Reddit comment](https://www.reddit.com/r/programming/comments/a8cbm6/comment/ecapcf5?st=JPZF7J15&sh=1ae81c06))_ #### To get you started...