-
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.
- Loading branch information
Showing
6 changed files
with
314 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace Ghul is | ||
class BOX[T] is | ||
value: T public; | ||
|
||
init() is | ||
si | ||
|
||
init(value: T) is | ||
self.value = value; | ||
si | ||
|
||
to_string() -> string => "{value}"; | ||
si | ||
si |
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,42 @@ | ||
namespace Ghul.Pipes is | ||
use Collections; | ||
|
||
class FLAT_MAP_PIPE[TFrom,TTo]: Pipe[TTo] is | ||
current: TTo; | ||
|
||
_iterator: Iterator[TFrom]; | ||
_flat_iterator: Iterator[TTo]; | ||
|
||
_mapper: (TFrom) -> Iterable[TTo]; | ||
|
||
init(iterator: Iterator[TFrom], mapper: (TFrom) -> Iterable[TTo]) is | ||
_iterator = iterator; | ||
_mapper = mapper; | ||
si | ||
|
||
move_next() -> bool is | ||
do | ||
if _flat_iterator? /\ _flat_iterator.move_next() then | ||
current = _flat_iterator.current; | ||
|
||
return true; | ||
fi | ||
|
||
if !_iterator.move_next() then | ||
reset(); | ||
|
||
return false; | ||
fi | ||
|
||
let flat_iterable = _mapper(_iterator.current); | ||
|
||
_flat_iterator = flat_iterable.iterator; | ||
od | ||
si | ||
|
||
reset() is | ||
_iterator.reset(); | ||
_flat_iterator = null; | ||
si | ||
si | ||
si |
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,34 @@ | ||
namespace Ghul.Pipes is | ||
use Collections; | ||
|
||
class LIST_REVERSE_PIPE[T]: Pipe[T] is | ||
_list: List[T]; | ||
_index: int; | ||
|
||
current: T => _list[_index]; | ||
|
||
init(list: List[T]) is | ||
super.init(list); | ||
|
||
_list = list; | ||
reset(); | ||
si | ||
|
||
move_next() -> bool is | ||
let result = _index > 0; | ||
|
||
if result then | ||
_index = _index - 1; | ||
fi | ||
|
||
return result; | ||
si | ||
|
||
reset() is | ||
_index = _list.count; | ||
si | ||
|
||
dispose() is | ||
si | ||
si | ||
si |
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,136 @@ | ||
namespace Tests is | ||
|
||
use Collections; | ||
use NSubstitute; | ||
|
||
use NSubstitute.SubstituteExtensions.returns; | ||
use NSubstitute.SubstituteExtensions.received; | ||
|
||
use Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
use Ghul.Pipes; | ||
|
||
class FlatMapPipeShould is | ||
@test() | ||
|
||
init() is | ||
si | ||
|
||
FlatMap_EmptyArray_ReturnsEmptySequence() is | ||
@test() | ||
|
||
let pipe = | ||
Ghul.Pipes.Pipe`[int] | ||
.from(empty`[int]()); | ||
|
||
let result = | ||
pipe | ||
.flat_map(i => i.to_string()); | ||
|
||
assert_are_equal(empty`[char](), result); | ||
si | ||
|
||
FlatMap_IntsToStrings_ReturnsExpectedSequence() is | ||
@test() | ||
|
||
let pipe = | ||
Ghul.Pipes.Pipe`[int] | ||
.from([3, 4, 1, 5, 2, 3, 1, 4, 5]); | ||
|
||
let result = | ||
pipe | ||
.flat_map(i => i.to_string()); | ||
|
||
assert_are_equal(['3', '4', '1', '5', '2', '3', '1', '4', '5'], result); | ||
si | ||
|
||
FlatMap_ArraysIdentity_ReturnsConcatArrays() is | ||
@test() | ||
|
||
let pipe = | ||
Ghul.Pipes.Pipe[int[]] | ||
.from([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); | ||
|
||
let result = | ||
pipe | ||
.flat_map(a => a); | ||
|
||
assert_are_equal([1, 2, 3, 4, 5, 6, 7, 8, 9], result); | ||
si | ||
|
||
FlatMap_ResetBeforeIteration_HasNoEffect() is | ||
@test() | ||
|
||
let pipe = | ||
Ghul.Pipes.Pipe[int[]] | ||
.from([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); | ||
|
||
let result = | ||
pipe | ||
.flat_map(a => a); | ||
|
||
result.reset(); | ||
|
||
assert_are_equal([1, 2, 3, 4, 5, 6, 7, 8, 9], result); | ||
si | ||
|
||
FlatMap_ResetAfterIncompleteIteration_StartsAgainFromBeginning() is | ||
@test() | ||
|
||
let pipe = | ||
Ghul.Pipes.Pipe[int[]] | ||
.from([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); | ||
|
||
let result = | ||
pipe | ||
.flat_map(a => a); | ||
|
||
result.move_next(); | ||
result.move_next(); | ||
result.move_next(); | ||
result.move_next(); | ||
|
||
result.reset(); | ||
|
||
assert_are_equal([1, 2, 3, 4, 5, 6, 7, 8, 9], result); | ||
si | ||
|
||
FlatMap_CompleteIteration_ResetsPipe() is | ||
@test() | ||
|
||
let pipe = | ||
Ghul.Pipes.Pipe[int[]] | ||
.from([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); | ||
|
||
let result = | ||
pipe | ||
.flat_map(a => a); | ||
|
||
// count consumes the whole pipe and then resets it: | ||
let count = result.count(); | ||
|
||
assert_are_equal([1, 2, 3, 4, 5, 6, 7, 8, 9], result); | ||
si | ||
|
||
|
||
FlatMap_ResetAfterIteration_HasNoEffect() is | ||
@test() | ||
|
||
let pipe = | ||
Ghul.Pipes.Pipe[int[]] | ||
.from([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); | ||
|
||
let result = | ||
pipe | ||
.flat_map(a => a); | ||
|
||
// count consumes the whole pipe and then resets it: | ||
let count = result.count(); | ||
|
||
// this reset is a no-op: | ||
result.reset(); | ||
|
||
assert_are_equal([1, 2, 3, 4, 5, 6, 7, 8, 9], result); | ||
si | ||
si | ||
si |
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