4 ms·
That was a really interesting introduction. I hadn't looked at Rust before and that gave me a good feel for it. Thanks for putting it together, Armin. A minor
by aidos 11y ago
That was a really interesting introduction. I hadn't looked at Rust before and that gave me a good feel for it. Thanks for putting it together, Armin.
A minor peeve of mine is examples between languages that differ in ways that are unimportant (though maybe it can just justified as "idiomatic code"). It adds bias to the demonstration.
We could have written the python one like the following to wrap the lock up with the results structure (and get the automatic sorting behaviour, too):
import queue
from threading import Thread
def fib(num):
return 1 if num < 2 else fib(num - 2) + fib(num - 1)
def thread_prog(results, i):
rv = fib(i)
results.put((i, rv))
def main():
results = queue.PriorityQueue()
threads = [Thread(target=thread_prog, args=(results, i)) for i in range(35)]
[t.start() for t in threads]
[t.join() for t in threads]
while not results.empty():
i, rv = results.get()
print("fib({}) = {}".format(i, rv))
Though, I've also taken other liberties to make it shorter. I normally stay away from threading in python because the GIL makes it a bit of a waste of time in most cases, and who needs the complexity in their lives?
Again, thanks for the overview, I enjoyed it.
- the_mitsuhiko 11y agoYou can also use a locked data structure in Rust. This was to show how to actually use mutexes. In theory (assuming a Python with GIL or locked dicts) you can get rid of the mutex as well. But this only works for such a simple example in any case. The mutex generally works and was meant to show the relationship of the borrow checker and the mutability system to the mutex that moves checks to runtime.
- aidos 11y agoOk cool, I figured that was the case. I mostly sat down to write out the example to humour myself during my lunch break (as I said, I don't do much with threads so wanted to make sure I understood what was going on). The ownership/borrowing system is really interesting, not something I'd seen before - though I guess other languages do it too?
- DasIch 11y agoOwnership/borrowing enforced at compile time doesn't exist in any other (practical) language. Though C++ has some runtime support for it and in C you would/should do it by convention.
- scott_s 11y agoConsider that such examples are often not about convincing, but teaching. That is, they often not trying to convince the reader, "Language X is better than language Y." Rather, the intent is more, "You already know language X, so here's some code in X. Now here's how you would do the same thing in Y." It's a pedagogical tool, rather than an argument tactic.
- Dewie3 11y agoAccording to some people all mentions of PLs have to be evangelical and confrontational. :-)
- scardine 11y agoSome people consider an abuse of list comprehensions if you use them just for the side effects. Why not just a plain for loop? for t in threads: t.start()
- aidos 11y agoBelieve it or not, it's the first time I've used a list comprehension for execution like that (other than when working interactively in ipython)! Originally I had a comment about how it was a non-idiomatic thing to do, but I deleted it. What's strange is that I didn't even consider writing the for loop on a single line. I just never do that. It's clearly better in this case (than a list comprehension). In real code I'd just do it on 2 lines.
- Phrodo_00 11y agoYeah, I also found weird that he'd check the type of the addend in the adding example, he just talked about protocols, and that negates their use, and instead forces inheritance.