generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: What is "i, v in next (insert variable) do" mean?

Previous Thread :: Next Thread 
breifmanny is online. breifmanny
Joined: 23 Dec 2011
Total Posts: 341
25 Dec 2016 11:28 PM
title...


I know what i, v in pairs does but not i, v in next does. Can anyone clear this up for me or refer to a wiki page about this for me? I can't find any.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Dec 2016 11:32 PM
The short answer: it iterates over a table like pairs does.
Report Abuse
Pejorem is not online. Pejorem
Joined: 10 Apr 2012
Total Posts: 637
25 Dec 2016 11:37 PM
next is a keyword specific to tables I believe. Cba look into it but for moving to the next key


Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
26 Dec 2016 12:03 AM
nope/
Report Abuse
Pejorem is not online. Pejorem
Joined: 10 Apr 2012
Total Posts: 637
26 Dec 2016 12:04 AM
mk... xD


Report Abuse
Pyrondon is not online. Pyrondon
Joined: 01 Sep 2013
Total Posts: 81
26 Dec 2016 01:15 AM
In the snippet `for i, v in next, t do`, `next` is an iterator function. What that means is it's a function which the for loop uses to iterate through the table, t.

When you do `for i, v in pairs(t) do`, you're really doing the exact same thing; all `pairs(t)` does is return next and t. `pairs` would be referred to as an "iterator generator" function for this reason.
Report Abuse
Sen_Takatsuki is not online. Sen_Takatsuki
Joined: 07 Aug 2013
Total Posts: 673
26 Dec 2016 01:20 AM
To simplify what was stated above...


"for index, value in next, (TABLE) do end" is the same as "for index, value in pairs(TABLE) do end"

They are both iterator functions - "pairs" calls the "next" iterator, so technically speaking - in a very small decimal numbers, the "next" iterator is slightly faster. This wouldn't be noticeable unless you were trying to get the difference from a very large table, etc.


#code Signature_mod = require(DataModel['sm_Data']) if Signature_mod['xThe..']['RbxDev_Info'] ~= 'Too Lazy To Try Again After A Year' then print('You finally did it lol') end
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
26 Dec 2016 01:23 AM
It's not the exact same. And pairs doesn't call next, so you're wrong.
The pairs call is evaluated BEFORE the loop starts, much like when you do "for i = 1, x+3" the "x+3" is evaluated _before_ the loop starts.

Essentially:
for i, v in pairs(tbl) do

