Skip to content

Commit ed9bdf2

Browse files
committed
Use let-blocks when apprioriate in stdlib & examples
1 parent fd7736a commit ed9bdf2

File tree

10 files changed

+129
-118
lines changed

10 files changed

+129
-118
lines changed

core/stdlib/std.ncl

Lines changed: 68 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@
533533
"%
534534
= fun pred l =>
535535
let aux = fun acc x =>
536-
if (pred x) then
536+
if pred x then
537537
{ right = acc.right @ [x], wrong = acc.wrong }
538538
else
539539
{ right = acc.right, wrong = acc.wrong @ [x] }
@@ -654,8 +654,10 @@
654654
if length <= 1 then
655655
array
656656
else
657-
let first = %array/at% array 0 in
658-
let rest = %array/slice% 1 length array in
657+
let
658+
first = %array/at% array 0,
659+
rest = %array/slice% 1 length array,
660+
in
659661
[first] @ (flat_map (fun a => [v, a]) rest),
660662

661663
slice
@@ -866,8 +868,10 @@
866868
"%
867869
= fun f array =>
868870
let last_index = %array/length% array - 1 in
869-
let last = %array/at% array last_index in
870-
let rest = %array/slice% 0 last_index array in
871+
let
872+
last = %array/at% array last_index,
873+
rest = %array/slice% 0 last_index array,
874+
in
871875
fold_right f last rest,
872876

873877
zip_with
@@ -1850,8 +1854,8 @@
18501854
let label_module = label in
18511855
let attach_message = label_module.with_message "invalid array indexing" in
18521856

1853-
let ArrayIndexFirst =
1854-
%contract/custom% (fun label value =>
1857+
let
1858+
ArrayIndexFirst = %contract/custom% (fun label value =>
18551859
if %typeof% value == 'Number then
18561860
let label =
18571861
label
@@ -1861,10 +1865,9 @@
18611865
std.contract.check std.number.Nat label value
18621866
else
18631867
'Ok value
1864-
)
1865-
in
1868+
),
18661869

1867-
let ArrayIndexSecond = fun type min_size =>
1870+
ArrayIndexSecond = fun type min_size =>
18681871
%contract/custom% (fun _label value =>
18691872
if %typeof% min_size == 'Number && %typeof% value == 'Array then
18701873
let max_idx =
@@ -1893,7 +1896,7 @@
18931896
'Ok value
18941897
else
18951898
'Ok value
1896-
)
1899+
),
18971900
in
18981901

18991902
fun type =>
@@ -1923,59 +1926,58 @@
19231926
let label_module = label in
19241927
let attach_message = label_module.with_message "invalid array slice indexing" in
19251928

1926-
let SliceIndexFirst =
1927-
%contract/custom% (fun label value =>
1928-
if %typeof% value == 'Number then
1929-
let label =
1930-
label
1931-
|> attach_message
1932-
|> label_module.append_note "Expected the array slice start index to be a positive integer, got %{%to_string% value}"
1933-
in
1934-
std.contract.check std.number.Nat label value
1935-
else
1936-
'Ok value
1937-
)
1938-
in
1929+
let
1930+
SliceIndexFirst =
1931+
%contract/custom% (fun label value =>
1932+
if %typeof% value == 'Number then
1933+
let label =
1934+
label
1935+
|> attach_message
1936+
|> label_module.append_note "Expected the array slice start index to be a positive integer, got %{%to_string% value}"
1937+
in
1938+
std.contract.check std.number.Nat label value
1939+
else
1940+
'Ok value
1941+
),
1942+
1943+
SliceIndexSecond = fun start =>
1944+
%contract/custom% (fun label value =>
1945+
if %typeof% start == 'Number && %typeof% value == 'Number then
1946+
if value < start then
1947+
'Error {
1948+
message = "invalid array slice indexing",
1949+
notes = [
1950+
"Expected the array slice indices to satisfy `start <= end`, but got %{%to_string% start} (start) and %{%to_string% value} (end)"
1951+
],
1952+
}
1953+
else
1954+
let label =
1955+
label
1956+
|> attach_message
1957+
|> label_module.append_note "Expected the array slice end index to be a positive integer, got %{%to_string% value}"
1958+
in
1959+
std.contract.check std.number.Nat label value
1960+
else
1961+
'Ok value
1962+
),
1963+
1964+
ArraySliceArray = fun end_index =>
1965+
%contract/custom% (fun _label value =>
1966+
if %typeof% end_index == 'Number
1967+
&& %typeof% value == 'Array
1968+
&& end_index > %array/length% value then
1969+
let index_as_str = %to_string% end_index in
1970+
let size_as_str = %to_string% (%array/length% value) in
19391971

1940-
let SliceIndexSecond = fun start =>
1941-
%contract/custom% (fun label value =>
1942-
if %typeof% start == 'Number && %typeof% value == 'Number then
1943-
if value < start then
19441972
'Error {
19451973
message = "invalid array slice indexing",
19461974
notes = [
1947-
"Expected the array slice indices to satisfy `start <= end`, but got %{%to_string% start} (start) and %{%to_string% value} (end)"
1975+
"Expected the slice end index to be between 0 and %{size_as_str} (array's length), got %{index_as_str}"
19481976
],
19491977
}
19501978
else
1951-
let label =
1952-
label
1953-
|> attach_message
1954-
|> label_module.append_note "Expected the array slice end index to be a positive integer, got %{%to_string% value}"
1955-
in
1956-
std.contract.check std.number.Nat label value
1957-
else
1958-
'Ok value
1959-
)
1960-
in
1961-
1962-
let ArraySliceArray = fun end_index =>
1963-
%contract/custom% (fun _label value =>
1964-
if %typeof% end_index == 'Number
1965-
&& %typeof% value == 'Array
1966-
&& end_index > %array/length% value then
1967-
let index_as_str = %to_string% end_index in
1968-
let size_as_str = %to_string% (%array/length% value) in
1969-
1970-
'Error {
1971-
message = "invalid array slice indexing",
1972-
notes = [
1973-
"Expected the slice end index to be between 0 and %{size_as_str} (array's length), got %{index_as_str}"
1974-
],
1975-
}
1976-
else
1977-
'Ok value
1978-
)
1979+
'Ok value
1980+
),
19791981
in
19801982

