A package providing streams and optional functionality similar to Java streams and optionals.
Project description
jstreams
jstreams is a Python library aiming to replicate the Java Streams and Optional functionality. The library is implemented with type safety in mind.
Installation
Use the package manager pip to install jstream.
pip install jstream
Usage
from jstream import Stream
# Applies a mapping function on each element then produces a new string
print(Stream(["Test", "Best", "Lest"]).map(str.upper).collect())
# will output ["TEST", "BEST", "LEST"]
# Filter the stream elements
print(Stream(["Test", "Best", "Lest"])
.filter(lambda s: s.startswith("T"))
.collect())
# Will output ['Test']
# isNotEmpty checks if the stream is empty
print(Stream(["Test", "Best", "Lest"])
.filter(lambda s: s.startswith("T"))
.isNotEmpty())
# Will output True
# Checks if all elements match a given condition
print(Stream(["Test", "Best", "Lest"]).allMatch(lambda s: s.endswith("est")))
# Will output True
print(Stream(["Test", "Best", "Lest"]).allMatch(lambda s: s.startswith("T")))
# Will output False
# Checks if any element matches a given condition
print(Stream(["Test", "Best", "Lest"]).anyMatch(lambda s: s.startswith("T")))
# Will output True
# Checks if no elements match the given condition
print(Stream(["Test", "Best", "Lest"]).noneMatch(lambda s: s.startswith("T")))
# Will output False
# Gets the first value of the stream as an Opt (optional object)
print(Stream(["Test", "Best", "Lest"])
.findFirst(lambda s: s.startswith("L"))
.getActual())
# Will output "Lest"
# Returns the first element in the stream
print(Stream(["Test", "Best", "Lest"]).first())
# Will output "Test"
# cast casts the elements to a different type. Useful if you have a stream
# of base objects and want to get only those of a super class
print(Stream(["Test1", "Test2", 1, 2])
.filter(lambda el: el == "Test1")
# Casts the filtered elements to the given type
.cast(str)
.first())
# Will output "Test1"
# If the stream elements are Iterables, flatMap will produce a list of all contained items
print(Stream([["a", "b"], ["c", "d"]]).flatMap(list).toList())
# Will output ["a", "b", "c", "d"]
# reduce will produce a single value, my applying the comparator function given as parameter
# in order to decide which value is higher. The comparator function is applied on subsequent elements
# and only the 'highest' one will be kept
print(Stream([1, 2, 3, 4, 20, 5, 6]).reduce(max).getActual())
# Will output 20
# notNull returns a new stream containing only non null elements
print(Stream(["A", None, "B", None, None, "C", None, None]).nonNull().toList())
# Will output ["A", "B", "C"]
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
jstreams-1.0.5.tar.gz
(7.4 kB
view details)
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 jstreams-1.0.5.tar.gz.
File metadata
- Download URL: jstreams-1.0.5.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb5c609f38f67ed2702a6e42cdb07cafcff9d68ef03cc06b0d4227ee4de7c420
|
|
| MD5 |
62e3fb1fcfb40af0cb4953290759acc2
|
|
| BLAKE2b-256 |
95ae9228a1c9bdbff4d4334be0ebb33a746740056e86f834de54ee1c84b9474d
|
File details
Details for the file jstreams-1.0.5-py3-none-any.whl.
File metadata
- Download URL: jstreams-1.0.5-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa609d36af57e44c91909985af6d2211e5eb4d5f248746a799bffcdc2151b05d
|
|
| MD5 |
589c4b724d3eaa17f32efb4380e0e1a3
|
|
| BLAKE2b-256 |
940954d9f120c424012336e370caa20b7f4f9f3babd3860b7e0622763a1aa584
|