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:

```
mipmip marked this conversation as resolved.
Show resolved Hide resolved
nickel -f foreach-pattern-on-import export
mipmip marked this conversation as resolved.
Show resolved Hide resolved
```
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
12 changes: 12 additions & 0 deletions examples/foreach-pattern/foreach-pattern-on-import.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let users = (import "data_users.yml").users |> std.array.map
(
fun name =>
{
username = name,
email = "%{name}@nickel-lang.org"
}
) in

{
posix_users = users
}
12 changes: 12 additions & 0 deletions examples/foreach-pattern/foreach-pattern.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let users = ["jane", "pete", "richie"] |> std.array.map
(
fun name =>
{
username = name,
email = "%{name}@nickel-lang.org"
}
) in

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

This example shows how Nickel and import different filetypes without needing
extra boilerplate code.
mipmip marked this conversation as resolved.
Show resolved Hide resolved

## Run

```console
nickel -f various-imports.ncl export
```
7 changes: 7 additions & 0 deletions examples/various-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/various-imports/data_machines.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
machines = [
"VAX-11",
"PDP-1",
"Bomba"
]
15 changes: 15 additions & 0 deletions examples/various-imports/data_nickel_properties.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let kelvin_to_celcius = fun kelvin => kelvin - 273.15 in
let kelvin_to_fahrenheit = fun kelvin => (kelvin - 273.15) * 1.8000 + 32.00 in

{
physical = {
phase_at_STP = "solid",
melting_point = "1728 K (%{std.string.from_number (kelvin_to_celcius 1728)} °C, %{std.string.from_number(kelvin_to_fahrenheit 1728)} °F)",
boiling_point = "3003 K (%{std.string.from_number (kelvin_to_celcius 3003)} °C, %{std.string.from_number(kelvin_to_fahrenheit 3003)} °F)",
mipmip marked this conversation as resolved.
Show resolved Hide resolved
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/various-imports/data_users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
users:
- jane
- pete
- richie
- ellen
19 changes: 19 additions & 0 deletions examples/various-imports/various-imports.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Nickel can import plain yaml, or json files
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
}
}