A list comprehension is a special syntax in some programming languages to describe lists. It is similar to the way mathematicians describe sets, with a set comprehension, hence the name.

Some attributes of a list comprehension are:

 They should be distinct from (nested) for loops and the use of map and filter functions within the syntax of the language.
 They should return either a list or an iterator (something that returns successive members of a collection, in order).
 The syntax has parts corresponding to that of set-builder notation. 


Task

Write a list comprehension that builds the list of all Pythagorean triples with elements between   1   and   n.

If the language has multiple ways for expressing such a construct (for example, direct list comprehensions and generators), write one example for each.




 
LC={}
LC.__index = LC
 
function LC:new(o)
  o = o or {}
  setmetatable(o, self)
  return o
end
 
function LC:add_iter(func)
  local prev_iter = self.iter
  self.iter = coroutine.wrap(
    (prev_iter == nil) and (function() func{} end)
    or (function() for arg in prev_iter do func(arg) end end))
  return self
end
 
function maybe_call(maybe_func, arg)
  if type(maybe_func) == "function" then return maybe_func(arg) end
  return maybe_func
end
 
function LC:range(key, first, last)
  return self:add_iter(function(arg)
    for value=maybe_call(first, arg), maybe_call(last, arg) do
      arg[key] = value
      coroutine.yield(arg)
    end
  end)
end
 
function LC:where(pred)
  return self:add_iter(function(arg) if pred(arg) then coroutine.yield(arg) end end)
end
 