EntilZha / PyFunctional

Python library for creating data pipelines with chain functional programming
http://pyfunctional.pedro.ai
MIT License
2.41k stars 132 forks source link

"peek" functionality #165

Closed kchro3 closed 3 years ago

kchro3 commented 3 years ago

Hi there, just checking out this package. Really awesome work!

I might have just missed it, but is there an idiomatic way to do a "peek" operation (https://www.baeldung.com/java-streams-peek-api)? TL;DR: In Java, you can call .peek( someFn ) as an intermediate step in the pipeline, but it wouldn't modify the value like .map would.


# for example
(
  seq(1, 2, 3)
  .map(lambda x: x ** 2)
  .peek(print)
  .to_set()
)

I think it could be helpful for logging / maintaining stats / debugging purposes.

EntilZha commented 3 years ago

Thanks! @bmsan asked for similar functionality from for_each https://github.com/EntilZha/PyFunctional/issues/153 and I was hesitant to change an existing API, but open to adding it with a reasonable name (they also have a workaround). If you are interested, I think I'd accept a PR that implements peek how you've proposed.