forked from tweag/nickel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add from/to_tag_and_arg (tweag#1939)
* Add from/to_tag_and_arg This commit adds two functions to convert enums to and back from records, as a tag and an optional argument. Such functions are useful to handle enums in a general, dynamic way, while pattern matching requires to know in advance the possible tags. Additionally, we also implement a `map` function, which can be derived from the conversions above. * Update core/stdlib/std.ncl Co-authored-by: jneem <[email protected]> * Update core/stdlib/std.ncl Co-authored-by: jneem <[email protected]> * Update core/stdlib/std.ncl Co-authored-by: jneem <[email protected]> --------- Co-authored-by: jneem <[email protected]>
- Loading branch information
Showing
9 changed files
with
186 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# test.type = 'pass' | ||
let enum = std.enum in | ||
|
||
[ | ||
enum.is_enum_tag 'A, | ||
!(enum.is_enum_tag ('A 'arg)), | ||
enum.is_enum_variant ('A 'arg), | ||
!enum.is_enum_variant 'A, | ||
|
||
let enum_round_trip = fun enum_value => | ||
enum_value | ||
|> enum.to_tag_and_arg | ||
|> enum.from_tag_and_arg | ||
|> (==) enum_value | ||
in | ||
[ | ||
enum_round_trip 'Foo, | ||
enum_round_trip ('Foo 'arg), | ||
enum_round_trip ('Foo { value = "hello" }), | ||
] | ||
|> std.test.assert_all, | ||
|
||
let record_round_trip = fun data => | ||
data | ||
|> enum.from_tag_and_arg | ||
|> enum.to_tag_and_arg | ||
|> (==) data | ||
in | ||
[ | ||
record_round_trip { tag = "Foo" }, | ||
record_round_trip { tag = "Foo", arg = "arg" }, | ||
record_round_trip { tag = "Foo", arg = { value = "hello" } }, | ||
] | ||
|> std.test.assert_all, | ||
|
||
'Foo | ||
|> std.enum.map (fun _ => null) | ||
|> (==) 'Foo, | ||
|
||
'Foo 2 | ||
|> std.enum.map ((*) 2) | ||
|> (==) ('Foo 4), | ||
] | ||
|> std.test.assert_all |