Skip to content

Instantly share code, notes, and snippets.

@andrewrk
Last active April 9, 2020 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewrk/a9700ff88bf6ee060c483bdfb33899f7 to your computer and use it in GitHub Desktop.
Save andrewrk/a9700ff88bf6ee060c483bdfb33899f7 to your computer and use it in GitHub Desktop.
// Based on source here:
// https://www.reddit.com/r/programming/comments/fxswxe/c_needs_better_syntax_and_macros/fmx4f0y/
// (plant "far-field" "Old McDonald's Farm")
// (harvest "Old McDonald's Farm")
// (grind "stone-mill")
// (prepare-dough "sea-salt" "white-sugar" "ev-olive-oil")
// (make-base)
// (add-toppings "rosa-tomatoes" "mozzarella" "ham")
// (bake "stone oven")
// (enjoy))
// To run this code:
// zig run old-mcdonald-had-a-farm.zig
const std = @import("std");
pub fn main() void {
const directions = .{
.{ plant, .{ "far-field", "Old McDonald's Farm" } },
.{ harvest, .{"Old McDonald's Farm"} },
.{ grind, .{"stone-mill"} },
.{ prepareDough, .{ "sea-salt", "white-sugar", "ev-olive-oil" } },
.{ makeBase, .{} },
.{ addToppings, .{ "rosa-tomatoes", "mozzarella", "ham" } },
.{ enjoy, .{} },
};
var value: i32 = 10;
const opts: std.builtin.CallOptions = .{};
inline for (directions) |direction| {
value = @call(opts, direction[0], .{value} ++ direction[1]);
}
std.debug.warn("result={}\n", .{value});
}
fn plant(prev: i32, a: []const u8, b: []const u8) i32 {
return prev + 1;
}
fn harvest(prev: i32, a: []const u8) i32 {
return prev + 1;
}
fn grind(prev: i32, a: []const u8) i32 {
return prev + 1;
}
fn prepareDough(prev: i32, a: []const u8, b: []const u8, c: []const u8) i32 {
return prev + 1;
}
fn makeBase(prev: i32) i32 {
return prev + 1;
}
fn addToppings(prev: i32, a: []const u8, b: []const u8, c: []const u8) i32 {
return prev + 1;
}
fn enjoy(prev: i32) i32 {
return prev + 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment