Skip to content

Commit

Permalink
Add at_or returing n-th value or default
Browse files Browse the repository at this point in the history
  • Loading branch information
olorin37 committed May 21, 2024
1 parent ea41f82 commit e224f7e
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions core/stdlib/std.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,25 @@
"%
= fun n l => %elem_at% l n,

at_or
: forall a. Number -> a -> Array a -> a
| doc m%"
Retrieves the n-th element from an array, with indices starting at 0
or provided value in case index is out of bounds.

# Examples

```nickel
std.array.at 3 "default" [ "zero", "one" ] =>
"default"
```
"%
= fun n default_value l =>
if n < %length% then
%elem_at% l n
else
default_value,

concat
: forall a. Array a -> Array a -> Array a
| doc m%"
Expand Down

0 comments on commit e224f7e

Please sign in to comment.