Skip to main content

Phi is a library for fluent functional programming in Python which includes a DSL + facilities to create libraries that integrate with it.

Project description

# Phi
Python is a very nice language that favors readability but its not very strong at functional programming and this often leads to repetitive code. Phi eases your functional programming experience in Python by providing the following modules

* [dsl](https://cgarciae.github.io/phi/dsl.m.html): a neat way to compose computations + more.
* [lambdas](https://cgarciae.github.io/phi/lambdas.m.html): easy way to create quick lambdas with a mathematical flavor.
* [builder](https://cgarciae.github.io/phi/builder.m.html): an extensible class that enables you to integrate other libraries into the DSL through a [fluent](https://en.wikipedia.org/wiki/Fluent_interface) API.
* [patch](https://cgarciae.github.io/phi/patch.m.html): this module contains some helpers that enable you to integrate a complete existing module or class into the DSL be registering its methods/functions into a [Builder](https://cgarciae.github.io/phi/builder.m.html#phi.builder.Builder).

## Documentation
Check out the [complete documentation](https://cgarciae.github.io/phi/index.m.html).

## Installation

pip install phi==0.2.1


#### Bleeding Edge

pip install git+https://github.com/cgarciae/phi.git@develop

## Status
* Version: **0.2.1**.
* Current effort: Documentation (> 60%). Please create an issue if documentation is unclear, its of great priority for this library.
* Milestone: reach 1.0.0 after docs completed + feedback from the community.


## Getting Started
The global `phi.P` object exposes most of the API and preferably should be imported directly. The most simple thing the DSL does is function composition:


from phi import P

f = P.Make(
lambda x: x + 1,
lambda x: x * 2,
lambda x: x + 3
)

assert 11 == f(3)


The above computation is equivalent to


lambda x: (x + 1) * 2 + 3


`P.Make` can compile any valid expression of the DSL into a function, what you are seeing here is the compilation of the `*args` tuple. Check out the documentation of the [dsl](https://cgarciae.github.io/phi/dsl.m.html).

Now lets rewrite the previous using `P`'s [lambdas](https://cgarciae.github.io/phi/lambdas.m.html) capabilities


from phi import P

f = P.Make(
P + 1,
P * 2,
P + 3
)

assert 11 == f(3)


**Pipe**

You can also pipe a value directly into an expression with `P.Pipe`


from phi import P

assert 11 == P.Pipe(
3,
P + 1,
P * 2,
P + 3
)


### Branching
Sometimes we have do separate computations, this is where branching comes in. It is expressed via a list (iterable in general) where each element is a different computational path and a list is returned by the Branch element:


import phi import P

assert [0, 4] == P.Pipe(
1,
P + 1,
[
P * 2
,
P - 2
]
)


The above computation equivalent to


lambda x: [(x + 1) * 2, (x + 1) - 2]


As you the the `[...]` element is compiled to a function that returns a list of values. Check out the documentation of the [dsl](https://cgarciae.github.io/phi/dsl.m.html) for more information.

## Nice Examples


from phi import P, Obj

text = "1 22 33"

avg_word_length = P.Pipe(
text,
Obj.split(" "), # ['1', '22', '333']
P.map(len), # [1, 2, 3]
P.sum() / P.len() # sum([1,2,3]) / len([1,2,3]) == 6 / 3 == 2
)

assert 2 == avg_word_length

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

phi-0.2.1.tar.gz (20.7 kB view details)

Uploaded Source

File details

Details for the file phi-0.2.1.tar.gz.

File metadata

  • Download URL: phi-0.2.1.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for phi-0.2.1.tar.gz
Algorithm Hash digest
SHA256 4d2982ec8cd7a0f6b019b661fa436a4ed3db5537d3715e491a5693bdc2be4f70
MD5 f366946c9b431d23f0c50195f48c7e9e
BLAKE2b-256 d450d63bee3c0756e8aab9848e90cf6a3fec563f12823c09496416191c874d87

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