Live data from Hacker News

Functional Python Programming

docs.python.org

51–60 of 105 posts

Re: Functional Python Programming

#51
post #16
post #8

Sadly, Python is a pretty poor functional language. The core of functional programming is about avoiding mutable states , not much about anonymous functions or passing functions as data. To do proper functional programming in Python, there should be IMO: - a way to enforce non-mutable variables/objects; - non-mutable collections; - proper support for recursion and tail-recursion optimization; - a better syntax for an…

For tail recursion, you can use this snippet of code: class Recurse(Exception): def __init__(self, *args, **kwargs): self.args = args self.kwargs = kwargs class Terminate(Exception): def __init__(self, retval): self.retval = retval def tailrec(func): def wrapper(*args, **kwargs): while True: try: func(*args, **kwargs) except Recurse as r: args = r.args kwargs = r.kwargs except Terminate as t: return t.retval return w…

You can do a similar thing (that's around 2-3x faster) with a trampoline:

  def factorial(n):
      res = fac(n)
      while callable(res):
          res = res()

      return res

  def fac(n, acc=1):
      if n == 1:
          return acc
      else:
          return lambda: fac(n-1, n*acc)

Re: Functional Python Programming

#53
post #16
post #8

Sadly, Python is a pretty poor functional language. The core of functional programming is about avoiding mutable states , not much about anonymous functions or passing functions as data. To do proper functional programming in Python, there should be IMO: - a way to enforce non-mutable variables/objects; - non-mutable collections; - proper support for recursion and tail-recursion optimization; - a better syntax for an…

For tail recursion, you can use this snippet of code: class Recurse(Exception): def __init__(self, *args, **kwargs): self.args = args self.kwargs = kwargs class Terminate(Exception): def __init__(self, retval): self.retval = retval def tailrec(func): def wrapper(*args, **kwargs): while True: try: func(*args, **kwargs) except Recurse as r: args = r.args kwargs = r.kwargs except Terminate as t: return t.retval return w…

[deleted]

Re: Functional Python Programming

#54
post #50
post #21

shameless plug: I maintain a small library to do functional pipes. You can write: ( range(10) | Map(lambda x: x * 10) | Filter(lambda x: x % 2 == 0) | Reduce(lambda a, b: a + b) ) instead of: x = range(10) x = map(lambda x: x * 10, x) x = filter(lambda x: x % 2 == 0, x) x = reduce(lambda a, b: a + b, x) and more. https://tandav.github.io/pipe21/

import pandas as pd import functools ( pd.Series(range(10)) .apply(lambda x: x * 10) .where(lambda x: x % 2 == 0) .pipe(lambda s: functools.reduce(lambda x, y : x + y, s)) )

Readability isn't the best. Also what you present here is method chaining and not functional pipes.

Re: Functional Python Programming

#55

What's so useful about iterators and generators. The article says how to use them but not why. If you already know how to make list comprehensions and use "for elt in coll", do they let you do anything new?

Fundamentally? No, it's all just machine language in the end.

Practically? Yes, generators are often more memory-efficient, and thereby often more compute-efficient.

Re: Functional Python Programming

#56
Fun to work on doing FP in languages that don't really support it, but in my view a language has to be built for FP for it to be a practical option in any real applications. Several obvious reasons for Python being a poor lang in which to do FP:

  - mutable data structures 
  - no built-in function composition
  - limited support for HOF
  - no tail call optimization (AFAIK)
  - performance in general isn't great and I imagine it's even worse when relying heavily on recursion, etc
Still, a fun project!

Re: Functional Python Programming

#57
post #55

What's so useful about iterators and generators. The article says how to use them but not why. If you already know how to make list comprehensions and use "for elt in coll", do they let you do anything new?

Fundamentally? No, it's all just machine language in the end. Practically? Yes, generators are often more memory-efficient, and thereby often more compute-efficient.

Is it only about efficiency, and not expressivity? If so, can the compiler figure out where to transform my equivalent structures into iterators and generators?

Re: Functional Python Programming

#58
post #8

Sadly, Python is a pretty poor functional language. The core of functional programming is about avoiding mutable states , not much about anonymous functions or passing functions as data. To do proper functional programming in Python, there should be IMO: - a way to enforce non-mutable variables/objects; - non-mutable collections; - proper support for recursion and tail-recursion optimization; - a better syntax for an…

Syntactically maybe, but I find it has a quite workable functional subset.

Integers, floats, tuples, named tuples, and frozensets are all immutable, functions are values, etc. E.g.: https://joypy.osdn.io/notebooks/Derivatives_of_Regular_Expre... -or- https://github.com/calroc/xerblin/blob/master/xerblin/btree....

It's not fantastic, but it's not that bad.

Re: Functional Python Programming

#59
post #45

Earlier quoted context omitted.

It's not a list of improvements python needs. It's a list of things python already does / has.

do I have bots responding to me? whats hard about comprehending this? > To do proper functional programming in Python, there should be IMO: > ... > - a better syntax for anonymous functions than single statement lambdas.

[deleted]

Re: Functional Python Programming

#60
post #12

I thought I knew functional programming from knowing Python, but looking back I didn't really "grok" it until I moved to Elixir. Now I like it and prefer it. I don't run into any of the types of bugs I used to create in Python, mostly from being lazy and fiddling with data inside of a loop. I miss do miss early returns though.

I moved from Erlang to Python and was miserable. I still write most of my code in a functional style, but I dearly miss using a language that enforced those constraints.
Post reply on HN