Arrays.pop() fn: pop element from the array by key and return its value#54
Arrays.pop() fn: pop element from the array by key and return its value#54foxycode wants to merge 1 commit intonette:masterfrom foxycode:master
Conversation
|
The name is terrible because it does something very different from |
src/Utils/Arrays.php
Outdated
There was a problem hiding this comment.
The implementation could be simplified
if (array_key_exists($key, $arr)) { // or maybe isset?
$value = $arr[$key];
unset($arr[$key]);
return $value;
} elseif (func_num_args() < 3) {
throw new Nette\InvalidArgumentException("Missing item '$key'.");
} else {
return $default;
}There was a problem hiding this comment.
I actually tried to stick with conventions and used get function as template.
There was a problem hiding this comment.
get method has its reasons why it must look the way it does, but those reasons are not relevant to this function
There was a problem hiding this comment.
Why not? I see it working same way. If first argument is not array, you get provided default value like in get function. It could be odd to trigger error instead. So I decided is_array actually is important.
|
You are right, but I don't know better name. Pop function is used like this in Python. Maybe we can make it compatible with array_pop by making $key argument optional? |
|
@JanTvrdik Upravil jem implementaci dle připomínek, snad to takto bude vyhovovat. |
|
What about |
|
@dg |
|
|
|
Push & Pop are instructions older than me ;) used to work with the top of stack. How pop is enhanced in Python is interesting, but nowhere else it is used this way. |
|
@dg Should I remove unneeded |
|
No, it is needed. |
|
@dg I don't understand why if FYI: |
|
|
|
@vojtech-dobes Now I see it, thanks for explanation. @dg I rebased to one commit. Should I update method name to |
|
When you will rename it to |
|
I don't like |
|
@xificurk so what do you think that |
|
@dg Well, the term "pick" is used in PHP documentation e.g. here:
Regarding other languages see #54 (comment). Is there any other language that uses |
|
That doesn't seems to me…
|
|
@dg @xificurk Looks like I was wrong. Ruby have parameter in array C++ map have Python: https://docs.python.org/2/library/stdtypes.html#dict.pop Main difference between PHP a Python in this scenario is, Python have separate If function name should be other than |
|
One more thing gentlemans. Main reason I wanted to have public function formSuccess(Form $form, ArrayHash $values)
{
$id = $values->id;
unset($values->id);
$this->someRepository->update($id, $values);
}could be reduced to public function formSuccess(Form $form, ArrayHash $values)
{
$this->someRepository->update($values->pop('id'), $values);
}What do you think about it? |
|
ArrayHash contains no method and pop() doesn't seem to be so important to "pollute" pure class… |
|
ad |
|
@dg Yes, ArrayHash contains no method, so what about adding some more from Arrays, not just one? :-) Ad |
Not really, see
Only C# does return bool and C++ 0/1. |
|
@dg I'd really like to have it in Nette 2.3, so, what should I change? |
|
@foxycode Even if we add |
|
@JanTvrdik I'm ok with |
|
Just idea: what about |
|
As I said, |
|
@dg Should I therefore rename to Or what you see wrong on |
|
I guess I has already lost in this discussion :-) |
|
@foxycode if you want something like this: public static function remove(array & $arr, $key)
{
$value = isset($arr[$key]) ? $arr[$key] : NULL;
unset($arr[$key]);
return $value;
}it is ok, do it. Or do you want something more sofisticated? Pop that is backward compatible with array_pop? Why? Or do you want to use name |
|
@dg I originally chosed I will be satisfied with I can definitely remove |
|
Aha, so $default is for you important, yes? |
|
@dg |
|
@dg Could I do something to push this further? |
5617d88 to
209844d
Compare
I hope this could be good contribution to Array utils. I'm using this all the time.