4 ms·
What's wrong with (','.join(['a', 'b']))? If it's the order of arguments, then I want to point out that the order has two big advantages: * partial application
by creata 6y ago
What's wrong with (','.join(['a', 'b']))? If it's the order of arguments, then I want to point out that the order has two big advantages:
* partial application is actually useful: I've actually used (','.join) before, but (lambda sep: sep.join(['a', 'b'])) seems relatively useless. That's why Haskell also uses this order with (intercalate sep listOfStrings).
* it accepts any iterable, rather than just lists.
- leephillips 6y agoWhat strikes me as weird about this is that our intent is to do something to the array of strings, so we are looking for a function that takes this array and transforms it into something else. But we are obligated to invert our intuition here, and write this as if we are doing something to the punctuation that we want to insert between the array elements. It’s backwards, and causes us (me, anyway) to keep forgetting how to use this method. I must have gotten this wrong a dozen times. The sensible way would be something like join(array, separator). That is straightforward, and would be hard to get wrong. It could even be written in a way where the order of arguments didn’t matter, so you wouldn’t even have to remember that. Why is the separator privileged as the implicit first argument of `join`? I accept that you’ve identified some advantages of the Python version; it’s always possible to find cases where object.method() notation gets you something.
- hakuseki 6y ago> It could even be written in a way where the order of arguments didn’t matter Well, 'hello'.join(', ') does not produce the same output as ', '.join('hello'), so the order of the arguments does matter.
- creata 6y agoPerhaps they want named arguments so that join(sep=', ', strings='hello') and join(strings='hello', sep=', ') are equivalent.
- leephillips 6y agoOK, but I was thinking along the lines of how easy it is in (for example) Julia to make methods that dispatch on types, so that your function could examine the arguments, and if one of them happens to be a single character, and the other a string or array, do the sensible thing, regardless of the order in which you typed in the arguments.
- blt 6y agoOOP interface is wrong for this functionality. It should be a free function. There is no reason it should be restricted to strings -- "join" makes sense on any sequence-like objects. We should be able to join(Iter1, Iter2[Iter3]) where Iter* are all arbitrary iterable data types.
- creata 6y agoI agree: you can definitely make it generic in both arguments. I was just arguing that this order of arguments makes more sense.