diff --git a/examples/foreach-pattern/README.md b/examples/foreach-pattern/README.md new file mode 100644 index 0000000000..bb384bec2b --- /dev/null +++ b/examples/foreach-pattern/README.md @@ -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 +``` diff --git a/examples/foreach-pattern/data_users.yml b/examples/foreach-pattern/data_users.yml new file mode 100644 index 0000000000..2efbdc0442 --- /dev/null +++ b/examples/foreach-pattern/data_users.yml @@ -0,0 +1,6 @@ +--- +users: + - jane + - pete + - richie + - ellen diff --git a/examples/foreach-pattern/foreach-pattern-on-import.ncl b/examples/foreach-pattern/foreach-pattern-on-import.ncl new file mode 100644 index 0000000000..328d607685 --- /dev/null +++ b/examples/foreach-pattern/foreach-pattern-on-import.ncl @@ -0,0 +1,16 @@ +# test = 'ignore' +let users = + (import "data_users.yml").users + |> std.array.map + ( + fun name => + { + username = name, + email = "%{name}@nickel-lang.org" + } + ) +in + +{ + posix_users = users +} diff --git a/examples/foreach-pattern/foreach-pattern.ncl b/examples/foreach-pattern/foreach-pattern.ncl new file mode 100644 index 0000000000..b3c28f2ec6 --- /dev/null +++ b/examples/foreach-pattern/foreach-pattern.ncl @@ -0,0 +1,16 @@ +# test = 'pass' +let users = + ["jane", "pete", "richie"] + |> std.array.map + ( + fun name => + { + username = name, + email = "%{name}@nickel-lang.org" + } + ) +in + +{ + usernames = users +} diff --git a/examples/imports/README.md b/examples/imports/README.md new file mode 100644 index 0000000000..c15ba6f0d5 --- /dev/null +++ b/examples/imports/README.md @@ -0,0 +1,9 @@ +# Various Imports + +This example shows how Nickel can transparently import common serialization formats. + +## Run + +```console +nickel -f imports.ncl export +``` diff --git a/examples/imports/data_groups.json b/examples/imports/data_groups.json new file mode 100644 index 0000000000..a0128d7f17 --- /dev/null +++ b/examples/imports/data_groups.json @@ -0,0 +1,7 @@ +{ + "groups": [ + "desktop-users", + "administrators", + "terminal-users" + ] +} diff --git a/examples/imports/data_machines.toml b/examples/imports/data_machines.toml new file mode 100644 index 0000000000..2d3bd08e78 --- /dev/null +++ b/examples/imports/data_machines.toml @@ -0,0 +1,5 @@ +machines = [ + "VAX-11", + "PDP-1", + "Bomba" +] diff --git a/examples/imports/data_nickel_properties.ncl b/examples/imports/data_nickel_properties.ncl new file mode 100644 index 0000000000..de8a14b6d6 --- /dev/null +++ b/examples/imports/data_nickel_properties.ncl @@ -0,0 +1,22 @@ +# test = 'pass' +let kelvin_to_celcius = fun kelvin => kelvin - 273.15 in +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)" + } +} diff --git a/examples/imports/data_users.yml b/examples/imports/data_users.yml new file mode 100644 index 0000000000..2efbdc0442 --- /dev/null +++ b/examples/imports/data_users.yml @@ -0,0 +1,6 @@ +--- +users: + - jane + - pete + - richie + - ellen diff --git a/examples/imports/imports.ncl b/examples/imports/imports.ncl new file mode 100644 index 0000000000..21e9a81a78 --- /dev/null +++ b/examples/imports/imports.ncl @@ -0,0 +1,21 @@ +# test = 'ignore' + +# 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 + } +}