Skip to content

Feature request: .pipe, and possibly .fpipe #37

@james-pcdr

Description

@james-pcdr

Hello again,
What is your opinion on adding...

  1. a pipe method that simply applies a function, and
  2. a fpipe method that additionally wraps the result in a flu?

The benefit IMO is that it preserves the top-to-bottom readability of the fluent approach.

1. pipe example:

from statistics import mean

avg_of_small_nums = (flu([10, 30])
  .map(lambda x: x*10)
  .pipe(mean)
)

assert avg_of_small_nums == 200

2. fpipe example:

from itertools import starmap as starmap_original
from functools import partial

def starmap(f):
    return partial(starmap_original, f)

names_and_ages = ["bob:50", "sue:44"]
introductions = (flu(names_and_ages)
  .map(lambda x: x.split(":"))
  .fpipe(starmap(lambda name, age: f"{name} is {age} years old"))
  .to_list()
)

assert introductions == ["bob is 50 years old", "sue is 44 years old"]

Notes:

  • I have no opinion on the name choices. I picked pipe because I've seen it in similar projects.
  • I'm happy to submit a PR if you like the suggestion.

EDITED: fixed a parenthesis

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions