Example #1
0
def test_reduceWithAsFilter():
    bigUnsOf = lambda reducer: lambda acc, val: reducer(acc, val
                                                        ) if val > 5 else acc
    assert reduceWith(squares(arrayOf), [],
                      one2ten) == [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
    assert reduceWith(bigUnsOf(squares(arrayOf)), [],
                      one2ten) == [36, 49, 64, 81, 100]
Example #2
0
def test_reduceWithMap():
    incrementValue = map(lambda x: x + 1)
    assert reduceWith(incrementValue(arrayOf), [], [1, 2, 3]) == [2, 3, 4]
    assert reduceWith(map(lambda x: x + 1)(arrayOf), [],
                      [1, 2, 3]) == [2, 3, 4]
    assert reduceWith(map(lambda x: x + 1)(joinedWith('.')), '',
                      [1, 2, 3]) == "2.3.4"
    assert reduceWith(map(lambda x: x + 1)(sumOf), 0, [1, 2, 3]) == 9
    assert reduceWith(squares(sumOf), 0, one2ten) == 385
Example #3
0
def test_reduceWith():
    assert reduceWith(arrayOf, [], one2ten) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    def bigUns(acc, val):
        if val > 5:
            acc.append(val)
        return acc

    assert reduceWith(bigUns, [], one2ten) == [6, 7, 8, 9, 10]
    assert reduceWith(squares(bigUns), [],
                      one2ten) == [9, 16, 25, 36, 49, 64, 81, 100]
Example #4
0
def test_joinedWithReducer():
    assert reduceWith(joinedWith(', '), '', [1, 2, 3]) == "1, 2, 3"
Example #5
0
def test_efficientAccumulator():
    assert reduceWith(arrayOf, [], [1, 2, 3]) == [1, 2, 3]
Example #6
0
def test_reduceWithAsAggregator():
    assert reduceWith(filter(lambda x: x > 5)(squares(arrayOf)), [],
                      one2ten) == [36, 49, 64, 81, 100]
    assert reduceWith(filter(isOdd)(arrayOf), [], one2ten) == [1, 3, 5, 7, 9]
    assert reduceWith(filter(isOdd)(squares(sumOf)), 0, one2ten) == 165
Example #7
0
def test_reduceWithArrayPlusOperator():
    assert reduceWith(lambda acc, val: acc + [val], [], [1, 2, 3]) == [1, 2, 3]
Example #8
0
def test_reduceWithComposition():
    assert reduceWith(squaresOfTheOddNumbers(arrayOf), [],
                      one2ten) == [1, 9, 25, 49, 81]
    assert reduceWith(squaresOfTheOddNumbers(sumOf), 0, one2ten) == 165