Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add two examples: imports and foreach pattern #1387

Merged
merged 10 commits into from
Jul 6, 2023
Merged
16 changes: 16 additions & 0 deletions examples/foreach-pattern/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Foreach Pattern

This example shows how a _foreach pattern_ works in Nickel using
`std.array.map`. A second example combines a yaml-import with the map function.

## Run

```console
nickel -f foreach-pattern.ncl export
```

Second example:

```console
nickel -f foreach-pattern-on-import.ncl export
```
6 changes: 6 additions & 0 deletions examples/foreach-pattern/data_users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
users:
- jane
- pete
- richie
- ellen
17 changes: 17 additions & 0 deletions examples/foreach-pattern/foreach-pattern-on-import.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# test = 'ignore'
# fails because YAML files are source filtered
vkleen marked this conversation as resolved.
Show resolved Hide resolved
let users =
vkleen marked this conversation as resolved.
Show resolved Hide resolved
(import "data_users.yml").users
|> std.array.map
(
fun name =>
{
username = name,
email = "%{name}@nickel-lang.org"
}
)
in

{
posix_users = users
}
16 changes: 16 additions & 0 deletions examples/foreach-pattern/foreach-pattern.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# test = 'pass'
let users =
vkleen marked this conversation as resolved.
Show resolved Hide resolved
["jane", "pete", "richie"]
|> std.array.map
(
fun name =>
{
username = name,
email = "%{name}@nickel-lang.org"
}
)
in

{
usernames = users
}
9 changes: 9 additions & 0 deletions examples/imports/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Various Imports

This example shows how Nickel can transparently import common serialization formats.

## Run

```console
nickel -f imports.ncl export
```
7 changes: 7 additions & 0 deletions examples/imports/data_groups.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"groups": [
"desktop-users",
"administrators",
"terminal-users"
]
}
5 changes: 5 additions & 0 deletions examples/imports/data_machines.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
machines = [
"VAX-11",
"PDP-1",
"Bomba"
]
22 changes: 22 additions & 0 deletions examples/imports/data_nickel_properties.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# test = 'pass'
let kelvin_to_celcius = fun kelvin => kelvin - 273.15 in
vkleen marked this conversation as resolved.
Show resolved Hide resolved
let kelvin_to_fahrenheit = fun kelvin => (kelvin - 273.15) * 1.8000 + 32.00 in

let melting_celcius = std.string.from_number (kelvin_to_celcius 1728) in
let melting_fahrenheit = std.string.from_number (kelvin_to_fahrenheit 1728) in

let boiling_celcius = std.string.from_number (kelvin_to_celcius 3003) in
let boiling_fahrenheit = std.string.from_number (kelvin_to_fahrenheit 3003) in

{
physical = {
phase_at_STP = "solid",
melting_point = "1728 K (%{melting_celcius} °C, %{melting_fahrenheit} °F)",
boiling_point = "3003 K (%{boiling_celcius} °C, %{boiling_fahrenheit} °F)",
density = "8.908 g/cm3",
when_liquid = "7.81 g/cm3",
heat_of_fusion = "17.48 kJ/mol",
heat_of_vaporization = "379 kJ/mol",
molar_heat_capacity = "26.07 J/(mol·K)"
}
}
6 changes: 6 additions & 0 deletions examples/imports/data_users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
users:
- jane
- pete
- richie
- ellen
22 changes: 22 additions & 0 deletions examples/imports/imports.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# test = 'ignore'
# Fails because `yml`, `json` and `toml` files are source filtered
vkleen marked this conversation as resolved.
Show resolved Hide resolved

# Nickel can import plain yaml, or json files
vkleen marked this conversation as resolved.
Show resolved Hide resolved
let _users = (import "data_users.yml") in
let _groups = (import "data_groups.json") in

# It even imports toml
let _machines = (import "data_machines.toml") in

# And of course other nickel files
let _nickel_properties = (import "data_nickel_properties.ncl") in

# This is the output object
{
users = _users.users,
groups = _groups.groups,
machines = _machines.machines,
off_topic = {
nickel_properties = _nickel_properties
}
}