_Tested on python version 2.6, 2.7, 3.x
This package provides an incomplete simulation of LINQ/Stream style stream processing for python.
This package aims to provides but some convenient methods, not a complete fp toolset to replace traditional pythonic coding.
[TOC]
pip install easyfunc
Copy easyfunc.py to your project and use it.
from easyfunc import Stream
Create from iterable
Stream(range(10))
Stream([1,2,3,4,5])
Create Stream literally
Stream.of(1,2,3,4)
Infinite number Stream
Stream.number() # 0,1,2,3,...
Stream.number(start=100, step=5) # 100,105,110,...
Empty Stream
Stream.empty()
Concat with other Stream/Iterable
Stream.concat(Stream.of(1,2,3), Stream.of(4,5,6)) # 1,2,3,4,5,6
Stream.concat(Stream.of(1,2,3), range(3)) # 1,2,3,0,1,2
Extend current Stream with Stream/Iterable
Stream.of(1,2,3).extend(Stream.of(4,5,6)) # 1,2,3,4,5,6
Stream.of(1,2,3).extend(range(3)) # 1,2,3,0,1,2
Zip two Stream/Iterable
Stream.zip(range(3), Stream.number()) # (0,0),(1,1),(2,2) # stop at the shorter one
Stream.number().zip_with(Stream.of(99, 100)) # (0,99),(0,100)
Append element(s) to current Stream
Stream.of(1,2,3).append(4,5) # 1,2,3,4,5
Prepend element(s) to current Stream
Stream.of(4,5).prepend(1,2,3) # 1,2,3,4,5
Flat inside iterable to one-dimension
Stream.of([1,2,3], Stream.of(4,5,6)).flat() # 1,2,3,4,5,6
Take from first
Stream.number().take(4) # 0,1,2,3
Stream.number().take(4).take(5) # 0,1,2,3
Take while True, stop when False
Stream.number().takeWhile(lambda x: x != 4) # 0,1,2,3
Filter by condition
Stream.number().filter(lambda x: x % 2 == 0) # 0,2,4,6,...
Get next item
Stream.number().next_item().get() # 0
Stream.empty().next_item().or_else(-1) # -1
Find first which satisfies
Stream.number(start=1).find_first(lambda x: x % 6 == 0).or_else(100) # 6
If any matchs
Stream.number().take(8).any(lambda x: x % 7 == 0) # True
If all match
Stream.of(2,4,6,8).all(lambda x: x % 2 == 0) # True
Map each elements
Stream.number().map(lambda x: str(x)) # '0','1','2',...
Foreach
def printItem(x):
print(x)
Stream.number().take(4).foreach(lambda x: printItem(x))
Sum for numbers
Stream.number().take(10).sum() # 45
Join for strings
Stream.number().map(lambda x: str(x)).take(4).join(".") # '0.1.2.3'
Fold(reduce)
Stream.number().take(10).fold(lambda x, y: x+y, 0) # 45