forked from fable-compiler/Fable
-
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.
Merge pull request fable-compiler#3745 from nojaf/list-collector
ListCollector
- Loading branch information
Showing
8 changed files
with
84 additions
and
1 deletion.
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 | ||
|