Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays.pop() fn: pop element from the array by key and return its value #54

Closed
wants to merge 1 commit into from
Closed

Conversation

foxycode
Copy link
Contributor

I hope this could be good contribution to Array utils. I'm using this all the time.

@JanTvrdik
Copy link
Contributor

The name is terrible because it does something very different from array_pop.

}
return $default;
}
return $val;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually tried to stick with conventions and used get function as template.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get method has its reasons why it must look the way it does, but those reasons are not relevant to this function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@foxycode
Copy link
Contributor Author

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?

@foxycode
Copy link
Contributor Author

@JanTvrdik Upravil jem implementaci dle připomínek, snad to takto bude vyhovovat.

@dg
Copy link
Member

dg commented Feb 16, 2015

What about pick?

@foxycode foxycode changed the title Arrays.pop() function: pop element from the array by key and return its ... Arrays.pop() fn: pop element from the array by key and return its value Feb 16, 2015
@foxycode
Copy link
Contributor Author

@dg pop is convention for removing element from array (list, dict, etc.), pick does not sound like for that purpose to me. But me personally does not insist on this specific name.

@@ -236,4 +236,30 @@ public static function normalize(array $arr, $filling = NULL)
return $res;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is missing free line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@vojtech-dobes
Copy link

Arrays::pick sounds very appropriate to me and the pop functionality could be removed from it (to keep users using normal array_pop when "poping" without key is needed).

@dg
Copy link
Member

dg commented Feb 16, 2015

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.

@foxycode
Copy link
Contributor Author

@dg Should I remove unneeded is_array from get function too?

@dg
Copy link
Member

dg commented Feb 16, 2015

No, it is needed.

@foxycode
Copy link
Contributor Author

@dg I don't understand why if $arr type is forced in function arguments. I expect there will be error if you call get with non-array argument?

FYI: pop works like this in ruby too and even C++ map have it this way.

@vojtech-dobes
Copy link

$arr gets rewritten because of recursive going throught the array (if I get it correctly).

@foxycode
Copy link
Contributor Author

@vojtech-dobes Now I see it, thanks for explanation.

@dg I rebased to one commit. Should I update method name to pick and you merge it or can I wait for other comments?

@dg
Copy link
Member

dg commented Feb 16, 2015

When you will rename it to pick probably pop functionality can be removed, as mentioned #54 (comment).

@xificurk
Copy link
Contributor

I don't like pick, because it's not really obvious that it actually removes the item form the array. I'm biased towards pop for the same reasons as @foxycode (usage in other languages like Python).

@dg
Copy link
Member

dg commented Feb 16, 2015

@xificurk so what do you think that Arrays:pick($arr, $key) does? And is pop used this way in other language than Python?

@xificurk
Copy link
Contributor

@dg Well, the term "pick" is used in PHP documentation e.g. here:

array_rand — Pick one or more random entries out of an array

Regarding other languages see #54 (comment). Is there any other language that uses pick get and remove item from an array?

@dg
Copy link
Member

dg commented Feb 16, 2015

That doesn't seems to me…

@foxycode
Copy link
Contributor Author

@dg @xificurk Looks like I was wrong.

Ruby have parameter in array pop function, but for other reason: http://www.rubydoc.info/stdlib/core/1.9.3/Array:pop

C++ map have find and erase methods, pop by key can't be done in one step.

Python: https://docs.python.org/2/library/stdtypes.html#dict.pop

Main difference between PHP a Python in this scenario is, Python have separate dict and list types. PHP combines these two types into one.

If function name should be other than pop, I vote for remove reather than pick, but I don't see problem with pop which is now compatible with array_pop.

@foxycode
Copy link
Contributor Author

One more thing gentlemans. Main reason I wanted to have pop in Arrays was include it in ArrayHash too. The constantly repeating scenario:

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?

@dg
Copy link
Member

dg commented Feb 17, 2015

ArrayHash contains no method and pop() doesn't seem to be so important to "pollute" pure class…

@dg
Copy link
Member

dg commented Feb 17, 2015

ad remove(): it is ok, but expected return value is bool. And remove($key, $default) is strange.

@foxycode
Copy link
Contributor Author

@dg Yes, ArrayHash contains no method, so what about adding some more from Arrays, not just one? :-)

Ad remove: what about cut?

@foxycode
Copy link
Contributor Author

@dg I'd really like to have it in Nette 2.3, so, what should I change?

@JanTvrdik
Copy link
Contributor

@foxycode Even if we add Arrays::remove() it will not work for ArrayHash. I'm not sure there is anything Nette can do for you in this regard.

@foxycode
Copy link
Contributor Author

@JanTvrdik I'm ok with Arrays::remove() too :-) Extend ArrayHash was just a suggestion.

@MartinMystikJonas
Copy link

Just idea: what about takeOut?

@dg
Copy link
Member

dg commented Feb 19, 2015

As I said, remove($key) is for me ok, but remove($key, $default) is weird. Also remove with optional $key.

@foxycode
Copy link
Contributor Author

@dg Should I therefore rename to pick and remove optinal $key? I settle with that.

Or what you see wrong on pop if it is backward compatible with array_pop like this?

@dg
Copy link
Member

dg commented Feb 19, 2015

I guess I has already lost in this discussion :-)

@dg
Copy link
Member

dg commented Feb 19, 2015

@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 pick? You said that you prefer remove? Why now pick?

@foxycode
Copy link
Contributor Author

@dg I originally chosed pop becaue it removes element from array and it is used that way in python. I added array_pop backward compatibility persuant comment from @JanTvrdik

I will be satisfied with pick too, but it doesn't sounds to me like actually removing element from array. I really don't like delete either. What about cut or pull?

I can definitely remove array_pop compatibility, but I'd like to keep default value, is it possible? I rebased commit to name pick and removed array_pop part. If it is ok for you this way, you can merge it :-)

@dg
Copy link
Member

dg commented Feb 20, 2015

Aha, so $default is for you important, yes?

@dg
Copy link
Member

dg commented Feb 20, 2015

@foxycode
Copy link
Contributor Author

@dg $default not so much, but throwing exception when $key does not exists is.

@foxycode
Copy link
Contributor Author

@dg Could I do something to push this further?

@dg dg force-pushed the master branch 2 times, most recently from 5617d88 to 209844d Compare May 21, 2015 11:51
@dg dg closed this in 2d7fea3 Jun 15, 2015
dg pushed a commit that referenced this pull request Jun 17, 2015
dg pushed a commit that referenced this pull request Jun 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants