-
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
Merged
Merged
ListCollector #3745
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
824f582
Naieve implementation
nojaf 8c64bf5
Fixed ListCollector constructor
ncave db4b061
Merge pull request #1 from ncave/test
nojaf 382b63a
Add unit test for ListCollector
nojaf a3ca9b3
Revert QuickTest.fs
nojaf 47ef3fe
Use 4 spaces for indentation
MangelMaxime f1aaedb
Cover more API in the tests for ListCollector
MangelMaxime 320819b
Update the changelog
MangelMaxime 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
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,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 |
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,53 @@ | ||
module Fable.Tests.ListCollector | ||
|
||
open Util.Testing | ||
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 ] | ||
|
||
testCase "ListCollector.Close works for empty list" <| fun () -> | ||
let mutable l = ListCollector<_>() | ||
let result = l.Close() | ||
|
||
result |> equal [] | ||
|
||
testCase "ListCollector.AddMany works" <| fun () -> | ||
let mutable l = ListCollector<_>() | ||
l.AddMany([ 1; 2; 3 ]) | ||
|
||
let result = l.Close() | ||
|
||
result |> equal [ 1; 2; 3 ] | ||
|
||
testCase "ListCollector.AddMany works for empty list" <| fun () -> | ||
let mutable l = ListCollector<_>() | ||
l.AddMany([]) | ||
|
||
let result = l.Close() | ||
|
||
result |> equal [] | ||
|
||
testCase "ListCollector.AddManyAndClose works" <| fun () -> | ||
let mutable l = ListCollector<_>() | ||
let result = l.AddManyAndClose([ 1; 2; 3 ]) | ||
|
||
result |> equal [ 1; 2; 3 ] | ||
] |
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 |
---|---|---|
|
@@ -50,6 +50,7 @@ let allTests = | |
TypeTests.tests | ||
UnionTypes.tests | ||
Uri.tests | ||
ListCollector.tests | ||
|] | ||
|
||
#if FABLE_COMPILER | ||
|
Oops, something went wrong.
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.
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.