mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-22 21:35:03 +00:00
compare equality for collections, concat strings with str
This commit is contained in:
parent
69918f3300
commit
9e113faf78
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace MadLisp\Lib;
|
||||
|
||||
use MadLisp\Collection;
|
||||
use MadLisp\CoreFunc;
|
||||
use MadLisp\Env;
|
||||
|
||||
@ -8,22 +9,20 @@ class Compare implements ILib
|
||||
{
|
||||
public function register(Env $env): void
|
||||
{
|
||||
// TODO: handle collections
|
||||
|
||||
$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,
|
||||
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,
|
||||
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,
|
||||
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,
|
||||
@ -42,4 +41,13 @@ class Compare implements ILib
|
||||
fn ($a, $b) => $a >= $b
|
||||
));
|
||||
}
|
||||
|
||||
private function getValue($a)
|
||||
{
|
||||
if ($a instanceof Collection) {
|
||||
return $a->getData();
|
||||
}
|
||||
|
||||
return $a;
|
||||
}
|
||||
}
|
||||
|
@ -45,5 +45,7 @@ class Math implements ILib
|
||||
return array_reduce(array_slice($args, 1), fn ($a, $b) => $a % $b, $args[0]);
|
||||
}
|
||||
));
|
||||
|
||||
// TODO: add pow, sqrt, floor, ceil, abs
|
||||
}
|
||||
}
|
||||
|
@ -16,20 +16,20 @@ class Types implements ILib
|
||||
{
|
||||
// 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)
|
||||
));
|
||||
|
||||
$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)
|
||||
));
|
||||
|
||||
$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)
|
||||
));
|
||||
|
||||
$env->set('to-str', new CoreFunc('fn?', 'Convert argument to string.', 1, 1,
|
||||
fn ($a) => strval($a)
|
||||
$env->set('str', new CoreFunc('str', 'Convert arguments to string and concatenate them together.', 1, -1,
|
||||
fn (...$args) => implode('', array_map('strval', $args))
|
||||
));
|
||||
|
||||
// Test types
|
||||
|
Loading…
Reference in New Issue
Block a user