Exemple #1
0
    def testG():
        def add(x):
            return lambda y: y + x

        data = ([10, 10])
        op = H.pipe([L.modify(0, add(1)), L.modify(1, add(-10))])
        result = op(data)
        assert (result == [11, 0])
        return result
Exemple #2
0
    def testH():
        def modifier(x):
            return x + 10

        inputs = {'a': {'b': {'c': [0, [90, 1, 2, 3, 4, 5, 6]]}}}
        expected = {'a': {'b': {'c': [0, [90, 1, 12, 13, 14, 5, 6]]}}}
        optic = ['a', 'b', 'c', 1, L.subseq_u(2, 5, L.elems)]
        result = L.modify(optic, modifier, inputs)
        assert (result == expected)
        return result
Exemple #3
0
    def testL():
        # shouldn't do anything, but should fail silently?
        def add10(x):
            return x + 10

        data = {'a': 1, 'b': [1, 2, 3]}
        expected = {'a': 1, 'b': [1, 2, 3]}
        optic = L.pick({'z': ['b', 1, L.modify_op(add10)], 'q': 'a'})
        result = L.modify(optic, add10, data)
        assert (result == expected)
        return result
Exemple #4
0
    def testK():
        # point is, using L.modify with L.pick shouldn't have any effect
        def merge_with(x):
            return dict(**x, **dict(merge_data=10))

        data = {'a': 1, 'b': [1, 2, 3]}
        expected = {'a': 1, 'b': [1, 2, 3]}
        optic = L.pick({'z': ['b', 1], 'q': 'a'})
        result = L.modify(optic, merge_with, data)
        assert (result == expected)
        return result
Exemple #5
0
def composing_plain_functions_tests():
    assert_eq(lambda: L.get(lambda x: x + 1, 2), 3)
    assert_eq(lambda: L.modify(R.inc, R.negate, 1), 1)
    assert_eq(lambda: L.get(['x', lambda x, i: [x, i]], {'x': -1}), [-1, 'x'])
    assert_eq(lambda: L.collect([L.elems, lambda x, i: [x, i]], ['x', 'y']),
              [['x', 0], ['y', 1]])
    assert_eq(
        lambda: L.collect([L.values, lambda x, i: [x, i]], {
            'x': 1,
            'y': -1
        }), [[1, 'x'], [-1, 'y']])
    assert_eq(lambda: L.get([0, lambda x, i: [x, i]], [-1]), [-1, 0])
    assert_eq(lambda: L.get([0, 'x', R.negate], [{'x': -1}]), 1)
    assert_eq(lambda: L.set([0, 'x', R.negate], 2, [{'x': -1}]), [{'x': -1}])
    assert_eq(lambda: L.get(I.always('always'), 'anything'), 'always')
    assert_eq(lambda: L.set(I.always('always'), 'anything', 'original'),
              'original')
Exemple #6
0
    def testE():
        def add10(x):
            return x + 10

        expected = {
            'a': 1,
            'b': {
                'b': {
                    'b': {
                        'b': {
                            'b': {
                                'b': [11, 12, 13]
                            }
                        }
                    }
                }
            }
        }
        data = dict(a=1, b=dict(b=dict(b=dict(b=dict(b=dict(b=[1, 2, 3]))))))
        optic = ['b', 'b', 'b', 'b', 'b', 'b', L.elems]
        result = L.modify(optic, add10, data)
        assert (result == expected)
        return result
Exemple #7
0
 def testN():
     data = [{
         'Description': 'A',
         'Amount': 10,
         'Date': '2018/01/01'
     }, {
         'Description': 'B',
         'Amount': 20,
         'Date': '2018/01/02'
     }]
     expected = [{
         'description': 'A',
         'amount': 10,
         'date': '2018/01/01!'
     }, {
         'description': 'B',
         'amount': 20,
         'date': '2018/01/02!'
     }]
     operation = L.collect([
         L.elems,
         L.get(
             L.pick({
                 'description': 'Description',
                 'amount': 'Amount',
                 'date': 'Date',
             })),
         L.modify('date', lambda x: x + '!'),
         #     L.set('account',
         #           'checking',
         #     })),
     ])
     result = operation(data)
     #print('result', result)
     assert (result == expected)
     return result
Exemple #8
0
def values_tests():
    result = L.modify(L.values, R.identity, {'a': 1, 'b': 2})
    print('\n\n\n here!!!!!', result, '\n\n\n')
    result = L.modify(L.values, lambda x, y: x, {'x': 11, 'y': 22})
    print(result)
