Git Product home page Git Product logo

Comments (2)

olirice avatar olirice commented on May 31, 2024

The implementation of flu.reduce is based on functools.reduce.

from typeshed, python's functools.reduce supports both same and different types

def reduce(function: Callable[[T, T], T], sequence: Iterable[T]) -> T: 

def reduce(function: Callable[[S, T], S], sequence: Iterable[T], initial: S) -> S:

In contrast, flu.reduce only supports

def reduce(self, func: Callable[[T, T], T]) -> T:

I think the right solution would be to add

def fold_left(self: Fluent[T], function: Callable[[S, T], S], initial: S) -> S:

That is also implemented in terms of functools.reduce.

Based on SO reference below, the academic difference between the fold_left and reduce is that reduce assumes associativity. That assumption enables parallelism. Given that functools.reduce executes sequentially, they are interchangeable.

Assuming you agree with ^, Would you be interested in opening a PR? If not, I'm happy to knock it out within a few days.

References:

Explanation of reduce vs fold
https://stackoverflow.com/questions/25158780/difference-between-reduce-and-foldleft-fold-in-functional-programming-particula/25158790

Typeshed type definition of functools.reduce
https://github.com/python/typeshed/blob/d21370965aa2a1711d6986e509e43d651f2e3411/stdlib/3/functools.pyi#L10

Example implementation
https://www.burgaud.com/foldl-foldr-python

from flupy.

threecifanggen avatar threecifanggen commented on May 31, 2024

I've implemented it and opened a PR.
#12

from flupy.

Related Issues (14)

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.