19811983
DependentFun
@@ -2252,8 +2254,10 @@
22522254
"%
22532255
= fun f enum_value =>
22542256
if %enum/is_variant% enum_value then
2255-
let tag = (%to_string% (%enum/get_tag% enum_value)) in
2256-
let mapped = f (%enum/get_arg% enum_value) in
2257+
let
2258+
tag = (%to_string% (%enum/get_tag% enum_value)),
2259+
mapped = f (%enum/get_arg% enum_value),
2260+
in
22572261

22582262
%enum/make_variant% tag mapped
22592263
else
@@ -3505,10 +3509,11 @@
35053509
if length == 0 then
35063510
""
35073511
else
3508-
let first = %array/at% fragments 0 in
3509-
let rest =
3510-
%array/slice% 1 length fragments
3511-
|> std.array.fold_left (fun acc s => acc ++ sep ++ s) ""
3512+
let
3513+
first = %array/at% fragments 0,
3514+
rest =
3515+
%array/slice% 1 length fragments
3516+
|> std.array.fold_left (fun acc s => acc ++ sep ++ s) "",
35123517
in
35133518
first ++ rest,
35143519

examples/arrays/arrays.ncl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Example array functions. This code is illustrative: prefer using the array
44
# stdlib functions `std.array.map` and `std.array.fold_right` instead.
5-
let my_array_lib = {
5+
let rec
66
map : forall a b. (a -> b) -> Array a -> Array b
77
= fun f arr =>
88
if arr == [] then
@@ -18,9 +18,8 @@ let my_array_lib = {
1818
else
1919
let [head, ..tail] = arr in
2020
f head (fold f first tail),
21-
}
2221
in
2322
# Compute `7!`
2423
[1, 2, 3, 4, 5, 6]
25-
|> my_array_lib.map ((+) 1)
26-
|> my_array_lib.fold (*) 1
24+
|> map ((+) 1)
25+
|> fold (*) 1

examples/config-gcc/config-gcc.ncl

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
# Validate and normalize gcc flags. They can be either a string `-Wextra` or
44
# a structured value `{flag = "W", arg = "extra"}`. Arguments are not checked.
5-
let GccFlag =
5+
let
6+
7+
GccFlag =
68
let supported_flags = ["W", "c", "S", "e", "o"] in
79
let is_valid_flag
810
| doc "check if a string of length > 0 is a valid flag"
@@ -26,10 +28,9 @@ let GccFlag =
2628
},
2729
_ => 'Error { message = "expected record or string" },
2830
}
29-
)
30-
in
31+
),
3132

32-
let Path =
33+
Path =
3334
let pattern = m%"^(.+)/([^/]+)$"% in
3435
std.contract.from_validator (fun value =>
3536
if std.is_string value then
@@ -39,10 +40,9 @@ let Path =
3940
'Error { message = "invalid path" }
4041
else
4142
'Error { message = "not a string" }
42-
)
43-
in
43+
),
4444

