Skip to content

Commit

Permalink
Consistently use markdown heading for examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vkleen committed Mar 27, 2023
1 parent a2f31ea commit 0de20b2
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 93 deletions.
62 changes: 41 additions & 21 deletions stdlib/array.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
array = {
NonEmpty
| doc m%"
Contract to ensure an array is not empty.
Contract to enforce an array is not empty.

# Examples

For example:
```nickel
([] | NonEmpty) =>
error
Expand Down Expand Up @@ -51,7 +52,8 @@
| doc m%"
Returns the given array without its first element.

For example:
# Examples

```nickel
drop_first [ 1, 2, 3 ] =>
[ 2, 3 ]
Expand All @@ -63,7 +65,8 @@
| doc m%"
Returns the given array without its last element.

For example:
# Examples

```nickel
drop_last [ 1, 2, 3 ] =>
[ 1, 2 ]
Expand All @@ -75,7 +78,8 @@
| doc m%"
Returns the length of the given array.

For example:
# Examples

```nickel
length [ "Hello,", " World!" ] =>
2
Expand All @@ -87,7 +91,8 @@
| doc m%"
Apply a function to every element in the given array.

For example:
# Examples

```nickel
map f [ x1, x2, ..., xn ] =>
[ f x1, f x2, ..., f xn ]
Expand All @@ -102,7 +107,8 @@
| doc m%"
Retrieve the n'th element from an array, with indices starting at 0.

For example:
# Examples

```nickel
at 3 [ "zero" "one" "two" "three" "four" ] =>
"three"
Expand All @@ -114,7 +120,8 @@
| doc m%"
Append the second array to the first one.

For example:
# Examples

```nickel
concat [ 1, 2, 3 ] [ 4, 5, 6 ] =>
[ 1, 2, 3, 4, 5, 6 ]
Expand Down Expand Up @@ -235,7 +242,8 @@
| doc m%"
Build an array given the first element and the rest of the array.

For example:
# Examples

```nickel
prepend 1 [ 2, 3 ] =>
[ 1, 2, 3 ]
Expand All @@ -247,7 +255,8 @@
| doc m%"
Build an array given the last element and the rest of the array.

For example:
# Examples

```nickel
append 3 [ 1, 2 ] =>
[ 1, 2, 3 ]
Expand All @@ -260,7 +269,8 @@
| doc m%"
Reverse an array.

For example:
# Examples

```nickel
reverse [ 1, 2, 3 ] =>
[ 3, 2, 1 ]
Expand All @@ -272,7 +282,8 @@
| doc m%"
`filter f xs` returns an array containing all elements from `xs` that satisfy `f`.

For example:
# Examples

```nickel
filter (fun x => x <= 3) [ 4, 3, 2, 5, 1 ] =>
[ 3, 2, 1 ]
Expand All @@ -284,7 +295,8 @@
| doc m%"
Concatenate all elements of a given array of arrays.

For example:
# Examples

```nickel
flatten [[1, 2], [3, 4]] =>
[1, 2, 3, 4]
Expand All @@ -296,7 +308,8 @@
| doc m%"
Returns true if all elements in the given array satisfy the predicate, false otherwise.

For example:
# Examples

```nickel
all (fun x => x < 3) [ 1, 2 ] =>
true
Expand All @@ -310,7 +323,8 @@
| doc m%"
Returns true if at least one element in the given array satisfies the predicate, false otherwise.

For example:
# Examples

```nickel
any (fun x => x < 3) [ 1, 2, 3, 4 ] =>
true
Expand All @@ -324,7 +338,8 @@
| doc m%"
Returns true if the given value appears in the array, false otherwise.

For example:
# Examples

```nickel
elem 3 [ 1, 2, 3, 4, 5 ] =>
true
Expand All @@ -338,7 +353,8 @@
elements that satisfy the predicate, while `wrong` will contain those
that do not.

For example:
# Examples

```nickel
partition (fun x => x < 5) [ 2, 4, 5, 3, 7, 8, 6 ] =>
{ right = [ 3, 4, 2 ], wrong = [ 6, 8, 7, 5 ] }
Expand All @@ -360,7 +376,8 @@
generate f n = [ f 0, f 1, ..., f (n - 1) ]
```

For example:
# Examples

```nickel
generate (fun x => x * x) 4 =>
[ 0, 1, 4, 9 ]
Expand All @@ -372,7 +389,8 @@
| doc m%"
Sort the given array based on the provided comparison operator.

For example:
# Examples

```nickel
sort (fun x y => if x < y then `Lesser else if (x == y) then `Equal else `Greater) [ 4, 5, 1, 2 ] =>
[ 1, 2, 4, 5 ]
Expand All @@ -394,7 +412,8 @@
First `map` the given function over the array and then `flatten` the
result.

