ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Lua to Context Table
@ 2014-08-01 22:59 John Kitzmiller
  2014-08-02  1:23 ` luigi scarso
  0 siblings, 1 reply; 4+ messages in thread
From: John Kitzmiller @ 2014-08-01 22:59 UTC (permalink / raw)
  To: ntg-context

Here is Lua code that prints the first nine fibonacci numbers:

local function fib(n)
  f={1,1}
  for i=3,9 do
    f[i]=f[i-1]+f[i-2]
  end
  return(f[n])
end
for n=1,9 do print(fib(n)) end

Here is Context+Lua that prints 3 x 3 table of the first nine natural numbers:

\starttext
\setupTABLE[each][each][frame=off,align=left,width=4em,height=2em]
\startluacode
context.bTABLE()
for j = 0, 2 do
  context.bTR()
    for k = 1, 3 do
      context.bTD()
        context(3*j+k)
      context.eTD()
    end
  context.eTR()
end
context.eTABLE()
\stopluacode
\stoptext

How can these be combined to print a 3 x 3 table of the first nine fibonacci numbers?

I have tried a couple of ways to iterate over the lua table f in the most indented line of context, looked at what pertinent threads I could find, the Wiki entries, CLD, and tried Aditya's method from Stack Exchange; I am missing something.

John

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Lua to Context Table
  2014-08-01 22:59 Lua to Context Table John Kitzmiller
@ 2014-08-02  1:23 ` luigi scarso
  2014-08-02  4:48   ` Aditya Mahajan
  0 siblings, 1 reply; 4+ messages in thread
From: luigi scarso @ 2014-08-02  1:23 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 1645 bytes --]

On Sat, Aug 2, 2014 at 12:59 AM, John Kitzmiller <kitz@inradius.net> wrote:

> Here is Lua code that prints the first nine fibonacci numbers:
>
> local function fib(n)
>   f={1,1}
>   for i=3,9 do
>     f[i]=f[i-1]+f[i-2]
>   end
>   return(f[n])
> end
> for n=1,9 do print(fib(n)) end
>
> Here is Context+Lua that prints 3 x 3 table of the first nine natural
> numbers:
>
> \starttext
> \setupTABLE[each][each][frame=off,align=left,width=4em,height=2em]
> \startluacode
> context.bTABLE()
> for j = 0, 2 do
>   context.bTR()
>     for k = 1, 3 do
>       context.bTD()
>         context(3*j+k)
>       context.eTD()
>     end
>   context.eTR()
> end
> context.eTABLE()
> \stopluacode
> \stoptext
>
> How can these be combined to print a 3 x 3 table of the first nine
> fibonacci numbers?
>
> I have tried a couple of ways to iterate over the lua table f in the most
> indented line of context, looked at what pertinent threads I could find,
> the Wiki entries, CLD, and tried Aditya's method from Stack Exchange; I am
> missing something.
>

You have a *global* table f  that collides with the internals of context
local function fib(n)
  f={1,1}
  for i=3,9 do
    f[i]=f[i-1]+f[i-2]
  end
  return(f[n])
end


\starttext
\setupTABLE[each][each][frame=off,align=left,width=8em,height=2em]

\startluacode
local function fib(n)
  local f={1,1}
  for i=3,9 do
    f[i]=f[i-1]+f[i-2]
  end
  return(f[n])
end
context.bTABLE()
for j = 0, 2 do
  context.bTR()
    for k = 1, 3 do
      context.bTD()
        context("fib(%s)=%s" ,3*j+k, fib(3*j+k) )
      context.eTD()
    end
  context.eTR()
end
context.eTABLE()
\stopluacode
\stoptext

-- 
luigi

[-- Attachment #1.2: Type: text/html, Size: 2589 bytes --]

[-- Attachment #2: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Lua to Context Table
  2014-08-02  1:23 ` luigi scarso
