Skip to main content

Functional Programming Streams ,Similar like Java, for writing concise functions

Project description

functional-streams

Writing concise functional code in python

Converting to concise code

Demo

#To Fetch from a list of users
#       Get their firstname , if their salary greater than 80000 and gender is male

#Instead of writing like this


list(map(lambda user: user['first_name'],  
         filter(lambda user:user['salary'] > 80000, 
                filter(lambda product: product['gender'] == 'Male',
                       users))))

#Write this
from streams.Stream import Stream

(Stream
   .create(users)
   .filter(lambda user:user['salary'] > 80000)
   .filter(lambda product: product['gender'] == 'Male')
   .map(lambda user: user['first_name'])
   .asList())

#A concise way to write lambdas,functional code in python
from streams.Stream import Stream
users = [
    {
        "id": 1,
        "first_name": "Mandy",
        "last_name": "Gowan",
        "email": "mgowan0@aol.com",
        "gender": "Female",
        "loves": ['Soccer','Cricket','Golf'],
        "salary": 119885
    },
    {
        "id": 2,
        "first_name": "Janessa",
        "last_name": "Cotterell",
        "email": "jcotterell1@aol.com",
        "gender": "Female",
        "loves": ['Cricket'],
        "salary": 107629
    },
    {
        "id": 6,
        "first_name": "Jasen",
        "last_name": "Franzini",
        "email": "jfranzini5@aol.com",
        "gender": "Male",
        "loves": ['Soccer','Golf'],
        "salary": 78373
    }
]

#Using Map Filter 
results = (Stream
           .create(users)
           .filter(lambda user:user['salary'] > 80000)
           .map(lambda user: user['first_name'])
           .asList())
#['Mandy', 'Janessa']

#Using flatMap Distinct 
results = (Stream
           .create(users)
           .flatmap(lambda user:user['loves'] )
           .distinct()
           .asList())
#['Cricket', 'Golf', 'Soccer']

#Using skip take 
results = (Stream
           .create(users)
           .skip(1)
           .take(1)
           .map(lambda user: user['first_name'])
           .asList())
#['Janessa']


#Even you can peek results
results = (Stream
           .create(users)
           .peek(lambda data:print("User",data))
           .map(lambda user: user['first_name'])
           .asList())
#Will list out all users

Additional Information

Design

Most of the functions underneath uses the same functions available in python (map uses map , filter uses filter etc..). Only we have added wrapper to make the code concise

Abstractions

If you need to use abstractions to get reusable , try using stream method. as the generators used get corrupted by the very first expansion For Example

stream_of_users = (Stream
                    .create(users)
                    )

#The below code wont work , as the genrators expire once you aggregate it
total_users = (stream_of_users
               .length())

firstname_of_users = (stream_of_users           
                           .map(lambda user: user['first_name'])
                           .asList())


#The above code should be rewritten as
total_users = (stream_of_users
                .stream()
                .length())

firstname_of_users = (stream_of_users
                           .stream()
                           .map(lambda user: user['first_name'])
                           .asList())

# The stream will make use of copying the generators

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

functional-streams-1.2.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

functional_streams-1.2.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file functional-streams-1.2.0.tar.gz.

File metadata

  • Download URL: functional-streams-1.2.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8

File hashes

Hashes for functional-streams-1.2.0.tar.gz
Algorithm Hash digest
SHA256 d5d3646abc152344019f830e8305f6e10dedf3133afb0b632916f4113d880d51
MD5 d0a2b7ee0354cb4b7cb21ff85699e0b9
BLAKE2b-256 3c2bc4ce7feff83c80bdaf31e490311aa6b943db28eb51e85436cd70dc535c92

See more details on using hashes here.

File details

Details for the file functional_streams-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: functional_streams-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8

File hashes

Hashes for functional_streams-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6b4a7ed46f72a0b054493ad7bc40da6d4eb637f0d104ace38d5d214c6d98a6fc
MD5 7a0ca1f25d9532419f0fe6d6b4bd7aa3
BLAKE2b-256 d5bcece90e10564aa1670e7f2b476c7730e97039d77c0774c459a5f437f91df1

See more details on using hashes here.

Supported by

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