Skip to content

timoniq/kungfu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kungfu

⚙️ Functional typing in Python!

get_user()
  .then(get_posts)
  .ensure(lambda posts: len(posts) > 0, "User has no posts")
  .map(lambda posts: sum(post.views for post in posts) / len(posts))
  .unwrap()

why kungfu?

kungfu is based on the belief that raising exceptions should be avoided. So it defines a set of functional types needed to write better code. This type strategy grants you with higher control over your runtime.

Panicking is the last recourse but for some obscure reason spawning exceptions each time you encounter something other than the all-successful behaviour became a norm in python code.

Let's fix this up and instead of panicking do treat error-state as equal to successful-state. kungfu provides you with all you need to migrate to functional typing approach

Improving control flow will definitely result in avoiding logical errors and getting better code readability: you start to see each kind of behaviour you get from the function.

why "kungfu" as a name? writing in kungfu requires some discipline. kung fu literally meaning "hard work" reflects on this concept. through the hard work on gaining control over our types we get clarity and power

(📖) documentation

See documentation

See examples

examples

howto build monad chains:

def get_user(user_id: int) -> Result[User, str]:
    ...

def get_posts(user: User) -> Result[list[Post], str]:
    ...

def get_average_views(user_id: int) -> Result[int, str]:
    return (
        get_user(user_id)
        .then(get_posts)
        .ensure(len, "User has no posts")
        .map(lambda posts: sum(post.views for post in posts) / len(posts))
    )

avg_views = get_average_views().unwrap()

howto detalize function result:

@unwrapping
def send_funds(
    sender_id: int, 
    receiver_id: int, 
    amount: decimal.Decimal,
) -> Result[TransactionID, str]:
    sender = get_user(sender_id).expect("Sender is undefined")
    receiver = get_user(receiver_id).expect("Receiver is undefined")

    if sender.get_balance().expect("Could not get sender balance") < amount:
        return Error("Sender has not enough funds to complete transaction")
    
    return Ok(
        create_transaction(sender, receiver, amount)
        .unwrap()
        .transaction_id
    )

Contributions are welcome

MIT licensed

About

Functional typing in Python

Resources

License

Stars

Watchers

Forks

Contributors 5

Languages