Skip to main content

small playground library for fp in python

Project description

boxcat

A small playground library for fp in Python, inspired from my time writing Scala

Development testing

pytest

Usage


Options

To wrap values in Option monad

option_ten = Option(10)
option_string = Option("hello")
option_bool = Option(True)

.map()

Integer example

option_ten = Option(10)
option_multiply_by_two = option_ten.map(lambda x: x * 2)
print(option_multiply_by_two.get_or_else(0))
result:
20

String example

option_hello_world = Option("hello world")
option_uppercase = option_hello_world.map(lambda s: s.upper())


print(option_uppercase.get_or_else(""))
result:
HELLO WORLD

.unsafe_get()

To get the value out of the Option

option_hello_world = Option("just give me the value I know it's there")

print(option_hello_world.unsafe_get())
result:
"just give me the value I know it's there"

Seq

So there isn't a great way to my knowledge of adding extension methods in Python There was a thing called Monkey-Patching but seemed dodgy.

We can wrap traditional Lists [] in Seq()

seq_empty = Seq([])
seq_ten = Seq([10])
seq_string = Seq(['hello', 'world'])
seq_bool = Seq([True])

my_list = [1, 'a', 2, 'b', 3, 'c', 4, 'd', 5, 'e', 6, 'f', 7, 'g' 8, 'h', 9, 'i', 10]

seq_my_list = Seq(my_list)

Important!!

.to_list()

Use .to_list() to get back the original Python List type from Seq()

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

seq_my_list = Seq(my_list)

back_to_python_list = seq_my_list.to_list()

print(back_to_python_list)
result:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

.map()

Integer example

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

seq_my_list = Seq(my_list)

seq_add_one = seq_my_list.map(lambda x: x + 1)
print(seq_add_one.to_list)
result:
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

.filter()

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

seq_numbers = Seq(my_list)

only_evens = seq_numbers.filter(lambda x: x % 2 == 0)
print(seq_add_one.to_list)
result:
[2, 4, 6, 8, 10]

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

boxcat-0.0.5.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

boxcat-0.0.5-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file boxcat-0.0.5.tar.gz.

File metadata

  • Download URL: boxcat-0.0.5.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for boxcat-0.0.5.tar.gz
Algorithm Hash digest
SHA256 53256feeb88b37f9310532828930e28a0aa38e4d980e13a25e82789fe021a8fb
MD5 5d5a4e221f6bc2ced8ee4848e67ca84d
BLAKE2b-256 f4068e135b224d107432ff5704f0340a43a4460c6b730c393a128d83001cde76

See more details on using hashes here.

File details

Details for the file boxcat-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: boxcat-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for boxcat-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 fd5ee220cc68126d317aa6e7162cfaccd8a23c02403a4cd12094fa90941f0ccc
MD5 16f733c608c56448d35825dbdf424802
BLAKE2b-256 7ef11554a3f9ce46515d1fef0654d6828d74926906dd7c5678947e88c4bce319

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page