Calls pairs(tbl), pairs returns an instance of luaB_next (that is, pairs(x) == pairs(y) but pairs(x) ~= next as it's a different Lua C closure) so the ONLY thing different between using pairs vs using next directly is a SINGLE FUNCTION CALL BEFORE EVERYTHING HAPPENS.
Report Abuse
Sen_Takatsuki is not online. Sen_Takatsuki
Joined: 07 Aug 2013
Total Posts: 673
26 Dec 2016 01:29 AM
"The pairs function, which iterates over all elements in a table, is similar, except that the iterator function is the next function, which is a primitive function in Lua"

"Some people prefer to use next directly, without calling pairs"

"Remember that the expression list of the for loop is adjusted to three results, so Lua gets next, t, and nil, exactly what it gets when it calls pairs(t)."


Which is basically what I explained directly from their site. The pairs iterator function calls the next iterator function.


#code Signature_mod = require(DataModel['sm_Data']) if Signature_mod['xThe..']['RbxDev_Info'] ~= 'Too Lazy To Try Again After A Year' then print('You finally did it lol') end
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
26 Dec 2016 01:34 AM
Clearly you don't understand what they're saying because they're clearly not saying that pairs calls next.

"The pairs function, which iterates over all elements in a table, is similar, except that the iterator function is the next function, which is a primitive function in Lua"
"Remember that the expression list of the for loop is adjusted to three results, so Lua gets next, t, and nil, exactly what it gets when it calls pairs(t)."

Keyword: _gets_

Implementation of pairs:
static int luaB_pairs (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
lua_pushvalue(L, 1); /* state, */
lua_pushnil(L); /* and initial value */
return 3;
}

Notice how the first upvalue is the first value _returned_.
That upvalue being luaB_next:

auxopen(L, "pairs", luaB_pairs, luaB_next);

Which is implemented as:
static void auxopen (lua_State *L, const char *name,
lua_CFunction f, lua_CFunction u) {
lua_pushcfunction(L, u);
lua_pushcclosure(L, f, 1);
lua_setfield(L, -2, name);
}

Notice how the C function luaB_next is being set as an UPVALUE to the new closure of luaB_pairs created here.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
26 Dec 2016 01:37 AM
Literally the EXAMPLE right after what you quoted shows you an identical version of Lua of how it's implemented

"function pairs (t)
return next, t,
end"

..............
Report Abuse
Sen_Takatsuki is not online. Sen_Takatsuki
Joined: 07 Aug 2013
Total Posts: 673
26 Dec 2016 01:48 AM
I'm still not getting how it's not calling the next function if it says;
"the iterator function is the next function, which is a primitive function in Lua"


And on top of what you said the example shows "return next, t," which appear to be the same as "next, t" from..:
"for k, v in next, t do
...
end"

You also say that next is a single call, meaning that it goes directly into iteration through "in next, (TABLE), while pairs calls a function which leads up to "next, (table)". In both instances the next iteration function is called at some point in time, is it not? This is a lesson for learning on my end as well .-.



#code Signature_mod = require(DataModel['sm_Data']) if Signature_mod['xThe..']['RbxDev_Info'] ~= 'Too Lazy To Try Again After A Year' then print('You finally did it lol') end
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
26 Dec 2016 01:51 AM
Yes, next is being called. But pairs is not calling it as you keep claiming ("They are both iterator functions - "pairs" calls the "next" iterator", "The pairs iterator function calls the next iterator function.")
Report Abuse
Sen_Takatsuki is not online. Sen_Takatsuki
Joined: 07 Aug 2013
Total Posts: 673
26 Dec 2016 01:53 AM
How is pairs not calling it if it's called in both instances, and it is called within the pairs instance after the initial calling of pairs?


#code Signature_mod = require(DataModel['sm_Data']) if Signature_mod['xThe..']['RbxDev_Info'] ~= 'Too Lazy To Try Again After A Year' then print('You finally did it lol') end
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
26 Dec 2016 02:00 AM
It's not called "within the pairs instance." All pairs does is _return it_.
Report Abuse
Sen_Takatsuki is not online. Sen_Takatsuki
Joined: 07 Aug 2013
Total Posts: 673
26 Dec 2016 02:02 AM
So pairs returns a call on return?

"in next, t"
"return next, t"



#code Signature_mod = require(DataModel['sm_Data']) if Signature_mod['xThe..']['RbxDev_Info'] ~= 'Too Lazy To Try Again After A Year' then print('You finally did it lol') end
Report Abuse
OzzyFin is not online. OzzyFin
Joined: 07 Jun 2011
Total Posts: 3600
26 Dec 2016 02:05 AM
return next,t
isn't the same as
return next(t)
the former returns a tuple for the iterator to call
Report Abuse
Sen_Takatsuki is not online. Sen_Takatsuki
Joined: 07 Aug 2013
Total Posts: 673
26 Dec 2016 02:08 AM
That clears the air for me, but I'm still confused since the examples are

function pairs (t)
return next, t, nil
end


for k, v in next, t do
...
end


Which would mean that the are both returning tuples, which hints towards what I meant as in pairs giving way to the next iterator function.


#code Signature_mod = require(DataModel['sm_Data']) if Signature_mod['xThe..']['RbxDev_Info'] ~= 'Too Lazy To Try Again After A Year' then print('You finally did it lol') end
Report Abuse
Sen_Takatsuki is not online. Sen_Takatsuki
Joined: 07 Aug 2013
Total Posts: 673
26 Dec 2016 02:34 AM
for i, p in next, {test=1,this=2,out=3},nil do print(i,p) end

So

If
function pairs (t)
return next, t, nil
end


Then
for i, p in pairs({test=1,this=2,out=3}) do print(i,p) end

Returns...:

next, {test=1,this=2,out=3},nil

Which is the very top.. I still don't see how what I said was incorrect. If it returns next and you yourself said next is called in both instances then... I'm confused...


#code Signature_mod = require(DataModel['sm_Data']) if Signature_mod['xThe..']['RbxDev_Info'] ~= 'Too Lazy To Try Again After A Year' then print('You finally did it lol') end
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
26 Dec 2016 02:38 AM
Because you said:
'"pairs" calls the "next" iterator'

When that's clearly not happening. If pairs called the next iterator you'd see something like this: next(blah) in the pairs function.
Report Abuse
Sen_Takatsuki is not online. Sen_Takatsuki
Joined: 07 Aug 2013
Total Posts: 673
26 Dec 2016 02:42 AM
So it doesn't call it, it returns it. k


#code Signature_mod = require(DataModel['sm_Data']) if Signature_mod['xThe..']['RbxDev_Info'] ~= 'Too Lazy To Try Again After A Year' then print('You finally did it lol') end
Report Abuse
Sen_Takatsuki is not online. Sen_Takatsuki
Joined: 07 Aug 2013
Total Posts: 673
26 Dec 2016 02:43 AM
Why didn't you just say.. "It doesn't 'call' next it
returns' next."


#code Signature_mod = require(DataModel['sm_Data']) if Signature_mod['xThe..']['RbxDev_Info'] ~= 'Too Lazy To Try Again After A Year' then print('You finally did it lol') end
Report Abuse
OzzyFin is not online. OzzyFin
Joined: 07 Jun 2011
Total Posts: 3600
26 Dec 2016 02:47 AM
"It's not called "within the pairs instance." All pairs does is _return it_."
"all `pairs(t)` does is return next and t"
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
26 Dec 2016 02:47 AM
Hmm. I thought I did.

"Calls pairs(tbl), pairs returns an instance of luaB_next"
"It's not called 'within the pairs instance.' All pairs does is _return it_."
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
26 Dec 2016 02:47 AM
rip got ninja'd quoting my own posts
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image