lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2012/12/22 Kevin Martin <kev82@khn.org.uk>:
>
> I want to write the following code:
>
> for a in carer:availBlocks() do
>         --do something with a
> end
>
> I do not want to write:
>
> for a in carer:toProblem():availBlocks() do if carer:id() == a:carer():id() then
>         --do something with a
> end
>

This can be done with coroutines.

function carer:availBlocks()
    return coroutine.wrap(
       function()
          for a in self:toProblem():availBlocks() do
          if self:id() == a:carer():id() then coroutine.yield(a)
      end end end)
end