For example:
# Examples

```nickel
flat_map (fun x => [x, x]) [1, 2, 3]
=> [1, 1, 2, 2, 3, 3]
Expand All @@ -406,7 +425,8 @@
| doc m%"
Intersperse a value between the elements of an array.

For example:
# Examples

```nickel
intersperse ", " [ "Hello", "wonderful", "world!" ]
=> [ "Hello", ", ", "wonderful", ", ", "world!" ]
Expand Down
51 changes: 34 additions & 17 deletions stdlib/builtin.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
| doc m%"
Check if a value is a number.

For example:
# Examples

```nickel
is_number 1 =>
true
Expand All @@ -18,7 +19,8 @@
| doc m%"
Check if a value is a bool.

For example:
# Examples

```nickel
is_bool false =>
true
Expand All @@ -32,7 +34,8 @@
| doc m%"
Check if a value is a string.

For example:
# Examples

```nickel
is_string true =>
false
Expand All @@ -46,7 +49,8 @@
| doc m%"
Check if a value is an enum tag.

For example:
# Examples

```nickel
is_enum true =>
false
Expand All @@ -61,7 +65,8 @@
| doc m%"
Check if a value is a function.

For example
# Examples

```nickel
is_function (fun x => x) =>
true
Expand All @@ -75,7 +80,8 @@
| doc m%"
Check if a value is an array.

For example
# Examples

```nickel
is_array [ 1, 2 ] =>
true
Expand All @@ -89,7 +95,8 @@
| doc m%"
Check if a value is a record.

For example
# Examples

```nickel
is_record [ 1, 2 ] =>
false
Expand All @@ -113,7 +120,8 @@
| doc m%"
Returns the type of a value.

For example:
# Examples

```nickel
typeof [ 1, 2 ] =>
`Array
Expand All @@ -127,7 +135,8 @@
| doc m%"
`seq x y` forces the evaluation of `x`, before returning `y`.

For example:
# Examples

```nickel
seq (42 / 0) 37 =>
error
Expand All @@ -143,7 +152,8 @@
| doc m%"
`deep_seq x y` forces a deep evaluation `x`, before returning `y`.

For example:
# Examples

```nickel
deep_seq (42 / 0) 37 =>
error
Expand All @@ -159,7 +169,8 @@
| doc m%"
Hash the given string with the desired hashing algorithm.

For example:
# Examples

```nickel
hash `Md5 "hunter2" =>
"2ab96390c7dbe3439de74d0c9b0b1767"
Expand All @@ -171,7 +182,8 @@
| doc m%"
Serialize a value into the desired representation.

For example:
# Examples

```nickel
serialize `Json { hello = "Hello", world = "World" } =>
"{
Expand All @@ -186,7 +198,8 @@
| doc m%"
Deserialize a string into a nickel value from the given representation.

For example:
# Examples

```nickel
deserialize `Json "{ \"hello\": \"Hello\", \"world\": \"World\" }"
{ hello = "Hello", world = "World" }
Expand All @@ -199,7 +212,8 @@
Converts a stringable value to a string representation. Same as
`string.from`.

For example:
# Examples

```nickel
from 42 =>
"42"
Expand All @@ -216,7 +230,8 @@
`builtin.trace msg x` prints `msg` to standard error, then proceeds with
the evaluation of `x`.

For example:
# Examples

```nickel
builtin.trace "Hello, world!" true =>
builtin.trace: Hello, world!
Expand All @@ -229,7 +244,8 @@
| doc m%"
A contract that always fails with the given message.

For example:
# Examples

```nickel
1 | FailWith "message" =>
error: contract broken by a value: message
Expand All @@ -250,7 +266,8 @@
| doc m%"
Abort evaluation with the given message.

For example:
# Examples

```nickel
fail_with "message" =>
error: contract broken by a value: message
Expand Down

0 comments on commit 0de20b2

Please sign in to comment.