Add even and odd functions

This commit is contained in:
Laurenz 2022-01-13 14:11:23 +01:00
parent e74ae6ce70
commit c7f52ed048
4 changed files with 20 additions and 1 deletions

View File

@ -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);

View File

@ -22,7 +22,7 @@ impl<S: ShapeKind> ShapeNode<S> {
pub const STROKE: Smart<Option<Paint>> = 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<Node> {

View File

@ -135,6 +135,16 @@ pub fn max(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
minmax(args, Ordering::Greater)
}
/// Whether an integer is even.
pub fn even(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
Ok(Value::Bool(args.expect::<i64>("integer")? % 2 == 0))
}
/// Whether an integer is odd.
pub fn odd(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
Ok(Value::Bool(args.expect::<i64>("integer")? % 2 != 0))
}
/// Find the minimum or maximum of a sequence of values.
fn minmax(args: &mut Args, goal: Ordering) -> TypResult<Value> {
let mut extremum = args.expect::<Value>("value")?;

View File

@ -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%)