Skip to content

Riparo/fc-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fc-Python

Build Status PyPI

Enables Python to be functionally chain-styled.

Brother Project

Document

  • Simplified Chinese documents here.

If you are interested in me,you can follow my Blog(一波软广2333)。

How to use?

install form pypi:

$ pip install fc

and you just import Fc,like this: from fc import Fc

then you can enjoy lambda like Kotlin/JS:

from fc import Fc

l = (
  Fc([1, 2, 3, 4, 5])
    .map(lambda x: x + 1)
    .filter(lambda x: x > 4).print()    # [5, 6]
    .map(lambda x: x + 1).print()       # [6, 7]
    .cat([8, 9]).print()                # [6, 7, 8, 9]
    .add(10).print()                    # [6, 7, 8, 9, 10]
    .catHead([1, 2, 3, 4, 5]).print()   # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    .limit(3)
    .done()
)
assert l == [1, 2, 3]

Multi-line Lambda

if you not like single line lambda for python,you can import mml(Make Multi-line Lambda) of fc like this:

from fc import Fc
from fc import mml as m

# normal
assert Fc([1, 2, 3]).map(m(
  '''
    lambda x:
      x*=2
      return x
  '''
)).done() == [2, 4, 6]

# default value
assert Fc([1, 2, 3]).map(m(
  '''
    lambda x,y=1:
      x+=y
      return x
  '''
)).done() == [2, 3, 4]

# support list,dict
assert m(
  '''
      lambda *l,**k:
        return (l,k)
    '''
)(1, a=2) == ((1,), {'a': 2})

# even supports recursive anonymous functions(but must use {self})
assert Fc([1, 2, 3, 4, 5]).map(m('''
lambda x:
  if x<=0: return 0
  else:    return x+{self}(x-1)
  ''')).done() == [1, 3, 6, 10, 15]

More

You can check this this test file and understand more.

Have Fun ;)

About

Enables Python to be functionally chain-styled.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages