4 ms·
I feel like most of his Day 0 functions should be in Python's standard library (groupby, first, firstrue (e.g.: detect), counttrue (e.g. count with block in rub
by dorianm 10y ago
I feel like most of his Day 0 functions should be in Python's standard library (groupby, first, firstrue (e.g.: detect), counttrue (e.g. count with block in ruby), trace, etc.)
- tedmiston 10y agoMost of them are only the slightest sprinkle of sytactic sugar over one-liners commonly used for puzzle problem solving but not real-world programming in Python.
- xapata 10y agoGotta be careful of that groupby. Using defaultdict has the pitfall that it continues to insert new keys when looking up missing keys after you're done grouping. Sometimes it's better to use a regular dict and setdefault during the grouping, ensuring that, after the grouping, missing lookups will properly raise KeyError. In fact, AFAIK, Norvig made that specific mistake previously.
- js8 10y agoSome of those are actually in itertools package. In general, Python doesn't seem to include a function in its standard libraries if you can write it as an obvious combination of 2 or 3 existing functions or sequence comprehensions. Which is a good thing, you don't want to cognitively overload the users with too many similar building blocks.
- rubber_duck 10y ago>Which is a good thing, you don't want to cognitively overload the users with too many similar building blocks. On the other hand if you've spent some time in a different high level language doing stuff like this in python feels too pedestrian.
- js8 10y agoNot sure what your point is - it may feel pedestrian, but consider the alternative. Let's say, to "save users time" you decide to include all the meaningful combinations of some basic functions into your standard library. Then selecting from all those functions is about as time consuming than combining the two basic functions, unless you have memorized all of them by heart. And then, what can also happen, if you haven't got them all memorized, you actually look up for a certain combination, only to find that particular one is for whatever reason missing from the library and you still have to compose it with the other functions you have. The point is, there is a trade-off between time saved by having functions in standard library and time spent looking those functions up. So you don't want to have functions that are obvious combinations of 2-3 function, unless it's something very very common (e.g. a map function as a combination of monadic bind and return).
- rubber_duck 10y ago>there is a trade-off between time saved by having functions in standard library and time spent looking those functions up. Meh, difficulty of writing such code is irrelevant in comparison to how these higher level functions read and compose, consider : next(x for x in seq if x > y, None) vs. first(seq, lambda x: x > y, None) seq.first(lambda x: x > y, None) the intent is very clear in the second example, it just has useless implementation details in the first. I haven't done python in a while but every time I go back to it I find such edge cases where the code ends up being obfuscated or you write one-off functions that you need in random places and should be in std. Contrast with Clojure for eg. with it's rich collection manipulation standard library - data manipulation is high level and expressive
- xapata 10y agoToo many small functions means name collisions. Names like "product" are precious real estate.
- choppaface 10y agoIt's funny seeing how Norvig implements some of these functions. I've seen a few of these used as Google interview questions and Norvig's solutions would not pass the hiring bar ...
- Kiro 10y agoWhy?
- kwhitefoot 10y agoThat's just slander unless you tell us why.
- kutkloon7 10y agoOf course. It would be idiotic to expect 'code of advent' solutions to pass as production code, especially if you take into account the time pressure for submitting these solutions.
- taeric 10y agoI get what you are saying. However, the comparison is to whiteboard interview questions. If these don't pass your bar... Think long and hard about what this implies about who you would not hire.
- deleted 10y ago[deleted]