mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-22 18:19:54 -04:00
DOCS: Update standard library documentation.
This commit is contained in:
parent
68790520d4
commit
69b573aed5
@ -11,5 +11,4 @@ manipulation, and some helpful testing assertions can all be found in the
|
|||||||
standard library.
|
standard library.
|
||||||
|
|
||||||
Each library is documented here in it's own section. Most operations in
|
Each library is documented here in it's own section. Most operations in
|
||||||
the standard library are in the form of modules and by convention you get
|
the standard library are in the form of modules.
|
||||||
the result of an operation from the `result` field after running the module.
|
|
21
docsite/site/content/stdlib/functional.md
Normal file
21
docsite/site/content/stdlib/functional.md
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
+++
|
||||||
|
title = "UCG functional operations"
|
||||||
|
weight = 5
|
||||||
|
sort_by = "weight"
|
||||||
|
in_search_index = true
|
||||||
|
+++
|
||||||
|
|
||||||
|
UCG comes with some builtin functional operations.
|
||||||
|
|
||||||
|
## Useful functions
|
||||||
|
|
||||||
|
* identity - the Identity function. Returns it's argument unchanged.
|
||||||
|
|
||||||
|
## Maybe module
|
||||||
|
|
||||||
|
Maybe is a monadic style wrapper for values that might be NULL. It provides a several operations for the wrapped value.
|
||||||
|
|
||||||
|
* do(op) - runs op which is a function of one argument against the wrapped value if it is not null. Returns the result or NULL wrapped in another maybe.
|
||||||
|
* is_null() - returns true if the wrapped value is null, false otherwise.
|
||||||
|
* unwrap() - returns the wrapped value
|
||||||
|
* expect(msg) - returns the wrapped value if it is not null. Throws a compile error with the user provided message otherwise.
|
@ -15,7 +15,7 @@ The reverse module reverses a list. It has a single required parameter:
|
|||||||
|
|
||||||
```
|
```
|
||||||
let l = import "std/lists.ucg";
|
let l = import "std/lists.ucg";
|
||||||
l.reverse{list=[1, 2, 3]}.result == [3, 2, 1];
|
l.reverse{list=[1, 2, 3]} == [3, 2, 1];
|
||||||
```
|
```
|
||||||
|
|
||||||
## str_join
|
## str_join
|
||||||
@ -31,7 +31,7 @@ let l = import "std/lists.ucg";
|
|||||||
l.str_join{
|
l.str_join{
|
||||||
sep=" ",
|
sep=" ",
|
||||||
list=[1, 2, 3]
|
list=[1, 2, 3]
|
||||||
}.result == "1,2,3";
|
} == "1,2,3";
|
||||||
```
|
```
|
||||||
|
|
||||||
## len
|
## len
|
||||||
@ -42,7 +42,7 @@ The len module returns the length of a list. It has a single required parameter.
|
|||||||
|
|
||||||
```
|
```
|
||||||
let l = import "std/lists.ucg";
|
let l = import "std/lists.ucg";
|
||||||
l.len{list=[0, 1, 2, 3]}.result == 4;
|
l.len{list=[0, 1, 2, 3]} == 4;
|
||||||
```
|
```
|
||||||
|
|
||||||
## enumerate
|
## enumerate
|
||||||
@ -57,12 +57,12 @@ The enumerate module enumerates the elements of a list. It has three parameters.
|
|||||||
let l = import "std/lists.ucg";
|
let l = import "std/lists.ucg";
|
||||||
|
|
||||||
// with defaults
|
// with defaults
|
||||||
l.enumerate{list=[1, 2, 3]}.result == [[0, 1], [1, 2], [3, 4]];
|
l.enumerate{list=[1, 2, 3]} == [[0, 1], [1, 2], [3, 4]];
|
||||||
|
|
||||||
// With all parameters
|
// With all parameters
|
||||||
l.enumerate{
|
l.enumerate{
|
||||||
start=1,
|
start=1,
|
||||||
step=2,
|
step=2,
|
||||||
list=["foo", "bar", "foobar"],
|
list=["foo", "bar", "foobar"],
|
||||||
}.result == [[1, "foo"], [3, "bar"], [5, "foobar"]];
|
} == [[1, "foo"], [3, "bar"], [5, "foobar"]];
|
||||||
```
|
```
|
@ -16,7 +16,7 @@ result field of the computed tuple will be true.
|
|||||||
let fits = schema.shaped{
|
let fits = schema.shaped{
|
||||||
val={foo="bar", inner=[1, 2]},
|
val={foo="bar", inner=[1, 2]},
|
||||||
shape={foo="", inner=[]}
|
shape={foo="", inner=[]}
|
||||||
}.result;
|
};
|
||||||
|
|
||||||
fits == true;
|
fits == true;
|
||||||
```
|
```
|
||||||
@ -28,7 +28,7 @@ let exact_fit = schema.shaped{
|
|||||||
partial=false,
|
partial=false,
|
||||||
val={foo="bar", count=1},
|
val={foo="bar", count=1},
|
||||||
shape={foo=""},
|
shape={foo=""},
|
||||||
}.result;
|
};
|
||||||
|
|
||||||
exact_fit == false;
|
exact_fit == false;
|
||||||
```
|
```
|
||||||
@ -42,7 +42,7 @@ The `any` module tests a value against a list of possible shapes. If the value
|
|||||||
fits any of the candidate shapes then it stores true in the result field. If it does not then it returns false in that result field.
|
fits any of the candidate shapes then it stores true in the result field. If it does not then it returns false in that result field.
|
||||||
|
|
||||||
```
|
```
|
||||||
let fits_one_of = any{val={foo="bar"}, types=[1, {foo=""}]}.result;
|
let fits_one_of = any{val={foo="bar"}, types=[1, {foo=""}]};
|
||||||
fits_one_of == true;
|
fits_one_of == true;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ parameter.
|
|||||||
|
|
||||||
```
|
```
|
||||||
let tpl = import "std/tuples.ucg";
|
let tpl = import "std/tuples.ucg";
|
||||||
tpl.fields{tpl={foo=1, bar=2}}.result == ["foo", "bar"];
|
tpl.fields{tpl={foo=1, bar=2}} == ["foo", "bar"];
|
||||||
```
|
```
|
||||||
|
|
||||||
## values
|
## values
|
||||||
@ -28,7 +28,7 @@ The `values` module retrieves all the values in a tuple. It has one parameter.
|
|||||||
|
|
||||||
```
|
```
|
||||||
let tpl = import "std/tuples.ucg";
|
let tpl = import "std/tuples.ucg";
|
||||||
tpl.values{tpl={foo=1, bar=2}}.result == [1, 2];
|
tpl.values{tpl={foo=1, bar=2}} == [1, 2];
|
||||||
```
|
```
|
||||||
|
|
||||||
## iter
|
## iter
|
||||||
@ -40,5 +40,5 @@ It has one parameter.
|
|||||||
|
|
||||||
```
|
```
|
||||||
let tpl = import "std/tuples.ucg";
|
let tpl = import "std/tuples.ucg";
|
||||||
tpl.enumerate{tpl={foo=1, bar=2}}.result == [["foo", 1], ["bar", 2]];
|
tpl.enumerate{tpl={foo=1, bar=2}} == [["foo", 1], ["bar", 2]];
|
||||||
```
|
```
|
Loading…
x
Reference in New Issue
Block a user