add usort

This commit is contained in:
Pekka Laiho 2020-10-17 13:32:57 +07:00
parent 0937042e07
commit a7e4524470
2 changed files with 9 additions and 0 deletions

View File

@ -179,6 +179,7 @@ keys | `(keys {"a" 1 "b" 2})` | `("a" "b")` | Return a list of the keys for a
values | `(values {"a" 1 "b" 2})` | `(1 2)` | Return a list of the values for a hash-map. values | `(values {"a" 1 "b" 2})` | `(1 2)` | Return a list of the values for a hash-map.
zip | `(zip ["a" "b"] [1 2])` | `{"a":1 "b":2}` | Create a hash-map using the first sequence as keys and the second as values. Uses [array_combine](https://www.php.net/manual/en/function.array-combine.php) internally. zip | `(zip ["a" "b"] [1 2])` | `{"a":1 "b":2}` | Create a hash-map using the first sequence as keys and the second as values. Uses [array_combine](https://www.php.net/manual/en/function.array-combine.php) internally.
sort | `(sort [6 4 8 1])` | `[1 4 6 8]` | Sort the sequence using [sort](https://www.php.net/manual/en/function.sort.php). sort | `(sort [6 4 8 1])` | `[1 4 6 8]` | Sort the sequence using [sort](https://www.php.net/manual/en/function.sort.php).
usort | `(usort (fn (a b) (if (< a b) 0 1)) [3 1 5 4 2])` | `[1 2 3 4 5]` | Sort the sequence using custom comparison function using [usort](https://www.php.net/manual/en/function.usort.php).
### Comparison functions ### Comparison functions

View File

@ -240,5 +240,13 @@ class Collections implements ILib
return $a::new($data); return $a::new($data);
} }
)); ));
$env->set('usort', new CoreFunc('usort', 'Sort the sequence using custom comparison function.', 2, 2,
function (Func $f, Seq $a) {
$data = $a->getData();
usort($data, $f->getClosure());
return $a::new($data);
}
));
} }
} }