Jump to content
sarogahtyp

apply or forEach (or maybe count)?

Recommended Posts

Hey guys,

I just crawled the wiki for usefull commands and found apply in it. Correct me if Im wrong but I think its doing nearly the same as forEach if u not need that _forEachIndex variable.

 

Is there another difference which i ve not seen and did anybody measure which command is faster?

Share this post


Link to post
Share on other sites

As far as I can tell there seem to be an extremely minimal difference, with apply being 0.001 ms faster, with this code:

{_x setVelocity [0,0,5]} count [HURON, Kart1, kart2, Quad1, Quad2];
[HURON, Kart1, kart2, Quad1, Quad2] apply {_x setVelocity [0,0,5]};
  • Like 1

Share this post


Link to post
Share on other sites

The purpose of apply is that it returns the resulting array, while forEach and count don't.

  • Like 4

Share this post


Link to post
Share on other sites

Apply is inherited from a concept in functional programming, namely functor. Normally it's called map

 

http://learnyouahaskell.com/making-our-own-types-and-typeclasses#the-functor-typeclass

 

It takes a function from one type to another and a list of one type and returns a list of another type

 

Or in SQF "it takes an array and code, applies code to each element and returns a new array"

E.g

_playerUIDS = allPlayers apply {getPlayerUID _x}; // ["12345"; "54321"]

It can also be chained which is where it's real power comes in, in combination with select (which is often called filter in programming languages)

_alivePlayersUID = allPlayers select {alive _x} apply {getPlayerUID _x}; // apply {} select {} apply {};

This can be done indefinitely. 

So no, apply is not like forEach and count. If you're not interested in the resulting array, use count or forEach

  • Like 1

Share this post


Link to post
Share on other sites

thanks a lot for ur responses.

Now I think I ve better understanding of the differences of those commands.

 

@cuel

could u explain this line a little bit more please?

_alivePlayersUID = allPlayers select {alive _x} apply {getPlayerUID _x}; // apply {} select {} apply {};

I ve problems to track the processing order.

But Ill try it :-)

Correct me if I m wrong.

that line first creates an array of all players (from allPlayers) which r alive and then apply get those players UID and throws it in the array _alivePlayersUID.

 

should be the same as:

_alivePlayersUID = allPlayers apply {if(alive _x) then {getPlayerUID _x};};

Share this post


Link to post
Share on other sites
could u explain this line a little bit more please?

_alivePlayersUID = allPlayers select {alive _x} apply {getPlayerUID _x}; // apply {} select {} apply {};

I ve problems to track the processing order.

But Ill try it :-)

Correct me if I m wrong.

that line first creates an array of all players (from allPlayers) which r alive and then apply get those players UID and throws it in the array _alivePlayersUID.

 

should be the same as:

_alivePlayersUID = allPlayers apply {if(alive _x) then {getPlayerUID _x};};

That's right.

The old way before apply and the current select was usually

_alivePlayersUID = [];
{
	if (alive _x) then {_alivePlayersUID pushpack (getPlayerUID _x)}
} foreach allPlayers;
  • Like 1

Share this post


Link to post
Share on other sites

thanks a lot for ur responses.

Now I think I ve better understanding of the differences of those commands.

 

@cuel

could u explain this line a little bit more please?

_alivePlayersUID = allPlayers select {alive _x} apply {getPlayerUID _x}; // apply {} select {} apply {};
I ve problems to track the processing order.

But Ill try it :-)

Correct me if I m wrong.

that line first creates an array of all players (from allPlayers) which r alive and then apply get those players UID and throws it in the array _alivePlayersUID.

 

should be the same as:

_alivePlayersUID = allPlayers apply {if(alive _x) then {getPlayerUID _x};};

Do both return the same results? Dead easy to verify, and there will be your answer.

Share this post


Link to post
Share on other sites

Do both return the same results? Dead easy to verify, and there will be your answer.

 

Thats true but I cant do that at work. I just try to understand things better without testing it.

Share this post


Link to post
Share on other sites

Do both return the same results?

 

When players 2 and 4  not alive:


allPlayers select {alive _x} apply {getPlayerUID _x}; //result is  [Player_1_UID, Player_3_UID, Player_5_UID]


allPlayers apply {if(alive _x) then {getPlayerUID _x};}; //result is  [Player_1_UID, null, Player_3_UID, null, Player_5_UID]
  • Like 2

Share this post


Link to post
Share on other sites

 

thanks a lot for ur responses.

Now I think I ve better understanding of the differences of those commands.

 

@cuel

could u explain this line a little bit more please?

_alivePlayersUID = allPlayers select {alive _x} apply {getPlayerUID _x}; // apply {} select {} apply {};

I ve problems to track the processing order.

But Ill try it :-)

Correct me if I m wrong.

that line first creates an array of all players (from allPlayers) which r alive and then apply get those players UID and throws it in the array _alivePlayersUID.

 

should be the same as:

_alivePlayersUID = allPlayers apply {if(alive _x) then {getPlayerUID _x};};

 

Not really, select is used to filter array elements, while apply is normally used to transform the array elements into something else. Select can return less elements while apply will ways return the same amount of elements

 

What I wrote can be explained like this

_plrs = allPlayers;
_alivePlrs = _plrs select {alive _x}; // we filter out dead players. the resulting array can be smaller
_alivePlrsUID = _alivePlrs apply {getPlayerUID _x}; // returns an array of playerUids for each element in the _alivePlrs
  • Like 1

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×