Skip to content

Tables are passed by reference - not an issue, but reassignment is not needed #3

@dcuny

Description

@dcuny

In the demo program xor.lua, you have the code:

local i = 0

while i<200 do
  brain = nn.train(brain,{1,1},{0})
  brain = nn.train(brain,{0,1},{1})
  brain = nn.train(brain,{1,0},{1})
  brain = nn.train(brain,{1,1},{0})
  i= i+1
  print("Epoch"..i)
end

In Lua, tables are passed by reference. Changes made to the table made in the routine nn.train() are being made to the same table referenced by brain. Returning the table from the function is not necessary.

The code below has the same result. Lua automatically creates i as a local variable when used in the loop, so there's no need to declare it:

for i = 1, 200 do
  nn.train(brain,{1,1},{0})
  nn.train(brain,{0,1},{1})
  nn.train(brain,{1,0},{1})
  nn.train(brain,{1,1},{0})
  print("Epoch"..i)
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions