Skip to content

yyjjl/lambda.py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

New lambda style in Python

Boost style

from fn import _1, _2, L, wrap, List, Dict, Tuple

map(L(_1 + _2), [1, 2, 3], [3, 4, 5])
# call method
map(L(_1.strip()), ['a ', ' b', ' c '])
# subscript
map(L(_1[_2]), ['a', 'ab', 'abc'], [0, 1, 2])

# wrap a builtin function to allow recursive definition
map = wrap(map)

double = L(_1 * 2)
square = L(_1**2)
add_one = L(_1 + 1)

func = L(map(L(_1 * _2), _1, [1, 4, 3]))

list(func([4, 5, 6]))  # => [4, 20, 18]

func = L(double(double(double(_1))))
func(10)  # => 80
func = L(double(square(add_one(_1))))
func(10)  # => 242

# Dict, List and Tuple
func = L(List([_1, [2, [_3, _2], _1], _2]))
func(10, 11, 12)  # => [10, [2, [12, 11], 10], 11]

map(L(func(_1, _1, _2)), [1, 2], ['a', 'b'])
# => [[1, [2, ['a', 1], 1], 1], [2, [2, ['b', 2], 2], 2]]

func = L(Dict({'a': _1, 'b': (_2, _1, 'c'), 'd': [_1, _2]}))
func(11, 12, 13)  # => {'a': 11, 'b': (12, 11, 'c'), 'd': [11, 12]}

# _1 while be exposed to upper Lambda, and _2 shift as _1
# L[1](_1 * _2) is a function generator
func = L(map(L[1](_1 * _2), [1, 2, 3]))
func(0)  # => [0, 0, 0]
func(2)  # => [2, 4, 6]

TODO: add closure

About

A boost style lambda in python3

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages