Optimization: remove some usage of array_map

This commit is contained in:
Pekka Laiho 2020-12-10 20:41:09 +07:00
parent ecc424c249
commit 458d09c007
3 changed files with 27 additions and 16 deletions

View File

@ -150,7 +150,11 @@ class Collections implements ILib
$env->set('chunk', new CoreFunc('chunk', 'Divide first argument (sequence) into new sequences with length of second argument (int).', 2, 2, $env->set('chunk', new CoreFunc('chunk', 'Divide first argument (sequence) into new sequences with length of second argument (int).', 2, 2,
function (Seq $a, int $len) { function (Seq $a, int $len) {
$chunks = array_chunk($a->getData(), $len); $chunks = array_chunk($a->getData(), $len);
return $a::new(array_map(fn ($b) => $a::new($b), $chunks)); $data = [];
foreach ($chunks as $c) {
$data[] = $a::new($c);
}
return $a::new($data);
} }
)); ));
@ -159,7 +163,11 @@ class Collections implements ILib
// This is used by quasiquote, so we need to always return // This is used by quasiquote, so we need to always return
// a list for it to work properly. // a list for it to work properly.
$data = array_map(fn ($a) => $a->getData(), $args); $data = [];
foreach ($args as $a) {
$data[] = $a->getData();
}
return new MList(array_merge(...$data)); return new MList(array_merge(...$data));
} }
)); ));

View File

@ -29,13 +29,15 @@ class Database implements ILib
)); ));
$env->set('db-query', new CoreFunc('db-query', 'Execute a database query.', 2, 4, $env->set('db-query', new CoreFunc('db-query', 'Execute a database query.', 2, 4,
function (PDO $pdo, string $sql, ?Collection $args, bool $rowVectors = false) { function (PDO $pdo, string $sql, ?Collection $args = null, bool $rowVectors = false) {
$stmt = $pdo->prepare($sql); $stmt = $pdo->prepare($sql);
$stmt->execute($args ? $args->getData() : []); $stmt->execute($args ? $args->getData() : []);
$rows = $stmt->fetchAll($rowVectors ? PDO::FETCH_NUM : PDO::FETCH_ASSOC); $rows = $stmt->fetchAll($rowVectors ? PDO::FETCH_NUM : PDO::FETCH_ASSOC);
return new Vector( $data = [];
array_map(fn ($row) => $rowVectors ? new Vector($row) : new Hash($row), $rows) foreach ($rows as $row) {
); $data[] = $rowVectors ? new Vector($row) : new Hash($row);
}
return new Vector($data);
} }
)); ));

View File

@ -30,7 +30,17 @@ class Types implements ILib
)); ));
$env->set('str', new CoreFunc('str', 'Convert arguments to string and concatenate them together.', 0, -1, $env->set('str', new CoreFunc('str', 'Convert arguments to string and concatenate them together.', 0, -1,
fn (...$args) => implode('', array_map([$this, 'getStrValue'], $args)) function (...$args) {
$data = [];
foreach ($args as $a) {
if ($a instanceof Symbol) {
$data[] = $a->getName();
} else {
$data[] = strval($a);
}
}
return implode('', $data);
}
)); ));
$env->set('symbol', new CoreFunc('symbol', 'Convert argument to symbol.', 1, 1, $env->set('symbol', new CoreFunc('symbol', 'Convert argument to symbol.', 1, 1,
@ -167,13 +177,4 @@ class Types implements ILib
fn ($a) => $a % 2 !== 0 fn ($a) => $a % 2 !== 0
)); ));
} }
private function getStrValue($a)
{
if ($a instanceof Symbol) {
return $a->getName();
}
return strval($a);
}
} }