Exemple #9
0
def elems_tests():
    run_test(lambda: L.modify(L.elems, R.identity, [0, -1]), [0, -1])
    assert_eq(lambda: L.modify(L.elems, R.identity, {
        'x': 1,
        'y': 2
    }), {
        'x': 1,
        'y': 2
    })
    assert_eq(lambda: L.modify(L.elems, R.inc, {
        'x': 1,
        'y': 2
    }), {
        'x': 1,
        'y': 2
    })
    assert_eq(lambda: L.modify(L.elems, R.negate, []), [])
    #assert_eq(lambda: L.remove(L.elems, [1]), [])
    assert_eq(
        lambda: L.modify(['xs', L.elems, 'x', L.elems], R.add(1),
                         {'xs': [{
                             'x': [1]
                         }, {
                             'x': [2, 3, 4]
                         }]}), {'xs': [{
                             'x': [2]
                         }, {
                             'x': [3, 4, 5]
                         }]})
    assert_eq(
        lambda: L.set(['xs', L.elems, 'x', L.elems], 101,
                      {'xs': [{
                          'x': [1]
                      }, {
                          'x': [2, 3, 4]
                      }]}), {'xs': [{
                          'x': [101]
                      }, {
                          'x': [101, 101, 101]
                      }]})
    # assert_eq(
    #     lambda:
    #     L.remove(['xs', L.elems, 'x', L.elems], {
    #         'ys': 'hip',
    #         'xs': [{'x': [1]}, {'x': [2, 3, 4]}]
    #     }),
    #     {'ys': 'hip', 'xs': [{'x': []}, {'x': []}]}
    # )
    assert_eq(
        lambda: L.modify(
            ['xs', L.elems, 'x'], lambda x: (None if x < 2 else x), {
                'xs': [{
                    'x': 3
                }, {
                    'x': 1
                }, {
                    'x': 4
                }, {
                    'x': 1,
                    'y': 0
                }, {
                    'x': 5
                }, {
                    'x': 9
                }, {
                    'x': 2
                }]
            }), {
                'xs': [{
                    'x': 3
                }, {}, {
                    'x': 4
                }, {
                    'y': 0
                }, {
                    'x': 5
                }, {
                    'x': 9
                }, {
                    'x': 2
                }]
            })
    assert_eq(
        lambda: L.modify([L.elems, ['x', L.elems]], R.add(1), [{
            'x': [1]
        }, {}, {
            'x': []
        }, {
            'x': [2, 3]
        }]), [{
            'x': [2]
        }, {}, {
            'x': []
        }, {
            'x': [3, 4]
        }])
    assert_eq(
        lambda: L.modify([[L.elems, 'x'], L.elems], R.add(1), [{
            'x': [1]
        }, {
            'y': 'keep'
        }, {
            'x': [],
            'z': 'these'
        }, {
            'x': [2, 3]
        }]), [{
            'x': [2]
        }, {
            'y': 'keep'
        }, {
            'x': [],
            'z': 'these'
        }, {
            'x': [3, 4]
        }])
Exemple #10
0
            L.find(
                R.pipe(
                    abs,
                    R.equals(2)
                ),
                {'hint': 2}
            ),
            [-1, -2, 3, 1, 2, 1]
        ),
        -2
    ),
    (lambda:  L.get([], [[{'x': {'y': 101}}]]), [[{'x': {'y': 101}}]]),
    (lambda: L.set([0], None, [None]), []),
    (lambda:  L.set(1, '2', ['1', '2', '3']), ['1', '2', '3']),
    # fails b/c unary modifier
    (lambda: L.modify('x', lambda x: x + 1, {'x': 1}), {'x': 2}),
]


def rewrite_tests():
    assert_eq(lambda: L.get(L.rewrite(lambda x: x - 1), 1), 1)
    assert_eq(lambda: L.get(L.rewrite(lambda x: x - 1), None), None)
    assert_eq(lambda: L.set(L.rewrite(lambda x: x - 1), None, 1), None)
    assert_eq(lambda: L.set(L.rewrite(lambda x: x - 1), 3, 1), 2)


def reread_tests():
    assert_eq(lambda: L.get(L.reread(lambda x: x - 1), 1), 0)
    assert_eq(lambda: L.get(L.reread(lambda x: x - 1), None), None)
    assert_eq(lambda: L.set(L.reread(lambda x: x - 1), None, 1), None)
    assert_eq(lambda: L.set(L.reread(lambda x: x - 1), 3, 1), 3)