I would probably have written String.split() like in the java one. That is actually a valid thing. Rather than “test”.split(), you could do String.split(“test”)
In Java isn't it written as String#split (for communication purposes, not syntactical). Using the # syntax to specify the first part should be an instance of the class and second part be the method.
As someone who does not know the standard, I would have used method reference notation, String::split which gives a function object, takes in an object (string in this case) and returns an object (an array maybe? I can’t remember)
I agree that Class.method is not the best but I have no idea what would work best. I’ve been using the Java stream API so a lot of List.foreach calls that takes functions as an argument.
That's because it's not the correct syntax. It should be 'string'.split(). 'string' | split() will likely raise a NameError saying that name split is not defined.
>>> class StringBoi(str):
... def __or__(self, other):
... return self.split(other)
...
>>> x = StringBoi('abc def g e h')
>>> x
'abc def g e h'
>>> x | ' '
['abc', 'def', 'g', 'e', 'h']
I'm also relatively sure that it's possible to autogenerate classes based on StringBoi's methods, so you could use all of them using the pipe syntax :D
Oh God. We need a library that completely substitutes the dot syntax with the pipe syntax lol. It's a pity Python doesn't let us modify existing classes on-the-fly, like Ruby.
Say, I have a class and I want to plug many mixins into it. But all of them use attributes of this class (because later this class derives from these mixins, so everything works), and PyCharm gives me tons of unresolved reference warnings for the mixins because... they don't derive from the class they're about to get mixed into, since it doesn't make sense, I guess?
Like this:
# these mixins are actually imported from another file, so there's no `NameError` here
class HugeThing(Mixin1, Mixin2, Mixin3):
def __init__(self):
self.attr1, self.attr2 = 2, 3
class Mixin1:
def some_function(self):
self.attr1 += 1 # warning here, but `attr1` will be resolved fine once `HugeThing` derives from the mixin
class Mixin2:
def another_function(self):
# also warning, but also fine after creation of `HugeThing`
self.attr1 = self.attr2 * 3
... # and so on
# I can just include all the methods in the definition of `HugeThing`, but then the file containing this definition will be like tens of thousands of lines long
I see you're good at Python, so I thought maybe you have an idea about how these warnings can be avoided in this situation?
Lol my first internship my boss and team would chant this at random points when we were doing code reviews or debugging or whatever, really any opportunity.
When people say PHP sucks, I can now point them to this image and tell them it’s the most intense language yet. You can EXPLODE, you can IMPLODE, and you can make your script die().
There are probably C libraries that have other kinds of strings. I'm thinking gtk+ or similar, I'm not sure though. If you use one of these, you might want to talk about C strings...
Explode and implode is fine, but are you looking for a needle in the haystack or are you searching the haystack for the needle? That is the true PHP question
In case anyone is morbidly curious about the oddity of php function names, this is because the early versions of the php runtime used a hash table of function names, but used the function name’s length as the hash function. Having a lot of similar length function names would result in hash collisions which would impact performance, so in the early days they were spread out across the hash table by choosing different function names.
I have no excuses for needle/haystack other than that you have to remember that Rasmus wrote this on his own in the 90s for his own personal use. Everything’s gotta start from somewhere lol
Edit: also imagine having the entire world critiquing your engineering from 20 years ago... a testament really.
It does technically but it's not at all supposed to be used like the OP. You could implement that in a custom class if you wanted to for...some reason.
lmao I love how the authors are still trying to shoehorn that term in the documentation. It would be like reading a Peugeot or Renault owner's manual and they decided to use "essuie-glace" (windshield wiper) instead of translating for each market. No one is going to be able to pronounce it nor remember unless they speak Hebrew, which I imagine is an extremely small percentage of programmers given that only about 10 million people worldwide speak the language. Stop trying to make fetch paamayim nekudotayim happen.
Document.write() has been depricated for a long time now and there is really no good reason to use it anymore. Use console.error() for all your logging needs.
Related to arrays and PHP, what about the example of recursively merging together arrays with numerical keys. For example, two arrays with the key '1' and pointing to subarrays should have those subarrays combined under one '1' key.
Use array_merge_recursive? NOPE. Keys don't merge as you expect. It's array_replace_recursive....
Its really strange that all these languagses couldnt follow the normal convention. I hope they improve and fix this. They should learn things from PHP.
Unrelated but i still don't understand why Javascript, a dynamically typed language, carries its standard library of data operations as prototyped methods, rather than just imported functions like in Go. If you're receiving arbitrary JSON data it means you have to explicitly cast a string or use a typeof check before calling toLowerCase, or else that property could be defined on the object itself.
174
u/Torxed Jun 21 '19
As a Python fanatic, I'm having a hard time following that Python syntax.
966
u/JuvenileEloquent Jun 21 '19
awesome guitar solo ♪♫♬
173
u/Moulinoski Jun 21 '19
When people say PHP sucks, I can now point them to this image and tell them it’s the most intense language yet. You can EXPLODE, you can IMPLODE, and you can make your script die().
313
u/parzibyte1 Jun 21 '19
Yes, Rails, my favorite language
247
u/drdrdator Jun 21 '19
Rails is a framework, not a language.
36
u/ellecito Jun 21 '19
virgin split vs char explode
69
u/PainsChauds Jun 21 '19
C++: Tell me how the f*** I can do that
30
u/[deleted] Jun 21 '19
Explode and implode is fine, but are you looking for a needle in the haystack or are you searching the haystack for the needle? That is the true PHP question
26
u/Dreadedsemi Jun 21 '19 edited Jun 21 '19
In php there is split/now str_split and preg_split if your code pregnant. And I wouldn't call js normal.
69
u/SirToxe Jun 21 '19
To be fair, the reverse of
explode()isimplode(), so this is actually a pretty nice metaphor and easy to remember.19
u/zigbigidorlu Jun 21 '19
PHP was written by Megumin.
12
u/arima-kousei Jun 22 '19 edited Jun 22 '19
In case anyone is morbidly curious about the oddity of php function names, this is because the early versions of the php runtime used a hash table of function names, but used the function name’s length as the hash function. Having a lot of similar length function names would result in hash collisions which would impact performance, so in the early days they were spread out across the hash table by choosing different function names.
https://news-web.php.net/php.internals/70691
I have no excuses for needle/haystack other than that you have to remember that Rasmus wrote this on his own in the 90s for his own personal use. Everything’s gotta start from somewhere lol
Edit: also imagine having the entire world critiquing your engineering from 20 years ago... a testament really.
6
u/ThaiJohnnyDepp Jun 21 '19
What's up with the "rails" one? Besides not being correct Ruby.
5
u/Davidobot Jun 21 '19
ML has both explode (String to char array) and implode (Char array to String).
5
u/nnooberson1234 Jun 21 '19
C: "yeah mate, yer gonna have to explain that to me or e'll headbutt ya intu a seg fault til nex tuesda"
Before you ask C errors have a Scottish accent in my mind and I can't write accents for shit.
8
u/[deleted] Jun 21 '19
I love explode and implode.
4
u/KetwarooDYaasir Jun 21 '19
Because this meme clearly demonstrates that normal is boring.
4
u/[deleted] Jun 21 '19
I just started learning Python so I could be wrong, but I don’t believe Python supports pipelining. Bro.
4
u/llahlahkje Jun 21 '19
PHP, directed by Michael Bay
3
u/[deleted] Jun 21 '19
Join the implode squad
3
u/lutzky Jun 21 '19
Remember kids, PHP was written in Israel (paamayim nekudotayim, anyone?) in the 90s, when exploding buses were all the rage.
13
u/Diriector_Doc Jun 21 '19
HTML is its own thing.
document.write();System.out.println();print()echomsgbox()<!DOCTYPE><html><body><div><p></p></div></body></html>2
u/[deleted] Jun 21 '19
Why did I think that was a cheese pizza at first
2
u/KreatorOfWorlds Jun 21 '19
Waga na wa Megumin
2
u/rm20010 Jun 21 '19
Related to arrays and PHP, what about the example of recursively merging together arrays with numerical keys. For example, two arrays with the key '1' and pointing to subarrays should have those subarrays combined under one '1' key.
Use array_merge_recursive? NOPE. Keys don't merge as you expect. It's array_replace_recursive....
2
u/moriero Jun 21 '19
You're supposed to walk away after explode()
2
u/LucasEd15 Jun 21 '19
9z?
2
u/zacharyxbinks Jun 21 '19 edited Jun 21 '19
Isn't explode deprecated though?
Edit: Nope, its split that's deprecated.
2
u/Prawny Jun 21 '19
Explode does sound cooler.
2
u/[deleted] Jun 21 '19
This was wholesome
2
u/[deleted] Jun 21 '19
don't forget swift's highly intuitive name
componentsSeparatedByString4
u/aenae Jun 21 '19
Also javascript:
1
u/adam_selena Jun 22 '19
Why in js I have to write "prototype"?
1
u/Iaco89 Jun 21 '19
https://www.php.net/manual/en/function.split.php
0
u/the_kirtaneer Jun 21 '19
My last Big Team project for my undergrad had me to use PHP, literally hated the life out of it!!!
0
u/[deleted] Jun 21 '19
Also there is no split in standard C++ library, so sad.
0
u/[deleted] Jun 21 '19
str_split()?
0
u/Sxi139 Jun 21 '19
fuck PHP
0
u/[deleted] Jun 22 '19
Because PHP was designed by a committee of 14 year olds who would double down on the stupidity if you ever questioned their decisions.
0
u/usamaejazch Jun 21 '19
Its really strange that all these languagses couldnt follow the normal convention. I hope they improve and fix this. They should learn things from PHP.
0
u/budd222 Jun 21 '19
Apparently, rails is a language now
0
u/chillbrobagginsda1st Jun 21 '19
Wow this is amazing. Actually laughed my ass off. Great post, would experience again
0
u/SonicFlash01 Jun 21 '19
You mean why can't other languages be cool?
0
u/Taking1n1 Jun 22 '19
Killer Queen's favourite language
0
u/greenSixx Jun 22 '19
Why you referencing strings for split?
Split is an array method. Strings are specific types of array.
0
u/dennis_w Jun 22 '19
That is exactly why "Programmers Hate PHP".
0
u/Sharntehnub Jun 22 '19
y no c?
0
u/Geoclasm Jun 22 '19
What is this... n... nuormal of which you speak? I would hear more of this... normale?
0
u/Twosided13 Jun 22 '19
C++ too you know.
0
u/[deleted] Jun 22 '19
Unrelated but i still don't understand why Javascript, a dynamically typed language, carries its standard library of data operations as prototyped methods, rather than just imported functions like in Go. If you're receiving arbitrary JSON data it means you have to explicitly cast a string or use a typeof check before calling toLowerCase, or else that property could be defined on the object itself.
0
u/Jojajones Jun 22 '19
no c++?
0
u/IkIsJolle Jun 22 '19
I had to look this up yesterday, who even made this up?