zoop.tuple
There are three types of Tuple:
empty
:.{}
raw
:.{a, b, ...}
tuple
:struct { pub const value = .{a, b, ...}; }
The last one is the normalized form, and all calculations on Tuple output are tuple
.
tuple.Init
pub fn Init(comptime any: anytype) type;
Function: Convert any comptime
value into a tuple
parameter:
any
: anycomptime
value
Returns: normalized tuple
example:
const result = tuple.Init(.{.one, .two});
result: struct { pub const value = .{.one, .two}; }
const result = tuple.Init(.one);
result: struct { pub const value = .{.one}; }
const result = tuple.Init(struct { const x = 1; });
result: struct { pub const value = .{struct { const x = 1; }}; }
tuple.Append
pub fn Append(comptime any: anytype, comptime any2: anytype) type;
Function: Returns a new tuple
, the content of which is the value of any2 added after any
parameter:
any
: anycomptime
valueany2
: anycomptime
value
Returns: A new tuple
containing any with the contents of any2 appended
example:
const result = tuple.Append(.{.one, .two}, .{.one, .three});
result: struct { pub const value = .{.one, .two, .one, .three}; }
tuple.AppendUnique
pub fn AppendUnique(comptime any: anytype, comptime any2: anytype) type;
Function: Returns a new tuple
, which contains the value of any2 added after any and without duplicates
parameter:
any
: anycomptime
valueany2
: anycomptime
value
Returns: A tuple containing any appended with any2 and without duplicates
example:
const result = tuple.AppendUnique(.one, .{.one, .two});
result: struct { pub const value = .{.one, .two}; }
tuple.Remove
pub fn Remove(comptime any: anytype, comptime any_remove: anytype) type;
Function: Returns a new tuple
containing all the values in any that have not appeared in any_remove
parameter:
any
: anycomptime
valueany_remove
: anycomptime
value
Returns: A tuple
containing all the values in any that are not present in any_remove
example:
const result = tuple.Remove(.{1,2,3,4}, .{2, 3});
result: struct { pub const value = .{1, 4}; }
tuple.Unique
pub fn Unique(comptime any: anytype) type;
Function: Remove duplicate items in any
parameter:
any
: anycomptime
value
Returns: any tuple after deduplication
example:
const result = tuple.Unique(.{1, 2, 1, 3, 1, 4});
result: struct { pub const value = .{1, 2, 3, 4}; }
tuple.Intersection
pub fn Intersection(comptime any: anytype, comptime any2: anytype) type
Function: Find the intersection of any and any2
any
: anycomptime
valueany2
: anycomptime
value
Returns: the intersection of any and any2 tuple
example:
const result = tuple.Intersection(.{ 1, 2, 3, 4 }, .{ 4, 5, 6 });
result: struct { pub const value = .{4}; }
tuple.len
pub inline fn len(any: anytype) comptime_int;
Function: Calculate the length of any
parameter:
any
: anycomptime
value
Returns: the length of any
example:
const t = std.testing;
try t.expect(tuple.len(.{}) == 0);
try t.expect(tuple.len(8) == 1);
try t.expect(tuple.len(.{1}) == 1);
try t.expect(tuple.len(tuple.Init(.{1, .x})) == 2);
tuple.isRaw
pub inline fn isRaw(any: anytype) bool;
Function: Test whether any is of raw type
parameter:
any
: anycomptime
value
Return: if it is raw type, return true
, otherwise return false
example:
const t = std.testing;
try t.expect(isRaw(.{1,2}) == true);
try t.expect(isRaw(tuple.Init(.{1,2})) == false);
tuple.isEmpty
pub inline fn isEmpty(any: anytype) bool;
Function: Test whether any is .{}
parameter:
any
: anycomptime
value
Returns: any is .{}
returns true
otherwise returns false
example:
const t = std.testing;
try t.expect(tuple.isEmpty(.{}) == true);
try t.expect(tuple.isEmpty(.{1}) == false);
tuple.existed
pub inline fn existed(any: anytype, val: anytype) bool;
Function: Test whether any contains val
parameter:
any
: anycomptime
valueval
: anycomptime
value
Returns: any containing val returns true
otherwise false
example:
const t = std.testing;
try t.expect(tuple.existed(.{1, 2, .x}, .x) == true);
try t.expect(tuple.existed(.{1, 2, .x}, .y) == false);