-
Notifications
You must be signed in to change notification settings - Fork 301
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
ListCollector #3745
ListCollector #3745
Changes from 5 commits
824f582
8c64bf5
db4b061
382b63a
a3ca9b3
47ef3fe
f1aaedb
320819b
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Microsoft.FSharp.Core.CompilerServices | ||
|
||
[<NoEquality; NoComparison>] | ||
type ListCollector<'T>() = | ||
let collector = ResizeArray<'T>() | ||
|
||
member this.Add(value: 'T) = collector.Add(value) | ||
|
||
member this.AddMany(values: seq<'T>) = collector.AddRange(values) | ||
|
||
// In the particular case of closing with a final add of an F# list | ||
// we can simply stitch the list into the end of the resulting list | ||
member this.AddManyAndClose(values: seq<'T>) = | ||
collector.AddRange(values) | ||
Seq.toList collector | ||
|
||
member this.Close() = Seq.toList collector |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Fable.Tests.ListCollector | ||
|
||
open Util.Testing | ||
open Fable.Tests.Util | ||
open Microsoft.FSharp.Core.CompilerServices | ||
|
||
let cutOffLast list = | ||
let mutable headList = ListCollector<'a>() | ||
|
||
let rec visit list = | ||
match list with | ||
| [] | ||
| [ _ ] -> () | ||
| head :: tail -> | ||
headList.Add(head) | ||
visit tail | ||
|
||
visit list | ||
headList.Close() | ||
|
||
let tests = | ||
testList "ListCollector" [ | ||
testCase "ListCollector.Add and .Close" <| fun () -> | ||
let result = cutOffLast [ 1; 2; 3 ] | ||
result |> equal [ 1; 2 ] | ||
] | ||
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. Can we add tests for each 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. Let's not overextend. I'm including only the parts that interest me, as this is largely experimental. The code is already solid—something we're both aware of—and this pull request improves its current state. 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. I don't agree yes for this module it is probably enough, but it happens several times that doing so allowed us to catch behaviour we were not aware of. There are still some APIs in fable-library that don't behave correctly because they didn't have a test associated for every usage. Having tests also allows us to catch on regressions. I added the tests to cover the most common cases. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ let allTests = | |
TypeTests.tests | ||
UnionTypes.tests | ||
Uri.tests | ||
ListCollector.tests | ||
|] | ||
|
||
#if FABLE_COMPILER | ||
|
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.
Looking at the code it seems like we can remove the
mutable
hereThere 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.
That would not reflect the actual usage of shared code. In regular F# the collector needs to be mutable.
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.
Didn't know about that.