Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
zig source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
// Author: Jan Halsema // functional ideas in zig const std = @import("std"); const meta = std.meta; export fn demo() u64 { const list = iterator([]u64{0, 1, 2, 3, 4}); return sum(map(addOne, list)); // list = 0 1 2 3 4 // map -> 1 2 3 4 5 // sum -> 15 } export fn demo2() u64 { var t1: u64 = 0; var t2: u64 = 0; const list = iterator([]u64{0, 1, 2, 3}); var generator = zip(map(addOne, list), list); while (generator.next()) |v| { t1 += v.fst; t2 += v.snd; } return t1 * t2; } fn addOne(a: u64) u64 { return a + 1; } // Warning: here be dragons fn IteratorType(comptime arr_t: var) type { return struct { arr: arr_t, index: usize, const Self = @This(); const TypeOfChild = @typeInfo(arr_t).Array.child; pub fn next(self: *Self) ?TypeOfChild { if (self.index == self.arr.len) return null; defer self.index += 1; return self.arr[self.index]; } pub fn init(arr: var) Self { return Self{ .arr = arr, .index = 0, }; } }; } pub fn iterator(arr: var) IteratorType(@typeOf(arr)) { return IteratorType(@typeOf(arr)).init(arr); } fn MapType(comptime func_t: var, comptime it_t: var) type { return struct { it: it_t, func: func_t, const Self = @This(); pub fn next(self: *Self) ?func_t.ReturnType { return self.func(self.it.next() orelse return null); } pub fn init(func: var, it: var) Self { return Self{ .it = it, .func = func, }; } }; } pub fn map(func: var, it: var) MapType(@typeOf(func), @typeOf(it)) { return MapType(@typeOf(func), @typeOf(it)).init(func, it); } fn ZipType(comptime it1_t: var, comptime it2_t: var) type { return struct { it1: it1_t, it2: it2_t, const Self = @This(); const TypeOfChild1 = meta.Child(@typeOf(it1_t.next).ReturnType); const TypeOfChild2 = meta.Child(@typeOf(it1_t.next).ReturnType); const ReturnType = struct { fst: TypeOfChild1, snd: TypeOfChild2, }; pub fn next(self: *Self) ?ReturnType { return ReturnType { .fst = self.it1.next() orelse return null, .snd = self.it2.next() orelse return null, }; } pub fn init(it1: var, it2: var) Self { return Self{ .it1 = it1, .it2 = it2, }; } }; } pub fn zip(it1: var, it2: var) ZipType(@typeOf(it1), @typeOf(it2)) { return ZipType(@typeOf(it1), @typeOf(it2)).init(it1, it2); } pub fn sum(it: var) meta.Child(@typeOf(@typeOf(it).next).ReturnType) { var total: meta.Child(@typeOf(@typeOf(it).next).ReturnType) = 0; var it_ = it; while (it_.next()) |v| total +%= v; return total; }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree