-
Notifications
You must be signed in to change notification settings - Fork 323
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
Implement Vector.build
: implementation and some tests
#9725
Changes from all commits
c07f260
4bd580e
4194d15
7b4128a
e28c006
4de1bbc
ae3110a
71219a2
36a7023
7cf7649
81d8556
8b7b1f3
ec44244
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -420,27 +420,25 @@ type_spec suite_builder name alter = suite_builder.group name group_builder-> | |
|
||
group_builder.specify "should allow applying a function to each element" <| | ||
vec = alter [1, 2, 3, 4] | ||
vec_mut = Vector.new_builder | ||
vec.each vec_mut.append | ||
vec_mut.to_vector . should_equal vec | ||
result = Vector.build builder-> | ||
vec.each builder.append | ||
result . should_equal vec | ||
|
||
group_builder.specify "should accept changed elements" <| | ||
vec_mut = Vector.new_builder | ||
vec_mut.append 1 | ||
vec_mut.append 1.1 | ||
vec_mut.append Nothing | ||
vec = alter <| Vector.build builder-> | ||
builder.append 1 | ||
builder.append 1.1 | ||
builder.append Nothing | ||
|
||
vec = alter vec_mut.to_vector | ||
vec.length . should_equal 3 | ||
vec.at 0 . should_equal 1 | ||
vec.at 1 . should_equal 1.1 | ||
vec.at 2 . should_equal Nothing | ||
|
||
group_builder.specify "should accept Nothing" <| | ||
vec_mut = Vector.new_builder | ||
vec_mut.append Nothing | ||
vec = alter <| Vector.build builder-> | ||
builder.append Nothing | ||
|
||
vec = alter vec_mut.to_vector | ||
vec.length . should_equal 1 | ||
vec.at 0 . should_equal Nothing | ||
|
||
|
@@ -778,21 +776,19 @@ type_spec suite_builder name alter = suite_builder.group name group_builder-> | |
|
||
group_builder.specify "should correctly propagate state through each" <| | ||
v = State.run Number 77 <| | ||
b = Vector.new_builder | ||
(alter ["A", "B"]).each x-> | ||
b.append x | ||
b.append (State.get Number).to_text | ||
b.to_vector | ||
Vector.build builder-> | ||
(alter ["A", "B"]).each x-> | ||
builder.append x | ||
builder.append (State.get Number).to_text | ||
|
||
v.should_equal ['A', '77', 'B', '77'] | ||
|
||
group_builder.specify "should correctly propagate state through map" <| | ||
v = State.run Number 55 <| | ||
b = Vector.new_builder | ||
(alter ["X", "Y"]).map x-> | ||
b.append x | ||
b.append (State.get Number).to_text | ||
b.to_vector | ||
Vector.build builder-> | ||
(alter ["X", "Y"]).map x-> | ||
builder.append x | ||
builder.append (State.get Number).to_text | ||
|
||
v.should_equal ['X', '55', 'Y', '55'] | ||
|
||
|
@@ -1034,6 +1030,30 @@ add_specs suite_builder = | |
v = Vector.collect l .x .xs limit=30 stop_at=(_==List.Nil) | ||
v . should_equal [1, 2, 3] | ||
|
||
suite_builder.group "Vector.build" group_builder-> | ||
group_builder.specify "Should be able to build a vector" <| | ||
v = Vector.build builder-> | ||
builder.append 1 | ||
builder.append 2 | ||
builder.append Nothing | ||
builder.append 3 | ||
v . should_equal [1, 2, Nothing, 3] | ||
|
||
v2 = Vector.build builder-> | ||
_ = builder # Do nothing | ||
v2 . should_equal [] | ||
|
||
v3 = Vector.build initial_capacity=20 builder-> | ||
builder.append 1 | ||
v3 . should_equal [1] | ||
|
||
group_builder.specify "Should propagate dataflow errors" <| | ||
v = Vector.build builder-> | ||
builder.append 1 | ||
builder.append 2 | ||
Error.throw (Illegal_Argument.Error "asdf") | ||
v . should_fail_with (Illegal_Argument.Error "asdf") | ||
Comment on lines
+1050
to
+1055
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a great test. Not directly related, but while we are at it we could consider solving a long outstanding problem we used to have:
Unfortunately, as far as I know currently this will actually not fail but produce a vector We used to have a pattern of 'folding' the builder i.e.
in which case the error would be correctly propagated. Unfortunately, the 'folding the builder' pattern did not get much traction. In many places it was just not too practical - the builder was inherently mutable object so this more 'functional' folding was often not fitting its usages. Do you think we could amend the builder to detect that This could be implemented by holding a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But this is a bit out of scope of this PR, so I guess we can also do it as a separate ticket/PR instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very good idea. #9733 |
||
|
||
suite_builder.group "Vector/Array equality" group_builder-> | ||
v1 = [1, 2, 3] | ||
a1 = v1.to_array | ||
|
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.
I'd assume this is a general even for complicated computations. Just wrap your computation in
Vector.build builder->
block and that's it.