Live data from Hacker News

More intuitive partial function application in Python

github.com

21–30 of 36 posts

Re: More intuitive partial function application in Python

#21
While it does solve the problem with functools.partial() forcing you to use keyword arguments in some cases, I don't think currying is a common enough operation to warrant a whole lib, or even a new syntax.

I'd rather see partial() promoted to a universal static method attached to any callable, so it's easier to discover, and use.

Re: More intuitive partial function application in Python

#22
This is great. I will use it with my python chaining approach: https://github.com/tpapastylianou/chain-ops-python

My only grief is that decorator, which forces you to wrap existing functions anyway (same way I had to define lambdas in my example anyway).

Do you have any insight on that?

Re: More intuitive partial function application in Python

#23
This is not a new idea. siuba (a dplyr-like Pandas alternative) is one project that does it, and it also has a "pipeline operator": https://github.com/machow/siuba

(mtcars >> group_by(_.cyl) >> summarize(avg_hp = _.hp.mean()) )

(Edit: ok, it seems siuba doesn't have the "parameter fixing" thing.)

Related: Coconut - a functional superset of Python - https://github.com/evhub/coconut

range(10) |> map$(pow$(?, 2)) |> list

There were others but I can't remember which now.

Re: More intuitive partial function application in Python

#26
post #9

whats wrong with functools.partial? I don't see that anywhere in the arguments.. all you say is "I find functools.partial unintuitive".. why? its a simple wrapper.

here's an example of one of the behaviors of functools.partial that I find unintuitive / undesirable:

https://gist.github.com/chrisgrimm/64bca66f14528cfda6d865cc2...

Re: More intuitive partial function application in Python

#28

Could this be used without the "bp."? Half the point of coming up with a nicer notation is for the notation to actually be nicer.

As pointed out, you could import _ directly into your namespace, or map it to some other shorthand name that doesn't interfere with your naming conventions.

Also note that the _ placeholder isn't the only way that better_partial helps you. You can also use ... to indicate that you want to omit all arguments except ones you explicitly pass as kwargs. For instance:

  from better_partial import partial, _

  @partial
  def f(a,b,c,d,e):
    return (a,b,c,d,e)
  
  f(_,_,3,_,_)(1,2,4,5) == f(..., c=3)(1,2,4,5)

Re: More intuitive partial function application in Python

#29

Hey, I use functools.partial a lot in my projects and I've always felt like it was pretty limited in the types of partial function application it could accomplish. I put together a modified version of partial over the weekend that I wanted to share / get feedback on. Let me know what you all think!

`functools.partial` has been bothering me for a long time and I've always thought it'd be nice to have something like the placeholder `·` that people use in math (i.e. `f(·, y, z)`). It didn't occur to me that a decorator might actually get us there without having to introduce new syntax. Nice project!

I'm glad you like it! This project was basically me trying to visualize how I wish functions generally worked like in Python.

Re: More intuitive partial function application in Python

#30

I was gonna say cool for FP guys but alien to the Python world, but then i saw the f(..., x=1) syntax and decided you're a genius. Really nice idea. In practice i worry about static checkers and IDE param hints though, how have you fared with these? Not your fault at all, but mypy/pyright have very poor support for wrappers like this, and in IDEs it's common for their arg hints to devolve to a copout (args, kwargs) a…

High praise :-) I'm glad you like it! I don't know much about static checks / param hinting. TBH, I coded this up because I wanted to imagine how functions should work in Python as more of a fun exercise.

Accordingly there are a lot of things I have to do to make better_partial a strong tool for the community. I'll probably have time to work on the project again later in the week.

Post reply on HN