9 ms·
I've been writing functional code for a year or so now at work with the help of ramda in nodejs, there is also a port for python. My colleagues don't like becu
by vorticalbox 6y ago
I've been writing functional code for a year or so now at work with the help of ramda in nodejs, there is also a port for python.
My colleagues don't like becuase it's so different to what they are used to but I am putting out code faster and with less bugs so I'm not going to stop.
- deleted 6y ago[deleted]
- zelphirkalt 6y agoWhy use a library for it? Can it not be done with just Python? And if using the library is much different from normal Python, does it mitigate Python's problems with functional programming? (For example one expression only lambdas and no TCO.) I also do use some functional concepts in my Python work, but do not use a library for it. Only procedures or functions. No additional dependencies.
- gugagore 6y agoI think `grouby` is a compelling example: https://toolz.readthedocs.io/en/latest/control.html#other-patterns https://toolz.readthedocs.io/en/latest/control.html#other-pa... > Most programmers have written code exactly like this over and over again, just like they may have repeated the map control pattern. When we identify code as a groupby operation we mentally collapse the detailed manipulation into a single concept. If you don't use a library, then you have to re-write something like groupby many times, I would expect. Or WORSE, you don't even use the pattern, writing "code exactly like this over and over again".
- zelphirkalt 6y agoHuh, interesting. This does not remind me of the database groupby operation, but rather of partition, like in SRFI-1. I mean in natural language it is clear to me, why they'd name it groupby, but in programming terms I think partition is more appropriate, as groupby is already "blocked" by the database operation. Often one only needs one of the partitions though, which is when filter is sufficient. Otherwise I guess one can easily write partition oneself and then use that function over and over again, without resorting to a library. But perhaps it is a good example, so that you do not have to write partition in every project and if the additional dependency is OK to have, why not, if it is indeed a good one.
- dragonwriter 6y ago> This does not remind me of the database groupby operation, but rather of partition, like in SRFI-1. I mean in natural language it is clear to me, why they’d name it groupby, but in programming terms I think partition is more appropriate, as groupby is already “blocked” by the database operation. But…this is exactly what a database GROUP BY does. (You’ll always have aggregations in the SELECT clause which work on the data in the groupings, but the GROUP BY itself just specifies splitting the dataset up into this kind of groupings.) > Otherwise I guess one can easily write partition oneself and then use that function over and over again, without resorting to a library. Yeah, literally all a library is avoiding having to rewrite code that someone has already written once.
- zelphirkalt 6y agoAh I see. Perhaps the need to always have an aggregation confused me, which is specific to relational databases (all? most of? some?). Of course the aggregation has to work on something and that might be the same as a partitioning. Thanks for clearing that up! The downside of a library is often, that it comes with its own dependencies and a lot of things you might not need. In general you should not buy into using a library whenever one is available, that among other things offers one procedure, which you need. The decision to use a library should be thought about a little more.
- joppy 6y agoIn some databases like Postgres, you can do something like “SELECT x, array_agg(y) GROUP BY x” to get the exact same effect of this groupby operation in toolz.
- theamk 6y agoYou probably know this, but for the other readers, I'd like to note that "groupby" specifically is part of Python's standard library (in "itertools" module) https://docs.python.org/3/library/itertools.html#itertools.groupby https://docs.python.org/3/library/itertools.html#itertools.g... Don't forget that python is batteries included -- and you can avoid 3rd party dependencies a lot of times.
- joppy 6y agoThe source code for that function (groupby in toolz) is bizarre! Creating a defaultdict where the entries are append-to-list functions, calling them, then going over the dictionary again to extract the underling list objects. Does anyone know what this pattern is for, and why one wouldn’t just create a defaultdict(list)? The function: https://toolz.readthedocs.io/en/latest/_modules/toolz/itertoolz.html#groupby https://toolz.readthedocs.io/en/latest/_modules/toolz/iterto...
- pdonis 6y ago> why one wouldn’t just create a defaultdict(list)? One shouldn't even do that. groupby is supposed to assume that the input is already sorted by the given key, so it can be implemented as a generator. What's more, it should be implemented as a generator (that's the way the Python stlib's itertools.groupby does it), to avoid having to realize the entire iterable at once.
- ciupicri 6y agoThe Toolz version has a different purpose than the one from Python's stdlib (itertools). One is unsorted input, while the other one is for sorted input. The Toolz version is not a replacement and the documentation states > Not to be confused with ``itertools.groupby``
- ciupicri 6y agoIt's the avoiding dots pattern [1]. The idea is that referencing the `append` method in the for grouping loop, i.e. doing d[key(item)].append(item) takes more time than rebuilding the _rv_ groups dictionary with lists instead of `append` methods. Of course, a benchmark should be run and see how much longer the input sequence needs to be than the resulting groups, for this to happen. [1]: https://wiki.python.org/moin/PythonSpeed/PerformanceTips#Avoiding_dots https://wiki.python.org/moin/PythonSpeed/PerformanceTips#Avo......
- archarios 6y agoHell yeah me too! Some of my coworkers like it, but there are a few out there who basically don't want to learn something new.. I have been using Ramda professionally for about two years now and I love the heck out of it. My org is using more and more TypeScript which makes using Ramda a little harder but it still works pretty well in this context too. I have been working on a test data inventory project with Ramda recently and it has made the implementation so much nicer than it would have been otherwise. I started using pipeP (by defining it with pipeWith) and omg it's so great for dealing with a mix of async and non async calls.
- vorticalbox 6y agoAre you using andThen with pipeWith? We have started using types too I've noticed some of the types in @types/ramda are wrong where they only show arrays but it works with strings too like R.startsWith. I might do a PR and update a few when I have time.
- agumonkey 6y agoI've dealt with python noob codebase that had many deep side effects over multiple (~2000loc total) files. The overall logic fit into a single page, bugproof and easy to augment using FP idioms. Yet people want to stick with a "style". Crazy.
- vorticalbox 6y agoExactly, I have many discussions with a front end dev about this sort of thing. Our fe team uses angular so is very class focused. I use classes to but that doesn't mean I can't be functional inside them. It is annoying when devs religiously stuck to a style even if that makes to code base worse
- dean177 6y ago> My colleagues don't like becuase it's so different to what they are used Is it worth being faster if it makes everyone else slower?
- qorrect 6y agoYeah let's all code for the lowest common denominator.
- vorticalbox 6y agoThis does play into my code, we use types so I fully type my code and wrote comments and name functions with a fitting name. I find classes much hard to follow then functional code should they change their styles becuase it makes me slower? I think we all need to play to our strengths and as a team make it so it's as easy as possible to follow code even if it's in a style you don't fully understand.