Guest User

Untitled

a guest
Feb 9th, 2022
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.38 KB | None | 0 0
  1. // Using tests for convenience. To run tests it's just
  2. // $ zig test {file}
  3. // plus whatever else it needs to run, of course, in this case it
  4. // needs -lc to link libc
  5.  
  6. const std = @import("std");
  7. const math = std.math;
  8. const c = @cImport({
  9.     @cInclude("stdio.h");
  10.     @cInclude("string.h");
  11.     // I don't even know if zig's stdlib has threads, but it doesn't
  12.     // matter, because posix threads just work(tm)
  13.     @cInclude("pthread.h");
  14. });
  15.  
  16. // Shit that you don't use just doesn't get included in the final
  17. // binary
  18. const print = std.debug.print;
  19. const expect = std.testing.expect;
  20.  
  21. test "loops" {
  22.     var i: u32 = 20;
  23.    
  24.     while (i < 10) : ({ i += 1; }) {
  25.         print("Kek, jk, nobody uses this form of while\n"
  26.                   ++ "Use defer instead\n", .{});
  27.     }
  28.  
  29.     while (i > 0) {
  30.         defer i -= 1;
  31.         print("Doing shit...\n", .{});
  32.     }
  33.  
  34.     // defer can do blocks too:
  35.     defer({
  36.         i = 10;
  37.         while (i > 0) {
  38.             defer i -= 1;
  39.             print("Fuck you\n", .{});
  40.         }
  41.     });
  42.  
  43.     // Prefer consts because they can be optimized away completely
  44.     const arr1 = [_]u32{ 10, 20, 30 };
  45.     const arr2 = [_]u32{ 40, 20, 60, 20, 30 };
  46.     i = 0;
  47.     for (arr1) |v| {
  48.         // You can't shadow variables, btw, which is shit imo
  49.         // I guess that's an incentive to keep your namespaces clean
  50.         i += v;
  51.     }
  52.  
  53.     try expect(i == 60);
  54.    
  55.     // Slices are a pointer + a length, and they aren't immutable
  56.     var slice: []const u32 = arr1[0..2];
  57.     // I can just change it, this kills the rustranny
  58.     slice.ptr = &arr2;
  59.     slice.len = arr2.len;
  60.  
  61.     i = 0;
  62.     for (slice) |v| {
  63.         i += v;
  64.     }
  65.  
  66.  
  67.     try expect(i == 40 + 20 + 60 + 20 + 30);
  68.  
  69.     // You can label loops, and even return values with break:
  70.     const found = loop: for (arr2) |v| {
  71.         if (v == 22)
  72.             break :loop true;
  73.     } else false;
  74.     // You can also inline loops
  75.    
  76.     // Zig complains if you don't use what you declare, use _ = * for
  77.     // that. Works with function return types too, in case of c
  78.     // functions.
  79.     _ = found;
  80.  
  81.     // Everything in the stdlib that allocates memory takes a
  82.     // memory allocator. You'll want to link with libc because at the
  83.     // moment zig doesn't have a general purpose allocator, so you
  84.     // can just use c_allocator
  85.     var arrlist = try std.ArrayList(u8)
  86.         .initCapacity(std.heap.c_allocator, 200);
  87.     defer arrlist.deinit();
  88.    
  89.     _ = c.sprintf(arrlist.items.ptr, "Pajeet list %s\n", ":)");
  90.     arrlist.items.len = c.strlen(arrlist.items.ptr);
  91.     try arrlist.appendSlice("Good morning sirs!\n");
  92.     print("{s}\n", .{arrlist.items});
  93. }
  94.  
  95. // Here's structures:
  96. const Point = struct {
  97.     x: i32,
  98.     y: i32,
  99.  
  100.     // They also double as namespaces
  101.     const center = Point{ 0, 0 };
  102.  
  103.     // If a function starts with the type or a pointer to the type,
  104.     // you can do x.fn() syntax, but you can also use Point.fn(x).
  105.     fn len(p1: Point, p2: Point) f32 {
  106.         // Language is verbose as fuck with casts...
  107.         const x1 = @intToFloat(f32, p1.x);
  108.         const y1 = @intToFloat(f32, p1.y);
  109.         const x2 = @intToFloat(f32, p2.x);
  110.         const y2 = @intToFloat(f32, p2.y);
  111.  
  112.         return math.sqrt(
  113.             math.pow(f32, x1 - x2, @as(f32, 2)) +
  114.             math.pow(f32, y1 - y2, @as(f32, 2)));
  115.     }
  116. };
  117.  
Add Comment
Please, Sign In to add comment