-1

I don't know Python super well but I know it well enough that I'm trying to translate some Python code to Lua. But I can't figure out what this code is supposed to do.

 var_declarations = [
  VarDecl(var_node, type_node)
  for var_node in var_nodes
 ]

VarDecl is a class, and var_nodes is a list. Full code is here.

Vonkswalgo
  • 11
  • 6
  • 1
    This is list comprehension. Take a look at https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions – kuro Jun 27 '21 at 05:01
  • 1
    This is a list comprehension syntax, It is instantiating `VarDecl` from var_nodes list – ShivaGaire Jun 27 '21 at 05:01

2 Answers2

0

That's called a "list comprehension". It is exactly the same as:

var_declarations = []
for var_node in var_nodes:
    var_declarations.append( VarDecl(var_node, type_node) )

It just builds a new list with the result of calling VarDecl one at a time.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
0

This is a list comprehension syntax. Looking at the code snippet that you referred to, it is instantiating the VarDecl class for each var_node in var_nodes list(array) and creating a new list called var_declarations.

ShivaGaire
  • 2,283
  • 1
  • 20
  • 31