DEV Community

Discussion on: Functional programming in python

Collapse
 
cappe987 profile image
Casper

Any language that has functions would encourage you to use functions in your example, because of DRY. So that's not really a good example to demonstrate FP. Functions does not mean it's FP. C is in no way am FP language, yet you should use functions to keep it DRY. FP means to use only functions to compose your code, generally coupled with immutability.

You are right that less code means less memory. But in terms of code size it may only be a few hundred bytes and should be the least of your concerns, especially since using functions is something you should do anyways. DRY focuses on writing maintainable and readable code, not efficiency.

Generally when talking about memory efficiency we mean space complexity, how much memory the code requires to run. Even if the code is only a few lines, if it creates a list of 1000 objects then that list has probably already exceeded the size of the code in terms of memory. And in terms of that I would say that FP is generally less memory efficient because of immutability (but if you know what you're doing you can still write efficient code).

If you're interested in learning an actual FP language I would recommend maybe F# or Clojure.

Thread Thread
 
aswinbarath profile image
Aswin Barath

@cappe987 I can understand that there is more to functional programming with concepts like Recursion, Referential transparency, Functions are First-Class and can be Higher-Order, Variables are Immutable.
FP is well implemented in languages like Common Lisp, Scheme, Clojure, Wolfram Language, Racket, Erlang, OCaml, Haskell, and F#.

My point is that functional programming is sometimes treated as synonymous with purely functional programming, a subset of functional programming which treats all functions as deterministic mathematical functions, or pure functions.
In addition, many other programming languages support programming in a functional style or have implemented features from functional programming, such as C++11, Kotlin, Perl, PHP, Python, Go, Rust, Raku, and Scala.

Check out the Wikipedia page on Functional programming to understand that FP can be applied to python.

Functional programming is a programming paradigm, so FP can be applied to a language like python using pure functions concept.

And I never mentioned that python is an FP language or that if we use functions it becomes FP.
My point is that we can apply the FP concept of pure functions to implement the FP style.

So please don't confuse yourself about FP cannot be implemented in languages like python.

Thread Thread
 
aswinbarath profile image
Aswin Barath

@cappe987
Casper, before making strong opinions and extreme use-case on Functional programming being not memory efficient, please look at the big picture.

When you write through functional programming, you store less information right?
You say that - "You are right that less code means less memory. But in terms of code size it may only be a few hundred bytes and should be the least of your concerns, especially since using functions is something you should do anyways."

But, consider a big project which has millions or billions of lines of code in its codebase, at those times you get to reduce a lot of memory in an exponential rate.

Thread Thread
 
cappe987 profile image
Casper

I'm well aware how FP concepts can be applied in non-FP languages. I never said anything against pure functions in Python (I just pointed out that map/filter/reduce aren't inherently pure as the purity depends on the function passed, I wasn't discrediting the use of them) But you talked about using functions to reduce code duplication as an FP concept when it's not. It's a concept of programming in general.

when I say FP code is being more memory efficient I mean that
when we start to use functions we are reducing the number of repetitions in our code

You could remove the "FP" from the quote above and it would still hold true.

Thread Thread
 
aswinbarath profile image
Aswin Barath

OK, got it