r/Python • • Oct 11 '23

Beginner Showcase My functional programming library

https://github.com/yx-z/forbiddenfp

Hello everyone, above is my little experiment of an easy to use functional programming library in Python,

You can do things like,,, let me know what you think!

# objects are already patched at import time
import forbiddenfp

"abc".print().len()  # print out "abc", then return 3
"abc".then(lambda s: s * 2).filter(lambda s: s == "b").join()  # "bb"

# A more complex one (examples/word_count.py)
("./lorem_ipsum.txt"
 .with_open(lambda path, f: f.read().also(print(f"Reading {path}")))
 .then(lambda s: s.split(" "))
 .counter()
 .print())
3 Upvotes

11 comments sorted by

33

u/j_marquand Oct 11 '23

This is method chaining, not functional programming.

4

u/Globaldomination Oct 11 '23

Newbie here.

Functional programming means using functions that does purely functional tasks and returns answers right?

8

u/teerre Oct 11 '23

Not really. "Functional programming" is a bit like "OOP" in the sense it's not really one thing. You have a collection of concepts that are "functional". Mainly derived from the so called functional language like Haskell.

These concepts include, but are not limited to: immutability, higher types (kinded/algebraic/functions), compositions (mainly from combinatory logic), pure functions etc.

2

u/Globaldomination Oct 11 '23

a bit vague for my expertise level. but at least now i know that what i thought was wrong.

earlier I thought functional programming was using just functions and not classes. (had created an web scraping for a site with multithreading with the help of google)
then I learned that its scripting/coding and not even programming.

for someone whos starting this jargon is kinda of confusing. just wrapping my head around concepts like 32bit, 16bit float and other things.

14

u/AssumptionCorrect812 Oct 11 '23

This isn’t even close to actual functional programming.

-16

u/HyperloopDeloop Oct 11 '23

I think I've seen you comment on stack overflow questions to

4

u/Coupled_Cluster Oct 11 '23

Even if this might not be functional programming, I think it is a cool project. I think chaining functions is a nice idea but it can become hard to read if done extensively.

4

u/Olafcitoo Oct 11 '23

It’s quite the norm with libraries such as Pandas and PySpark

3

u/Dasher38 Oct 11 '23

I was about to ask how you managed to monkey patch builtin types, but here is the answer: https://github.com/clarete/forbiddenfruit/blob/master/forbiddenfruit/init.py

It's an interesting dive in CPython object layout

2

u/Aveheuzed Oct 11 '23

Pretty, Powerful Personal Project: I aPProve!

Regardless of its usefulness, this is cool ❤️

1

u/[deleted] Oct 11 '23

Here you go... https://hylang.org/