3 ms·
I think we have reached the depth of discussion threads on HN, so i'd like to thank you and tzs for your comments and this (imo) good discussion. The use of ma
by buster 15y ago
I think we have reached the depth of discussion threads on HN, so i'd like to thank you and tzs for your comments and this (imo) good discussion.
The use of map is nice, BUT it shows partially what is wrong:
1. there are too many non-obvious ways to do simple things (this means that out of 10 programmers you will typically see 10 different solutions to a simple problem)
2. i have a problem with using functions for "different" usecases as what their name implies.. in my world eval simply evaluates code and map is short for "apply function to every item" not "filter out an item"
- i hate doing "go over every item in a list and execute a function that returns the item if it is not 'dogfood' and return that new list, overwriting the original one" when all i want is "delete 'dogfood' from my list"
As good as your answers were, you've shown me some good points and that your community indeed is very helpful, i am not convinced of this language. Both of you said yourself that there are quirks in the language. "still try to decide what exceptions look like" after how many years of perl? That was my thought ;)
At the end of the day i will still try to find and fix bugs in other peoples perl code, curse frequently and hope that people move on to the next language..
- Mithaldu 15y agoI do appreciate the grace you've shown in replies. > 1. there are too many non-obvious ways to do simple things To a person who learned Lisp first your pythonish ways would be similarly non-obvious. Obviousness depends always on previous knowledge and you seem to know a lot about python; and as such expect things to operate like there. Consider how confused a foreigner would be when he hears the word "bowling" and does not know about the sport and thinks it has to do with bowls. > map is short for "apply function to every item" not "filter out an item" - i hate doing "go over every item in a list and execute a function that returns the item if it is not 'dogfood' and return that new list, overwriting the original one" when all i want is "delete 'dogfood' from my list" But that is what filter does. Literally, there is no other way to implement this, unless you're operating with linked lists, which are not arrays. Filter is a map with a negated search func - wait, i'm an idiot. it should've been grep. But yeah, filter is just a grep with a negated search function. @list = grep { $_ ne 'dogfood' } @list; Let me ask you a question though. In Perl, if i have a list of objects representing links and i want to remove all SSL links, i can do this: @links = grep { $_->protocol ne 'https' } @links; How would i implement this in python? Does array.remove() accept lambdas as parameter? Or would i need to go another way? > At the end of the day i will still try to find and fix bugs in other peoples perl code, curse frequently Good luck and hop by on IRC! It will increase your curse frequency a LOT. (Until you meet mst and start to pick things up from him.)
- buster 15y agoI'll visit that channel when back from vacation for sure. To do what you want you could use list comprehensions or the filter(function, sequence) function: List comprehension: links = ["https://securesite.com", "http://unsecuresite.com", "https://sec2.com", "http://unsec2.com"] [url for url in links if not url.startswith("https")] >>> ['http://unsecuresite.com', 'http://unsec2.com'] filter() + lambda: links = ["https://securesite.com", "http://unsecuresite.com", "https://sec2.com", "http://unsec2.com"] filter(lambda url: not url.startswith("https"), links) ['http://unsecuresite.com', 'http://unsec2.com'] I'd go with the list comprehension, from your use of map i'd guess you'd go with the filter ;) Of course you'd replace the url.startswith with whatever your criteria is.
- Mithaldu 15y agoAh, that's kinda what i expected. Suddenly the two languages look fairly similar, no? :) Also, list comprehension doesn't look bad to me. It's just a slightly weird and more verbose syntax of doing map, while filter does an in-place operation.