45-
let SharedObjectFile =
45+
SharedObjectFile =
4646
std.contract.from_validator (fun value =>
4747
if std.is_string value then
4848
if std.string.is_match m%"\.so$"% value then
@@ -51,18 +51,17 @@ let SharedObjectFile =
5151
'Error { message = "not an .so file" }
5252
else
5353
'Error { message = "not a string" }
54-
)
55-
in
54+
),
5655

57-
let OptLevel =
56+
OptLevel =
5857
std.contract.from_predicate (match {
5958
0 or 1 or 2 => true,
6059
_ => false,
6160
}
62-
)
61+
),
6362
in
6463

65-
let Contract = {
64+
let GccConf = {
6665
path_libc
6766
| doc "Path to libc."
6867
| Path
@@ -93,9 +92,7 @@ let Contract = {
9392
}
9493
in
9594

96-
(
9795
{
9896
flags = ["Wextra", { flag = "o", arg = "stuff.o" }],
9997
optimization_level = 2,
100-
} | Contract
101-
)
98+
} | GccConf

examples/fibonacci/fibonacci.ncl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ let rec fibonacci = match {
88
n => fibonacci (n - 1) + fibonacci (n - 2),
99
}
1010
in
11+
1112
fibonacci 10

examples/imports/data_nickel_properties.ncl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# test = 'pass'
2-
let kelvin_to_celcius = fun kelvin => kelvin - 273.15 in
3-
let kelvin_to_fahrenheit = fun kelvin => (kelvin - 273.15) * 1.8000 + 32.00 in
2+
let
3+
kelvin_to_celcius = fun kelvin => kelvin - 273.15,
4+
kelvin_to_fahrenheit = fun kelvin => (kelvin - 273.15) * 1.8000 + 32.00,
5+
in
46

5-
let melting_celcius = std.string.from_number (kelvin_to_celcius 1728) in
6-
let melting_fahrenheit = std.string.from_number (kelvin_to_fahrenheit 1728) in
7-
8-
let boiling_celcius = std.string.from_number (kelvin_to_celcius 3003) in
9-
let boiling_fahrenheit = std.string.from_number (kelvin_to_fahrenheit 3003) in
7+
let
8+
melting_celcius = std.string.from_number (kelvin_to_celcius 1728),
9+
melting_fahrenheit = std.string.from_number (kelvin_to_fahrenheit 1728),
10+
boiling_celcius = std.string.from_number (kelvin_to_celcius 3003),
11+
boiling_fahrenheit = std.string.from_number (kelvin_to_fahrenheit 3003),
12+
in
1013

1114
{
1215
physical = {

examples/imports/imports.ncl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
# test = 'ignore'
22

33
# Nickel can import plain yaml, or json files
4-
let _users = (import "data_users.yml") in
5-
let _groups = (import "data_groups.json") in
6-
7-
# It even imports toml
8-
let _machines = (import "data_machines.toml") in
9-
10-
# And of course other nickel files
11-
let _nickel_properties = (import "data_nickel_properties.ncl") in
4+
let
5+
data_users = (import "data_users.yml"),
6+
data_groups = (import "data_groups.json"),
7+
# or even toml
8+
data_machines = (import "data_machines.toml"),
9+
# And of course other nickel files
10+
data_nickel_properties = (import "data_nickel_properties.ncl"),
11+
in
1212

1313
# This is the output object
1414
{
15-
users = _users.users,
16-
groups = _groups.groups,
17-
machines = _machines.machines,
15+
users = data_users.users,
16+
groups = data_groups.groups,
17+
machines = data_machines.machines,
1818
off_topic = {
19-
nickel_properties = _nickel_properties
19+
nickel_properties = data_nickel_properties,
2020
}
2121
}

examples/merge-priorities/main.ncl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
# Merge several blocks into one final configuration. In a real world case, one
44
# would also want contracts to validate the shape of the data.
5-
let server = import "server.ncl" in
6-
let security = import "security.ncl" in
7-
# Disabling firewall in the final result
5+
let
6+
server = import "server.ncl",
7+
security = import "security.ncl",
8+
in
89
server
910
& security
1011
& {
11-
# As opposed to the simple merge example, this would now fail
12+
# As opposed to the simple merge example, uncommenting the next line would now
13+
# fail
1214
# firewall.enabled = false
1315

1416
firewall.open_ports | priority 10 = [80],

examples/merge/main.ncl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
# Merge several blocks into one final configuration. In a real world case, one
44
# would also want contracts to validate the shape of the data.
5-
let server = import "server.ncl" in
6-
let security = import "security.ncl" in
5+
let
6+
server = import "server.ncl",
7+
security = import "security.ncl",
8+
in
79
# Disabling firewall in the final result
810
server & security & { firewall.enabled = false }

0 commit comments

Comments
 (0)