evhub / coconut

Simple, elegant, Pythonic functional programming.
http://coconut-lang.org
Apache License 2.0
4.08k stars 125 forks source link

Hygenic Macros #102

Open datnamer opened 8 years ago

datnamer commented 8 years ago

Requesting Hygenic Macros like in macropy: https://github.com/lihaoyi/macropy

Its useful for example to refer to the columns of a dataframe of object in a formula or expression that is later expanded without being eagerly evaluated :

Or write more complex arbitrary domain specific languages for data analysis packages.

Relevant PEP: https://mail.python.org/pipermail/python-ideas/2015-March/032822.html

More use cases: http://multithreaded.stitchfix.com/blog/2015/03/17/grammar-of-data-science/

CC: @shoyer who might be interested in coconut as a Pydata DSL (with piping, and lots of other functional goodness, compiles to valid python 2/3, 100% compatible with python 3 etc) and this issue specifically.

Thanks!

evhub commented 8 years ago

Thanks for the issue! I'll look into this and see what I can do for the next version.

datnamer commented 8 years ago

Great! I'm hoping that coconut will hit critical mass with pydata people and we can help start to pick up some of the contrib and maintenance burden.

Again, inspiration from Julia. http://gray.clhn.org/dl/macros_etc.pdf

datnamer commented 8 years ago

cc @TomAugspurger who might also be interested in this

evhub commented 8 years ago

@datnamer @TomAugspurger It doesn't resolve this issue, but I thought you should know that in v1.1.1, I added a methodcaller implicit partial, which I believe should be a big help in using libraries like pandas with Coconut, since, because Python lacks pipes, pandas uses methods for everything. Thus,

flights |> group_by('year', 'month', 'day')

won't work in Coconut, since it assumes group_by is a function, not a method. But now, you should be able to do

flights |> .group_by('year', 'month', 'day')

(note that the only change was adding a .) which will allow you to mix and match methods and custom functions when using pipeline-style programming, so that

flights |> .group_by('year', 'month', 'day') |> my_transform_func |> .select('arr_delay', 'dep_delay')

will work, which is much cleaner than in Python, where you'd have to do

my_transform_func(flights.group_by('year', 'month', 'day')).select('arr_delay', 'dep_delay')

which obscures the order in which the functions / methods are called.

evhub commented 2 years ago

Now that there's a PEP for this, Coconut could try to implement it.