Easy and simple functional programming style helper for python
Project description
Simple functional programming style for python
_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]
Installation
pip
pip install easyfunc
manually
Copy easyfunc.py to your project and use it.
Usage
from easyfunc import Stream
Stream creating
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()
Stream combining
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
Stream operating
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,...
Element Accessing
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
check elements
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 and foreach
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))
aggregate operations
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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file easyfunc-0.2.5.1.tar.gz.
File metadata
- Download URL: easyfunc-0.2.5.1.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50718bc7ccffbfae698eb6270101eb7a9db1cb888b15a75db5ff7a5bad651fa9
|
|
| MD5 |
93ed8f03cc8d8aa536471bb7113d94a4
|
|
| BLAKE2b-256 |
de54f00aaea48b769655f9549171119897ac2b11ed46f908af45a9b8d7a4188f
|
File details
Details for the file easyfunc-0.2.5.1-py3-none-any.whl.
File metadata
- Download URL: easyfunc-0.2.5.1-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c7e55e1b82500307950896e7b0c796e0700be4624ae39553e3aabf544a8f119
|
|
| MD5 |
9906927f48cea7f861984f9b971b5179
|
|
| BLAKE2b-256 |
3c8e5ce55fd0a47f761a9a4d276f0791b6a29752d8dfb181d49b63101d0ac155
|