Giter Site home page Giter Site logo

py-functional-chaining's Introduction

py-functional-chaining

intro

你是否会对这种代码产生生理不适?

universalSet = set(map(lambda cell: str.strip(cell.value), filter(lambda cell: cell.value != None, list(sheet.columns)[0])))

他本可以更优雅:

universalSet = List(sheet.columns)[0] \
      .filter(lambda cell: cell.value != None) \
      .map(lambda cell: str.strip(cell.value)) \
      .toSet()

这个项目将为常用内建类型提供包装类,避免使用 Python 难读的函数式编程风格。

usage

List

from funcChaining import Type

lst = Type.List([1, 2, 3])

# 函数式方法的链式调用
result = lst.filter(lambda num: num % 2 != 0) \
   .map(lambda num: num * 2) \
   .reduce(lambda total, num: total + num, 0)
print(result)
# > 18

# 函数式方法不会影响实例本身
print(lst)
# > [1, 2, 3, 4, 5]


# 原有修改器方法的链式调用包装
print(lst.append(6).insert(6, 7).pop().remove(6))
# > [1, 2, 3, 4, 5]

print(lst.clear().extend([4, 5, 6]))
# > [4, 5, 6]

print(lst.reverse().sort(key=lambda x: -x))
# > [6, 5, 4]


# 访问器方法
newlst = lst.copy()
print(newlst)
# > [6, 5, 4]

print(lst.index(5))
# > 1

print(lst.count(1))
# > 0


# 可靠地重载了运算符
print(lst + [3, 2, 1])
# > [6, 5, 4, 3, 2, 1]

print([7] + lst)
# > [7, 6, 5, 4, 3, 2, 1]

lst += [0]
print(lst)
# > [7, 6, 5, 4, 3, 2, 1, 0]

lst *= 2
print(lst)
# > [7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0]

print(isinstance((lst * 2), List))
# > True
print(isinstance((2 * lst), List))
# > True
print(isinstance((lst + [1]), List))
# > True
print(isinstance(([1] + lst), List))
# > True

py-functional-chaining's People

Contributors

drincann avatar

Stargazers

chaoless avatar 齐下无贰 avatar  avatar

Watchers

 avatar

py-functional-chaining's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.