Giter Club home page Giter Club logo

pyfuncol's Introduction

pyfuncol

CI codecov PyPI Downloads Documentation Status GitHub license

A Python functional collections library. It extends collections built-in types with useful methods to write functional Python code. It uses Forbidden Fruit under the hood.

pyfuncol provides:

  • Standard "eager" methods, such as map, flat_map, group_by, etc.
  • Parallel methods, such as par_map, par_flat_map, etc.
  • Pure methods that leverage memoization to improve performance, such as pure_map, pure_flat_map, etc.
  • Lazy methods that return iterators and never materialize results, such as lazy_map, lazy_flat_map, etc.

pyfuncol can also be used without forbiddenfruit.

Installation

pip install pyfuncol

Usage

Note: If you are not using forbiddenfruit, the functions will not extend the builtins. Please see here for usage without forbiddenfruit.

To use the methods, you just need to import pyfuncol. Some examples:

import pyfuncol

[1, 2, 3, 4].map(lambda x: x * 2).filter(lambda x: x > 4)
# [6, 8]

[1, 2, 3, 4].fold_left(0, lambda acc, n: acc + n)
# 10

{1, 2, 3, 4}.map(lambda x: x * 2).filter_not(lambda x: x <= 4)
# {6, 8}

["abc", "def", "e"].group_by(lambda s: len(s))
# {3: ["abc", "def"], 1: ["e"]}

{"a": 1, "b": 2, "c": 3}.flat_map(lambda kv: {kv[0]: kv[1] ** 2})
# {"a": 1, "b": 4, "c": 9}

pyfuncol provides parallel operations (for now par_map, par_flat_map, par_filter and par_filter_not):

[1, 2, 3, 4].par_map(lambda x: x * 2).par_filter(lambda x: x > 4)
# [6, 8]

{1, 2, 3, 4}.par_map(lambda x: x * 2).par_filter_not(lambda x: x <= 4)
# {6, 8}

{"a": 1, "b": 2, "c": 3}.par_flat_map(lambda kv: {kv[0]: kv[1] ** 2})
# {"a": 1, "b": 4, "c": 9}

pyfuncol provides operations leveraging memoization to improve performance (for now pure_map, pure_flat_map, pure_filter and pure_filter_not). These versions work only for pure functions (i.e., all calls to the same args return the same value) on hashable inputs:

[1, 2, 3, 4].pure_map(lambda x: x * 2).pure_filter(lambda x: x > 4)
# [6, 8]

{1, 2, 3, 4}.pure_map(lambda x: x * 2).pure_filter_not(lambda x: x <= 4)
# {6, 8}

{"a": 1, "b": 2, "c": 3}.pure_flat_map(lambda kv: {kv[0]: kv[1] ** 2})
# {"a": 1, "b": 4, "c": 9}

pyfuncol provides lazy operations that never materialize results:

list([1, 2, 3, 4].lazy_map(lambda x: x * 2).lazy_filter(lambda x: x > 4))
# [6, 8]

list({1, 2, 3, 4}.lazy_map(lambda x: x * 2).lazy_filter_not(lambda x: x <= 4))
# [6, 8]

list({"a": 1, "b": 2, "c": 3}.lazy_flat_map(lambda kv: {kv[0]: kv[1] ** 2}))
# [("a", 1), ("b", 4), ("c", 9)]

set([1, 2, 3, 4].lazy_map(lambda x: x * 2).lazy_filter(lambda x: x > 4))
# {6, 8}

Usage without forbiddenfruit

If you are using a Python interpreter other than CPython, forbiddenfruit will not work.

Fortunately, if forbiddenfruit does not work on your installation or if you do not want to use it, pyfuncol also supports direct function calls without extending builtins.

from pyfuncol import list as pfclist

pfclist.map([1, 2, 3], lambda x: x * 2)
# [2, 4, 6]

API

For lists, please refer to the docs.

For dictionaries, please refer to the docs.

For sets and frozensets, please refer to the docs.

For more details, please have a look at the API reference.

We support all subclasses with default constructors (OrderedDict, for example).

Documentation

See https://pyfuncol.readthedocs.io/.

Compatibility

For functions to extend built-ins, Forbidden Fruit is necessary (CPython only).

Contributing

See the contributing guide for detailed instructions on how to get started with the project.

License

pyfuncol is licensed under the MIT license.

pyfuncol's People

Contributors

dependabot[bot] avatar gondolav avatar m-doescode avatar samuelchassot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pyfuncol's Issues

[FEATURE] Make forbiddenfruit optional

It would be great if forbiddenfruit was made optional. Since forbiddenfruit requires CPython and I think if it would be better if it could work on other versions of python.

Instead of:

import pyfuncol

my_list = [1, 2, 3]
print(my_list.map(lambda x: x * 2))

I can do:

from pyfuncol.list import *

my_list = [1, 2, 3]
print(map(my_list, lambda x: x * 2))

Maybe there can be a check to see if forbiddenfruit is installed. If it is, add all the functions to the builtin classes, otherwise do nothing.

Sorry if I misunderstand how this works.

[FEATURE] Add memoization

Is your feature request related to a problem? Please describe.
Expensive computations that are repeated in a map do not leverage the fact that we already computed once the result.

Describe the solution you'd like
We can memoize pure functions on hashable inputs with something like:

cached_f = functools.cache(f)
return [cached_f(x) for x in self]

To not restrict the library's usages, it would probably make sense to create pure_map, pure_filter, etc.

[FEATURE] Add lazy operations

Describe the solution you'd like
List operations like lazy_map, lazy_filter should return generators instead of lists.

Describe alternatives you've considered
An alternative would be adding a lazy argument to each operation, but it's less clean IMHO.

[FEATURE] Optimise chained transformations

Describe the solution you'd like
We should optimise chained transformations in a similar way as what Apache Spark does: they should be lazy and re-ordered so that transformations reducing the number of elements are always executed first.

[BUG] Built-in subclasses are not preserved

Describe the bug
Applying transformations on built-in subclasses, like OrderedDict, returns a plain dict.

Expected behavior
Subclasses should be preserved.

Desktop (please complete the following information):

  • OS: macOS Monterey 12.1
  • Python version 3.9.7
  • pyfuncol version 1.0

Additional context
Could be solved by doing type(self)(...) before returning the result.

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.