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

Update/refresh examples using latest Nickel idioms #1849

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions examples/arrays/arrays.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ let my_array_lib = {
let tail = std.array.drop_first arr in
[f head] @ map f tail,

fold : forall a b. (a -> b -> b) -> Array a -> b -> b = fun f arr first =>
fold : forall a b. (a -> b -> b) -> b -> Array a -> b = fun f first arr =>
if arr == [] then
first
else
let head = std.array.first arr in
let tail = std.array.drop_first arr in
f head (fold f tail first),
f head (fold f first tail),
}
in
# Compute `7!`
let l = my_array_lib.map (fun x => x + 1) [1, 2, 3, 4, 5, 6] in
my_array_lib.fold (fun x acc => x * acc) l 1
[1, 2, 3, 4, 5, 6]
|> my_array_lib.map ((+) 1)
|> my_array_lib.fold (*) 1
48 changes: 26 additions & 22 deletions examples/config-gcc/config-gcc.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@ let GccFlag =
# We only allow the following flags
let available = ["W", "c", "S", "e", "o"] in
fun label value =>
if std.is_string value then
if std.string.length value > 0
&& std.array.any (fun x => x == std.string.substring 0 1 value) available then
value
else
std.contract.blame_with_message "unknown flag %{value}" label
else if std.is_record value then
if std.record.has_field "flag" value && std.record.has_field "arg" value then
if std.array.any (fun x => x == value.flag) available then
#Normalize the tag to a string
value.flag ++ value.arg
std.typeof value
|> match {
'String =>
if std.string.length value > 0
&& std.array.any ((==) (std.string.substring 0 1 value)) available then
value
else
std.contract.blame_with_message "unknown flag %{value.flag}" label
else
std.contract.blame_with_message
"bad record structure: missing field `flag` or `arg`"
label
else
std.contract.blame_with_message "expected record or string" label
std.contract.blame_with_message "unknown flag %{value}" label,
'Record =>
if std.record.has_field "flag" value && std.record.has_field "arg" value then
if std.array.any ((==) value.flag) available then
#Normalize the tag to a string
value.flag ++ value.arg
else
std.contract.blame_with_message "unknown flag %{value.flag}" label
else
std.contract.blame_with_message
"bad record structure: missing field `flag` or `arg`"
label,
_ => std.contract.blame_with_message "expected record or string" label,
}
in

let Path =
Expand Down Expand Up @@ -87,7 +89,9 @@ let Contract = {
}
in

{
flags = ["Wextra", { flag = "o", arg = "stuff.o" }],
optimization_level = 2,
} | Contract
(
{
flags = ["Wextra", { flag = "o", arg = "stuff.o" }],
optimization_level = 2,
} | Contract
)
23 changes: 10 additions & 13 deletions examples/foreach-pattern/foreach-pattern-on-import.ncl
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
# 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
posix_users =
(import "data_users.yml").users
|> std.array.map
(
fun name =>
{
username = name,
email = "%{name}@nickel-lang.org"
}
)
}
23 changes: 10 additions & 13 deletions examples/foreach-pattern/foreach-pattern.ncl
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
# test = 'pass'
let users =
["jane", "pete", "richie"]
|> std.array.map
(
fun name =>
{
username = name,
email = "%{name}@nickel-lang.org"
}
)
in

{
usernames = users
user =
["jane", "pete", "richie"]
|> std.array.map
(
fun name =>
{
username = name,
email = "%{name}@nickel-lang.org",
}
)
}
8 changes: 5 additions & 3 deletions examples/merge-priorities/security.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
server.host.options | priority 10 = "TLS",

# force make it impossible to override this value with false
firewall.enabled | force = true,
firewall.type | default = "iptables",
firewall.open_ports | priority 5 = [21, 80, 443],
firewall = {
enabled | force = true,
type | default = "iptables",
open_ports | priority 5 = [21, 80, 443],
},
}
8 changes: 5 additions & 3 deletions examples/merge-priorities/server.ncl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# test = 'ignore'
{
server.host.ip | default = "182.168.1.1",
server.host.port | default = 80,
server.host.name | default = "hello-world.net",
server.host = {
ip | default = "182.168.1.1",
port | default = 80,
name | default = "hello-world.net",
},
}
8 changes: 5 additions & 3 deletions examples/merge/server.ncl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# test = 'ignore'
{
server.host.ip = "182.168.1.1",
server.host.port = 80,
server.host.name = "hello-world.net",
server.host = {
ip = "182.168.1.1",
port = 80,
name = "hello-world.net",
},
}
33 changes: 18 additions & 15 deletions examples/record-contract/record-contract.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,28 @@ let KubernetesConfig = {

apiVersion | String,

metadata = {
name | String,
labels.app | String,
},
metadata
| {
name | String,
labels.app | String,
},

spec = {
replicas
| std.number.PosNat
| doc "The number of replicas"
| default
= 1,
spec
| {
replicas
| std.number.PosNat
| doc "The number of replicas"
| default
= 1,

selector.matchLabels.app | String,
selector.matchLabels.app | String,

template = {
metadata.labels.app | String,
spec.containers | Array Container,
template
| {
metadata.labels.app | String,
spec.containers | Array Container,
},
},
},
}
in

Expand Down
Loading