compare equality for collections, concat strings with str

This commit is contained in:
Pekka Laiho 2020-06-01 20:12:26 +07:00
parent 69918f3300
commit 9e113faf78
3 changed files with 21 additions and 11 deletions

View File

@ -1,6 +1,7 @@
<?php <?php
namespace MadLisp\Lib; namespace MadLisp\Lib;
use MadLisp\Collection;
use MadLisp\CoreFunc; use MadLisp\CoreFunc;
use MadLisp\Env; use MadLisp\Env;
@ -8,22 +9,20 @@ class Compare implements ILib
{ {
public function register(Env $env): void public function register(Env $env): void
{ {
// TODO: handle collections
$env->set('=', new CoreFunc('=', 'Return true if arguments are equal.', 2, 2, $env->set('=', new CoreFunc('=', 'Return true if arguments are equal.', 2, 2,
fn ($a, $b) => $a == $b fn ($a, $b) => $this->getValue($a) == $this->getValue($b)
)); ));
$env->set('==', new CoreFunc('==', 'Return true if arguments are equal using strict comparison.', 2, 2, $env->set('==', new CoreFunc('==', 'Return true if arguments are equal using strict comparison.', 2, 2,
fn ($a, $b) => $a === $b fn ($a, $b) => $this->getValue($a) === $this->getValue($b)
)); ));
$env->set('!=', new CoreFunc('!=', 'Return true if arguments are not equal.', 2, 2, $env->set('!=', new CoreFunc('!=', 'Return true if arguments are not equal.', 2, 2,
fn ($a, $b) => $a != $b fn ($a, $b) => $this->getValue($a) != $this->getValue($b)
)); ));
$env->set('!==', new CoreFunc('!==', 'Return true if arguments are not equal using strict comparison.', 2, 2, $env->set('!==', new CoreFunc('!==', 'Return true if arguments are not equal using strict comparison.', 2, 2,
fn ($a, $b) => $a !== $b fn ($a, $b) => $this->getValue($a) !== $this->getValue($b)
)); ));
$env->set('<', new CoreFunc('<', 'Return true if first argument is less than second argument.', 2, 2, $env->set('<', new CoreFunc('<', 'Return true if first argument is less than second argument.', 2, 2,
@ -42,4 +41,13 @@ class Compare implements ILib
fn ($a, $b) => $a >= $b fn ($a, $b) => $a >= $b
)); ));
} }
private function getValue($a)
{
if ($a instanceof Collection) {
return $a->getData();
}
return $a;
}
} }

View File

@ -45,5 +45,7 @@ class Math implements ILib
return array_reduce(array_slice($args, 1), fn ($a, $b) => $a % $b, $args[0]); return array_reduce(array_slice($args, 1), fn ($a, $b) => $a % $b, $args[0]);
} }
)); ));
// TODO: add pow, sqrt, floor, ceil, abs
} }
} }

View File

@ -16,20 +16,20 @@ class Types implements ILib
{ {
// Conversions // Conversions
$env->set('to-bool', new CoreFunc('to-bool', 'Convert argument to boolean.', 1, 1, $env->set('bool', new CoreFunc('bool', 'Convert argument to boolean.', 1, 1,
fn ($a) => boolval($a) fn ($a) => boolval($a)
)); ));
$env->set('to-float', new CoreFunc('to-float', 'Convert argument to float.', 1, 1, $env->set('float', new CoreFunc('float', 'Convert argument to float.', 1, 1,
fn ($a) => floatval($a) fn ($a) => floatval($a)
)); ));
$env->set('to-int', new CoreFunc('to-int', 'Convert argument to integer.', 1, 1, $env->set('int', new CoreFunc('int', 'Convert argument to integer.', 1, 1,
fn ($a) => intval($a) fn ($a) => intval($a)
)); ));
$env->set('to-str', new CoreFunc('fn?', 'Convert argument to string.', 1, 1, $env->set('str', new CoreFunc('str', 'Convert arguments to string and concatenate them together.', 1, -1,
fn ($a) => strval($a) fn (...$args) => implode('', array_map('strval', $args))
)); ));
// Test types // Test types