From c7f52ed0489de0c144d4684a26f557b7a6ee182e Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 13 Jan 2022 14:11:23 +0100 Subject: [PATCH] Add `even` and `odd` functions --- src/library/mod.rs | 2 ++ src/library/shape.rs | 2 +- src/library/utility.rs | 10 ++++++++++ tests/typ/utility/math.typ | 7 +++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/library/mod.rs b/src/library/mod.rs index cb1177023..c4ec29880 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -129,6 +129,8 @@ pub fn new() -> Scope { std.def_func("abs", abs); std.def_func("min", min); std.def_func("max", max); + std.def_func("even", even); + std.def_func("odd", odd); std.def_func("range", range); std.def_func("rgb", rgb); std.def_func("lower", lower); diff --git a/src/library/shape.rs b/src/library/shape.rs index 32e39b6ab..12126ab45 100644 --- a/src/library/shape.rs +++ b/src/library/shape.rs @@ -22,7 +22,7 @@ impl ShapeNode { pub const STROKE: Smart> = Smart::Auto; /// The stroke's thickness. pub const THICKNESS: Length = Length::pt(1.0); - /// The How much to pad the shape's content. + /// How much to pad the shape's content. pub const PADDING: Linear = Linear::zero(); fn construct(_: &mut EvalContext, args: &mut Args) -> TypResult { diff --git a/src/library/utility.rs b/src/library/utility.rs index 6cc174490..10c5980ab 100644 --- a/src/library/utility.rs +++ b/src/library/utility.rs @@ -135,6 +135,16 @@ pub fn max(_: &mut EvalContext, args: &mut Args) -> TypResult { minmax(args, Ordering::Greater) } +/// Whether an integer is even. +pub fn even(_: &mut EvalContext, args: &mut Args) -> TypResult { + Ok(Value::Bool(args.expect::("integer")? % 2 == 0)) +} + +/// Whether an integer is odd. +pub fn odd(_: &mut EvalContext, args: &mut Args) -> TypResult { + Ok(Value::Bool(args.expect::("integer")? % 2 != 0)) +} + /// Find the minimum or maximum of a sequence of values. fn minmax(args: &mut Args, goal: Ordering) -> TypResult { let mut extremum = args.expect::("value")?; diff --git a/tests/typ/utility/math.typ b/tests/typ/utility/math.typ index c01d497f5..705db9379 100644 --- a/tests/typ/utility/math.typ +++ b/tests/typ/utility/math.typ @@ -11,6 +11,13 @@ #test(abs(-12pt), 12pt) #test(abs(50%), 50%) +--- +// Test the `even` and `odd` functions. +#test(even(2), true) +#test(odd(2), false) +#test(odd(-1), true) +#test(even(-11), false) + --- // Error: 6-16 cannot take absolute value of a linear #abs(10pt + 50%)