* Writing to .tuc directly from Lua
@ 2021-03-24 3:28 Jairo A. del Rio
2021-03-24 4:12 ` Aditya Mahajan
0 siblings, 1 reply; 8+ messages in thread
From: Jairo A. del Rio @ 2021-03-24 3:28 UTC (permalink / raw)
To: mailing list for ConTeXt users
[-- Attachment #1.1: Type: text/plain, Size: 554 bytes --]
Hi, list. I read about this:
https://www.mail-archive.com/ntg-context@ntg.nl/msg60217.html
But I want to know how to pass data to the .tuc file directly from Lua. I
think about some complex strings are calculations which are done in each
pass and slow down the whole compilation process. Something like:
local function factorial(n)
if in_tuc then -- whatever in_tuc means
return factorials[n]
else
...
factorials[n] = ...
return ...
end
end
Is that feasible? Should I use the CLD versions of the TeX macros instead?
Best regards,
Jairo
[-- Attachment #1.2: Type: text/html, Size: 893 bytes --]
[-- Attachment #2: Type: text/plain, Size: 493 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://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Writing to .tuc directly from Lua
2021-03-24 3:28 Writing to .tuc directly from Lua Jairo A. del Rio
@ 2021-03-24 4:12 ` Aditya Mahajan
2021-03-24 4:20 ` Jairo A. del Rio
0 siblings, 1 reply; 8+ messages in thread
From: Aditya Mahajan @ 2021-03-24 4:12 UTC (permalink / raw)
To: mailing list for ConTeXt users
On Tue, 23 Mar 2021, Jairo A. del Rio wrote:
> Hi, list. I read about this:
>
> https://www.mail-archive.com/ntg-context@ntg.nl/msg60217.html
>
> But I want to know how to pass data to the .tuc file directly from Lua. I
> think about some complex strings are calculations which are done in each
> pass and slow down the whole compilation process. Something like:
>
> local function factorial(n)
> if in_tuc then -- whatever in_tuc means
> return factorials[n]
> else
> ...
> factorials[n] = ...
> return ...
> end
> end
>
> Is that feasible? Should I use the CLD versions of the TeX macros instead?
Does this help:
https://wiki.contextgarden.net/System_Macros/Key_Value_Assignments#Multi-pass_data
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://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Writing to .tuc directly from Lua
2021-03-24 4:12 ` Aditya Mahajan
@ 2021-03-24 4:20 ` Jairo A. del Rio
2021-03-24 6:38 ` Hans Hagen
0 siblings, 1 reply; 8+ messages in thread
From: Jairo A. del Rio @ 2021-03-24 4:20 UTC (permalink / raw)
To: mailing list for ConTeXt users, Aditya Mahajan
[-- Attachment #1.1: Type: text/plain, Size: 1614 bytes --]
Thank you very much. It's useful, indeed. However, what I need is to bypass
TeX (Lua > .tuc) if possible, since all the data I need to handle is
generated with Lua scripts.
Jairo
El mar, 23 de mar. de 2021 a la(s) 23:12, Aditya Mahajan (adityam@umich.edu)
escribió:
> On Tue, 23 Mar 2021, Jairo A. del Rio wrote:
>
> > Hi, list. I read about this:
> >
> > https://www.mail-archive.com/ntg-context@ntg.nl/msg60217.html
> >
> > But I want to know how to pass data to the .tuc file directly from Lua. I
> > think about some complex strings are calculations which are done in each
> > pass and slow down the whole compilation process. Something like:
> >
> > local function factorial(n)
> > if in_tuc then -- whatever in_tuc means
> > return factorials[n]
> > else
> > ...
> > factorials[n] = ...
> > return ...
> > end
> > end
> >
> > Is that feasible? Should I use the CLD versions of the TeX macros
> instead?
>
> Does this help:
>
>
> https://wiki.contextgarden.net/System_Macros/Key_Value_Assignments#Multi-pass_data
>
> 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://context.aanhet.net
> archive : https://bitbucket.org/phg/context-mirror/commits/
> wiki : http://contextgarden.net
>
> ___________________________________________________________________________________
>
[-- Attachment #1.2: Type: text/html, Size: 2735 bytes --]
[-- Attachment #2: Type: text/plain, Size: 493 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://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Writing to .tuc directly from Lua
2021-03-24 4:20 ` Jairo A. del Rio
@ 2021-03-24 6:38 ` Hans Hagen
2021-03-24 17:33 ` Jairo A. del Rio
0 siblings, 1 reply; 8+ messages in thread
From: Hans Hagen @ 2021-03-24 6:38 UTC (permalink / raw)
To: mailing list for ConTeXt users, Jairo A. del Rio, Aditya Mahajan
On 3/24/2021 5:20 AM, Jairo A. del Rio wrote:
> Thank you very much. It's useful, indeed. However, what I need is to
> bypass TeX (Lua > .tuc) if possible, since all the data I need to handle
> is generated with Lua scripts.
datasets-001.tex in the test suite
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
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://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Writing to .tuc directly from Lua
2021-03-24 6:38 ` Hans Hagen
@ 2021-03-24 17:33 ` Jairo A. del Rio
[not found] ` <nycvar.YAK.7.78.908.2103241631460.1372067@nqv-guvaxcnq>
2021-03-24 21:47 ` Hans Hagen
0 siblings, 2 replies; 8+ messages in thread
From: Jairo A. del Rio @ 2021-03-24 17:33 UTC (permalink / raw)
To: Hans Hagen; +Cc: mailing list for ConTeXt users
[-- Attachment #1.1: Type: text/plain, Size: 1782 bytes --]
Hi, Hans. Here's my (miserably failing) attempt:
\definedataset[nicedata]
\starttext
\startluacode
local name = "nicedata"
-- For exposition only
local function dofactorial(n)
local function inner(c,m)
if m<2 then return c end
return inner(c*m, m-1)
end
return inner(1,n)
end
local function factorial(n)
local stringn = tostring(n)
if job.datasets.collected[name] then
return job.datasets.collected[name].factorial[stringn]
else
local mydata = {[stringn] = tostring(dofactorial(n))}
job.datasets.setdata{
name = name,
tag = "factorial",
data = mydata
}
return mydata[stringn]
end
end
interfaces.implement{
name = "factorial",
public = true,
arguments = {"string"},
actions = {tonumber, factorial, context}
}
\stopluacode
\factorial{7}
\stoptext
What happens is that, in each run, both the first and the second conditions
are met, so the compilation is twice as slow, the opposite result of what I
meant to do. How do I fix that?
Jairo
El mié, 24 de mar. de 2021 a la(s) 01:38, Hans Hagen (j.hagen@xs4all.nl)
escribió:
> On 3/24/2021 5:20 AM, Jairo A. del Rio wrote:
> > Thank you very much. It's useful, indeed. However, what I need is to
> > bypass TeX (Lua > .tuc) if possible, since all the data I need to handle
> > is generated with Lua scripts.
> datasets-001.tex in the test suite
>
>
> -----------------------------------------------------------------
> Hans Hagen | PRAGMA ADE
> Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
> tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
> -----------------------------------------------------------------
>
[-- Attachment #1.2: Type: text/html, Size: 4957 bytes --]
[-- Attachment #2: Type: text/plain, Size: 493 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://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Writing to .tuc directly from Lua
[not found] ` <nycvar.YAK.7.78.908.2103241631460.1372067@nqv-guvaxcnq>
@ 2021-03-24 21:00 ` Jairo A. del Rio
0 siblings, 0 replies; 8+ messages in thread
From: Jairo A. del Rio @ 2021-03-24 21:00 UTC (permalink / raw)
To: Aditya Mahajan, mailing list for ConTeXt users
[-- Attachment #1.1: Type: text/plain, Size: 1144 bytes --]
No, no actually. I only put a toy example. What I need to solve is related
with storing data in the .tuc file when processes take a long time.
Memoization doesn't work as data is lost with each new run.
Jairo
El mié, 24 de mar. de 2021 a la(s) 15:36, Aditya Mahajan (adityam@umich.edu)
escribió:
> On Wed, 24 Mar 2021, Jairo A. del Rio wrote:
>
> > Hi, Hans. Here's my (miserably failing) attempt:
> >
> > \definedataset[nicedata]
> >
> > \starttext
> >
> > \startluacode
> >
> > local name = "nicedata"
> >
> >
> > -- For exposition only
> >
> > local function dofactorial(n)
> >
> > local function inner(c,m)
> >
> > if m<2 then return c end
> >
> > return inner(c*m, m-1)
> >
> > end
> >
> > return inner(1,n)
> >
> > end
>
> Is this an example or your actual use case? In case you want to just
> compute the factorial without doing tail recursion:
>
> function factorial(n)
> local product = 1
> for i = 1,n do
> product = product * i
> end
> return product
> end
>
> is reasonable fast (for values of n for which factorial(n) doesn't
> overflow anyways).
>
> Aditya
>
[-- Attachment #1.2: Type: text/html, Size: 1707 bytes --]
[-- Attachment #2: Type: text/plain, Size: 493 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://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Writing to .tuc directly from Lua
2021-03-24 17:33 ` Jairo A. del Rio
[not found] ` <nycvar.YAK.7.78.908.2103241631460.1372067@nqv-guvaxcnq>
@ 2021-03-24 21:47 ` Hans Hagen
2021-03-24 23:00 ` Jairo A. del Rio
1 sibling, 1 reply; 8+ messages in thread
From: Hans Hagen @ 2021-03-24 21:47 UTC (permalink / raw)
To: Jairo A. del Rio; +Cc: mailing list for ConTeXt users
On 3/24/2021 6:33 PM, Jairo A. del Rio wrote:
> Hi, Hans. Here's my (miserably failing) attempt:
>
> \definedataset[nicedata]
>
> \starttext
>
> \startluacode
>
> local name = "nicedata"
>
>
> -- For exposition only
>
> local function dofactorial(n)
>
> local function inner(c,m)
>
> if m<2 then return c end
>
> return inner(c*m, m-1)
>
> end
>
> return inner(1,n)
>
> end
>
>
> local function factorial(n)
>
> local stringn = tostring(n)
>
> if job.datasets.collected[name] then
>
> return job.datasets.collected[name].factorial[stringn]
>
> else
>
> local mydata = {[stringn] = tostring(dofactorial(n))}
>
> job.datasets.setdata{
>
> name = name,
>
> tag = "factorial",
>
> data = mydata
>
> }
>
> return mydata[stringn]
>
> end
>
> end
>
>
> interfaces.implement{
>
> name = "factorial",
>
> public = true,
>
> arguments = {"string"},
>
> actions = {tonumber, factorial, context}
>
> }
>
>
> \stopluacode
>
> \factorial{7}
>
> \stoptext
>
>
> What happens is that, in each run, both the first and the second
> conditions are met, so the compilation is twice as slow, the opposite
> result of what I meant to do. How do I fix that?
\definedataset[nicedata]
\starttext
\startluacode
local function dofactorial(n)
local function inner(c,m)
if m < 2 then
return c
else
return inner(c*m, m-1)
end
end
return inner(1,n)
end
local function factorial(n)
local data = job.datasets.getdata("factorials","list")
if not data then
data = { }
end
local f = data[n]
if not f then
f = dofactorial(n)
data[n] = f
end
job.datasets.setdata {
name = "factorials",
tag = "list",
data = data
}
return f
end
interfaces.implement{
name = "factorial",
public = true,
arguments = { "integer" },
actions = { factorial, context }
}
\stopluacode
\dorecurse {20} {
\factorial #1 \relax
}
\stoptext
but you probably don't gain much as these factorials are not that slow
(if you use them many times in a document you can just cache them)
Hans
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
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://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Writing to .tuc directly from Lua
2021-03-24 21:47 ` Hans Hagen
@ 2021-03-24 23:00 ` Jairo A. del Rio
0 siblings, 0 replies; 8+ messages in thread
From: Jairo A. del Rio @ 2021-03-24 23:00 UTC (permalink / raw)
To: Hans Hagen; +Cc: mailing list for ConTeXt users
[-- Attachment #1.1: Type: text/plain, Size: 3227 bytes --]
El mié, 24 de mar. de 2021 a la(s) 16:47, Hans Hagen (j.hagen@xs4all.nl)
escribió:
> On 3/24/2021 6:33 PM, Jairo A. del Rio wrote:
> > Hi, Hans. Here's my (miserably failing) attempt:
> >
> > \definedataset[nicedata]
> >
> > \starttext
> >
> > \startluacode
> >
> > local name = "nicedata"
> >
> >
> > -- For exposition only
> >
> > local function dofactorial(n)
> >
> > local function inner(c,m)
> >
> > if m<2 then return c end
> >
> > return inner(c*m, m-1)
> >
> > end
> >
> > return inner(1,n)
> >
> > end
> >
> >
> > local function factorial(n)
> >
> > local stringn = tostring(n)
> >
> > if job.datasets.collected[name] then
> >
> > return job.datasets.collected[name].factorial[stringn]
> >
> > else
> >
> > local mydata = {[stringn] = tostring(dofactorial(n))}
> >
> > job.datasets.setdata{
> >
> > name = name,
> >
> > tag = "factorial",
> >
> > data = mydata
> >
> > }
> >
> > return mydata[stringn]
> >
> > end
> >
> > end
> >
> >
> > interfaces.implement{
> >
> > name = "factorial",
> >
> > public = true,
> >
> > arguments = {"string"},
> >
> > actions = {tonumber, factorial, context}
> >
> > }
> >
> >
> > \stopluacode
> >
> > \factorial{7}
> >
> > \stoptext
> >
> >
> > What happens is that, in each run, both the first and the second
> > conditions are met, so the compilation is twice as slow, the opposite
> > result of what I meant to do. How do I fix that?
>
> \definedataset[nicedata]
>
> \starttext
>
> \startluacode
>
> local function dofactorial(n)
> local function inner(c,m)
> if m < 2 then
> return c
> else
> return inner(c*m, m-1)
> end
> end
> return inner(1,n)
> end
>
> local function factorial(n)
> local data = job.datasets.getdata("factorials","list")
> if not data then
> data = { }
> end
> local f = data[n]
> if not f then
> f = dofactorial(n)
> data[n] = f
> end
> job.datasets.setdata {
> name = "factorials",
> tag = "list",
> data = data
> }
> return f
> end
>
> interfaces.implement{
> name = "factorial",
> public = true,
> arguments = { "integer" },
> actions = { factorial, context }
> }
>
> \stopluacode
>
> \dorecurse {20} {
> \factorial #1 \relax
> }
>
> \stoptext
>
>
It does exactly what I need. It's good to know datasets can be directly
generated, too. Thank you a lot!
> but you probably don't gain much as these factorials are not that slow
> (if you use them many times in a document you can just cache them)
>
>
It was only a toy example. I intend to work with way larger numbers. :)
> Hans
>
>
> -----------------------------------------------------------------
> Hans Hagen | PRAGMA ADE
> Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
> tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
> -----------------------------------------------------------------
>
Best regards,
Jairo
[-- Attachment #1.2: Type: text/html, Size: 4914 bytes --]
[-- Attachment #2: Type: text/plain, Size: 493 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://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2021-03-24 23:00 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-24 3:28 Writing to .tuc directly from Lua Jairo A. del Rio
2021-03-24 4:12 ` Aditya Mahajan
2021-03-24 4:20 ` Jairo A. del Rio
2021-03-24 6:38 ` Hans Hagen
2021-03-24 17:33 ` Jairo A. del Rio
[not found] ` <nycvar.YAK.7.78.908.2103241631460.1372067@nqv-guvaxcnq>
2021-03-24 21:00 ` Jairo A. del Rio
2021-03-24 21:47 ` Hans Hagen
2021-03-24 23:00 ` Jairo A. del Rio
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).