5 ms·
Personally I think the Python syntax make its more explicit which is an in-place sort vs returning a sorted copy. And also what's a method call vs an attribute
by samwillis 4y ago
Personally I think the Python syntax make its more explicit which is an in-place sort vs returning a sorted copy. And also what's a method call vs an attribute access.
Someone new to Ruby will be WTF is the ! for?
- xigoi 4y agoHow does having a function instead of a method indicate that it's out-of-place? Every Ruby tutorial explains that an exclamation mark means in-place, and this convention is consistent across Ruby libraries, unlike Python.
- aidos 4y agoIt's a convention, quite a nice one, but not all libraries are consistent and it does have different meanings too (! is sometimes used for "raise an error instead of returning a boolean"). In the Python case, `list.sort()` operates on the object, so you can kind of expect it to do mutation (but there's no convention / guarantee). Though it only makes sense as a function that the list object itself implements. `sorted` is a general function that gives you a sorted list from any iterable, and because working with iterables is such a common thing in software, Python has a bunch of standard functions like that. It's nice because you learn them once and they apply absolutely everywhere. Ultimately you learn the behaviours of a language and they become second nature. I find both Ruby and Python very clean and easy to read.
- peoplefromibiza 4y ago> it does have different meanings too the meaning is the same high level: it has side effects, being it a destructive update, an in place operation or throwing an exception. > `sorted` is a general function that gives you a sorted list from any iterable iterable in Python is a very loose concept though. And it's not very consistent too sorted("gheliabciou") ['a', 'b', 'c', 'e', 'g', 'h', 'i', 'i', 'l', 'o', 'u'] you give it a str it returns a list of char in Ruby it makes much more sense "gheliabciou".chars.sort.join "abceghiilou" You take out the chars from the string, sort them and then join them together again because a string cannot be sorted, sorting a string makes no sense, you sort the chars composing it. Also, Python could not do this for many years, while Ruby could no prob sorted("gheliabciouß⌚⌨⌛🆔🉑🈶🈚🈸🈺🈷🆚🉐㊙㊗🈴") edit: Python also shows some very counterintuitive semantic take the example above, to return a string you use ''.join(sorted("gheliabciou")) 'abceghiilouß' which is probably the last thing someone would think of. sorting a list does not produce any output, because sort is _always_ in place, so you have to waste a variable to sort a list because you can't sort it inline list("fedcba") ['f', 'e', 'd', 'c', 'b', 'a'] print(list("fedcba").sort()) None # <-- where did the list go? # you have to do l = list("fedcba") l.sort() print(l) ['a', 'b', 'c', 'd', 'e', 'f'] again, highly unexpected my favourite print([c for c in "bca"].sort()) None # <- WTF? list comprehension is a functional concept! # this is how you "intuitively" do it print(''.join(sorted([c for c in "cba"]))) abc
- nindalf 4y agoYou can use emoji in HN comments? Emojis get automatically removed from mine. Edit: ok, some do and some don’t. 🈶 is allowed but :laugh_with_tears: isn’t. I guess there’s a specific list of emojis that are getting stripped.
- saagarjha 4y agoThere are specific blocks that are filtered out.
- peoplefromibiza 4y agofor reference ;) https://news.ycombinator.com/item?id=19482991 https://news.ycombinator.com/item?id=19482991
- deleted 4y ago[deleted]
- aidos 4y agoLots of objects don't have a natural way to return a sorted version of themselves, especially inplace (what's the reverse sort of a range, for example). You get the same in Ruby, as you've just demonstrated. Sorting into a list is the only thing that makes sense in all cases. In that way `sorted` _is_ totally consistent - it always takes an iterable and returns a list of the elements. If we can't agree on that then I don't think there's much of a discussion to be had here.
- peoplefromibiza 4y ago> In that way `sorted` _is_ totally consistent - it always takes an iterable and returns a list of the elements except Iterable is the wrong "interface" in Python it is defined as > An iterable is any Python object capable of returning its members one at a time, permitting it to be iterated over in a for-loop. which is not always what you want for sorting quicksort doesn't work one element at a time so basically you have to extract all the elements before sorting them what about infinite streams? At least Ruby Enumerable mixins explicitly states that > The Enumerable mixin provides collection classes with several traversal and searching methods, and with the ability to __sort__. Another problem with sorted is that it accepts any iterable, true, returning a list of items, but it is not clear how you can tell if the object is an iterbale or not >>> sorted(123) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not iterable ok >>> isinstance(123, iterable) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'iterable' is not defined mmmm maybe it's because int is not iterable? >>> isinstace("abc", iterable) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'iterable' is not defined so the solutions are # fails for strings in Python 2 >>> try: _ = iter(some_object) except TypeError as te: print('is not iterable') or # if 2.6 >= Python < 3.3 from collections import Iterable # if Python >= 3.3 from collections.abc import Iterable # <- personal note, abc stands for...? core maybe? # no idea on how to do it in Python < 2.6 >>> isinstance("abc", Iterable) True not only it's very ugly, but it can also fail in mysterious ways. As per documentation Checking isinstance(obj, Iterable) detects classes that are registered as Iterable or that have an __iter__() method, but it does not detect classes that iterate with the __getitem__() method. The only reliable way to determine whether an object is iterable is to call iter(obj). while in Ruby it's straightforward [3] pry(main)> [1, 2].is_a? Enumerable => true [4] pry(main)> "abc".is_a? Enumerable => false [5] pry(main)> "abc".chars.is_a? Enumerable => true
- laserlight 4y agoNewcomers to Python will have many more WTFs, many of which are syntactically invisible.
- consp 4y agoI have (had) this for all languages, including ruby. No two people are alike and since it's all a "I think this is best" approach (either by person or committee) you will always have some form of it. Unless you use brainfuck, maybe.
- pizza234 4y agoWhen i learned Ruby, it wasn't really a WTF for me - I just learned that '!' methods are destructive, while non-'!' aren't. However, the destructiveness semantic has been significantly overloaded over the time, so each Ruby (Rails) project probably interprets them in a different way, and it ends up having arbitrary semantics. I'm used to it, but I personally think that for the reason above, it's not a good syntax/semantic to have in a language.
- michaelteter 4y ago> arbitrary semantics I wouldn't go so far to say it is arbitrary (unless someone uses it wrongly :) ). As far as I know, a bang name only exists if a non-bang name exists as well. And the one with ! indicates an unexpected or comparatively dangerous outcome, such as a mutation, raising an exception, bypassing some normal check, etc.
- bobbylarrybobby 4y agoBut random.shuffle also operates in place despite being a free function
- michaelteter 4y ago> WTF is the ! for? This takes so little time to learn. Same with ?. While ! is not always the same meaning, it exists when there is a non-bang version; and it means "watch out, this does some sh*t". Maybe it means mutating the source, or maybe it means "doesn't take the usual path", as with Active Record stuff (raising an exception if validations fail on save instead of just returning false). And the ? is a godsend, as you can clearly indicate functions which return a boolean (or intentionally truthy) value. As naming functions is a chore, and one which can have significant impact on code readability later, the ! and ? have good value when used properly. For example, Python's sorted() vs list.sort(). If you are new to Python, and you see sorted(list), you may wonder if that is telling you if the list is sorted or sorting the list. It's actually also very awkward, because most functions are named as their actions in present tense, not in past tense. The built-in Python function "sorted" really should be "sort". In the list of Python built-in functions, you see present tense verbs like slice(), sum(), round(), etc. Those are inconsistent with sorted() and reversed(). When we see a past tense verb, it feels like a question. With Ruby, if it's a question, it ends with a ?. BTW, why the hell is Python sorted() and reversed() not just called sort() and reverse()? And while I'm ranting about inconsistencies, Python my_list.sort() mutates my_list, while sorted(my_list) doesn't. In Ruby, my_list.sort returns my_list sorted, without mutating. my_list.sort!. That is consistent and concise, and the bang warns you that it is destructive.
- unethical_ban 4y agoI had to login to respond to this. To me, the ! and ? syntax in Rugby is genius. It is a language wide self documenting tool to explain a dangerous function and a boolean, which make total sense.