@ 2014-08-02  4:48   ` Aditya Mahajan
  0 siblings, 0 replies; 4+ messages in thread
From: Aditya Mahajan @ 2014-08-02  4:48 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On Sat, 2 Aug 2014, luigi scarso wrote:

> On Sat, Aug 2, 2014 at 12:59 AM, John Kitzmiller <kitz@inradius.net> wrote:
>
>> Here is Lua code that prints the first nine fibonacci numbers:
>>
>> local function fib(n)
>>   f={1,1}
>>   for i=3,9 do
>>     f[i]=f[i-1]+f[i-2]
>>   end
>>   return(f[n])
>> end
>> for n=1,9 do print(fib(n)) end
>>
>> Here is Context+Lua that prints 3 x 3 table of the first nine natural
>> numbers:
>>
>> \starttext
>> \setupTABLE[each][each][frame=off,align=left,width=4em,height=2em]
>> \startluacode
>> context.bTABLE()
>> for j = 0, 2 do
>>   context.bTR()
>>     for k = 1, 3 do
>>       context.bTD()
>>         context(3*j+k)
>>       context.eTD()
>>     end
>>   context.eTR()
>> end
>> context.eTABLE()
>> \stopluacode
>> \stoptext
>>
>> How can these be combined to print a 3 x 3 table of the first nine
>> fibonacci numbers?
>>
>> I have tried a couple of ways to iterate over the lua table f in the most
>> indented line of context, looked at what pertinent threads I could find,
>> the Wiki entries, CLD, and tried Aditya's method from Stack Exchange; I am
>> missing something.
>>
>
> You have a *global* table f  that collides with the internals of context
> local function fib(n)
>  f={1,1}
>  for i=3,9 do
>    f[i]=f[i-1]+f[i-2]
>  end
>  return(f[n])
> end
>
>
> \starttext
> \setupTABLE[each][each][frame=off,align=left,width=8em,height=2em]
>
> \startluacode
> local function fib(n)
>  local f={1,1}
>  for i=3,9 do
>    f[i]=f[i-1]+f[i-2]
>  end
>  return(f[n])
> end
> context.bTABLE()
> for j = 0, 2 do
>  context.bTR()
>    for k = 1, 3 do
>      context.bTD()
>        context("fib(%s)=%s" ,3*j+k, fib(3*j+k) )
>      context.eTD()
>    end
>  context.eTR()
> end
> context.eTABLE()
> \stopluacode
> \stoptext

This version recomputes all fibnocci numbers from scrach each time. Here 
is a memoized version:

\setupTABLE[each][each][frame=off,align=left,width=8em,height=2em]
\starttext

\startluacode
local fibs = {1, 1}
local function fib(n)
   if fibs[n] == nil then
     print(">>>>", "Computing fib:" .. n)
     fibs[n] = fib(n-1) + fib(n-2)
   end

   return fibs[n]
end

context.bTABLE()
for j = 0, 2 do
   context.bTR()
     for k = 1, 3 do
       context.bTD()
         context("fib(%s)=%s" ,3*j+k, fib(3*j+k) )
       context.eTD()
     end
   context.eTR()
end
context.eTABLE()
\stopluacode
\stoptext


Aditya
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Lua to Context Table
       [not found] <mailman.705.1406969529.2240.ntg-context@ntg.nl>
@ 2014-08-02 14:27 ` John Kitzmiller
  0 siblings, 0 replies; 4+ messages in thread
From: John Kitzmiller @ 2014-08-02 14:27 UTC (permalink / raw)
  To: ntg-context


> On Aug 2, 2014, Aditya Mahajan <adityam@umich.edu> wrote:
>> On Sat, 2 Aug 2014, luigi scarso wrote:
>>> On Sat, Aug 2, 2014, John Kitzmiller <kitz@inradius.net> wrote:
>>> Here is Lua code that prints the first nine fibonacci numbers:
>>> 
>>> local function fib(n)
>>>  f={1,1}
>>> ...the Wiki entries, CLD, and tried Aditya's method from Stack Exchange; I am
>>> missing something.
>>> 
>> 
>> You have a *global* table f  that collides with the internals of context
>> ...
>> local function fib(n)
>> local f={1,1}
>> ...
> 
> This version recomputes all fibnocci numbers from scrach each time. Here 
> is a memoized version:
> 
> local fibs = {1, 1}
> local function fib(n)
>   if fibs[n] == nil then
>     print(">>>>", "Computing fib:" .. n)
>     fibs[n] = fib(n-1) + fib(n-2)
>   end
> ...


Thanks to you both! I missed the global/local conflict, and then learned a way to avoid double computation; thanks again!

John
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-08-02 14:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-01 22:59 Lua to Context Table John Kitzmiller
2014-08-02  1:23 ` luigi scarso
2014-08-02  4:48   ` Aditya Mahajan
     [not found] <mailman.705.1406969529.2240.ntg-context@ntg.nl>
2014-08-02 14:27 ` John Kitzmiller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).