-
-
Notifications
You must be signed in to change notification settings - Fork 30
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
runningFold method alongside tests #189
Changes from all commits
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 |
---|---|---|
|
@@ -56,6 +56,17 @@ extension KtCollectionExtensions<T> on KtCollection<T> { | |
return sum as R; | ||
} | ||
|
||
/// Returns a list containing the results of applying the given [operation] | ||
/// function to each element in the original collection and the previous | ||
/// accumulator value. | ||
KtCollection<R> runningFold<R>(R initial, R Function(R, T) operation) { | ||
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. Create extension for |
||
var accumulator = initial; | ||
return KtMutableList<R>.from(iter | ||
.map((element) => accumulator = operation(accumulator, element)) | ||
.toList()) | ||
..addAt(0, initial); | ||
} | ||
|
||
/// Returns a [KtMutableList] filled with all elements of this collection. | ||
KtMutableList<T> toMutableList() => KtMutableList<T>.from(iter); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,6 +205,18 @@ void testCollection(KtCollection<T> Function<T>() emptyCollection, | |
}); | ||
}); | ||
|
||
group("runningFold", () { | ||
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. Don't forget the |
||
test("running folds a string list", () { | ||
final strings = listOf<String>("a", "b", "c", "d"); | ||
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. Add a test where the list is empty |
||
final runningFolded = | ||
strings.runningFold<String>("s", (acc, string) => acc + string); | ||
expect( | ||
runningFolded, | ||
KtMutableList.of("s", "sa", "sab", "sabc", "sabcd"), | ||
); | ||
}); | ||
}); | ||
|
||
group("toString", () { | ||
if (ordered) { | ||
test("default string representation", () { | ||
|
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.
Change return type to
KtList