-
Notifications
You must be signed in to change notification settings - Fork 5
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
Tuple module #5
Merged
Merged
Tuple module #5
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
_build |
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,82 @@ | ||
{-| Some helpers for working with 2-tuples. | ||
|
||
For larger chunks of data, it is best to switch to using records. So | ||
instead of representing a 3D point as `(3,4,5)` and wondering why there | ||
are no helper functions, represent it as `{ x = 3, y = 4, z = 5 }` and | ||
use all the built-in syntax for records. | ||
-} | ||
|
||
module tuple | ||
|
||
export first/1, second/1, map_first/2, map_second/2, tuple/2 | ||
|
||
|
||
{-| Extract the first value from a tuple. | ||
|
||
first (3, 4) == 3 | ||
first ("john", "doe") == "john" | ||
-} | ||
val first 'a 'b : fn ('a, 'b) -> 'a | ||
let first (x, _) = | ||
x | ||
|
||
test "first example 1" = | ||
assert.equal (first (3, 4)) 3 | ||
|
||
test "first example 2" = | ||
assert.equal (first ("john", "doe")) "john" | ||
|
||
|
||
{-| Extract the second value from a tuple. | ||
|
||
second (3, 4) == 4 | ||
second ("john", "doe") == "doe" | ||
-} | ||
val second 'a 'b : fn ('a, 'b) -> 'b | ||
let second (_, y) = | ||
y | ||
|
||
test "second example 1" = | ||
assert.equal (second (3, 4)) 4 | ||
|
||
test "second example 2" = | ||
assert.equal (second ("john", "doe")) "doe" | ||
|
||
|
||
{-| Transform the first value in a tuple. | ||
|
||
map_first (fn x -> x + 1) (0, 0) == (1, 0) | ||
-} | ||
val map_first 'a 'b 'aa : fn (fn 'a -> 'aa) ('a, 'b) -> ('aa, 'b) | ||
let map_first func (x, y) = | ||
(func x, y) | ||
|
||
test "map_first" = | ||
let result = map_first (fn x -> x + 1) (0, 0) in | ||
assert.equal result (1, 0) | ||
|
||
|
||
{-| Transform the second value in a tuple. | ||
|
||
map_second (fn x -> x + 1) (0, 0) == (0, 1) | ||
-} | ||
val map_second 'a 'b 'bb : fn (fn 'b -> 'bb) ('a, 'b) -> ('a, 'bb) | ||
let map_second func (x, y) = | ||
(x, func y) | ||
|
||
test "map_second" = | ||
let result = map_second (fn x -> x + 1) (0, 0) in | ||
assert.equal result (0, 1) | ||
|
||
|
||
{-| Construct a tuple from 2 values. | ||
|
||
tuple 1 2 == (1, 2) | ||
-} | ||
val tuple 'a 'b : fn 'a 'b -> ('a, 'b) | ||
let tuple x y = | ||
(x, y) | ||
|
||
test "tuple" = | ||
let result = tuple 1 2 in | ||
assert.equal result (1, 2) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiousity, I'm not familiar with the
{-|
comment syntax. Where is it from?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah! It's the syntax for a documentation comment in haskell and elm, meaning that the comment will be used when generating pretty html documentation for the module. I wrote it habitually.
Shall I continue to use this out shall I remove the pipe to make it a normal comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Short answer:
Doesn't bother me to leave it in atm but let's discuss it further under Alpaca :)
Longer answer:
I'm vaguely of the opinion that any comment preceding an AST node should be automatically attached to that node as documentation for output so I'm not sure if we need a special form (this ties into formatters and alternate ways of storing source). But! I think this is a good thing to discuss, especially here: alpaca-lang/alpaca#134