From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/110963 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: "Jairo A. del Rio" Newsgroups: gmane.comp.tex.context Subject: Re: Writing to .tuc directly from Lua Date: Wed, 24 Mar 2021 18:00:25 -0500 Message-ID: References: Reply-To: mailing list for ConTeXt users Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============4694779678928543378==" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="24856"; mail-complaints-to="usenet@ciao.gmane.io" Cc: mailing list for ConTeXt users To: Hans Hagen Original-X-From: ntg-context-bounces@ntg.nl Thu Mar 25 00:01:04 2021 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane-mx.org Original-Received: from zapf.boekplan.nl ([5.39.185.232] helo=zapf.ntg.nl) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lPCUo-0006LU-GH for gctc-ntg-context-518@m.gmane-mx.org; Thu, 25 Mar 2021 00:01:02 +0100 Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 7687F281D5E; Thu, 25 Mar 2021 00:00:41 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id xR8xVJa3Ck17; Thu, 25 Mar 2021 00:00:40 +0100 (CET) Original-Received: from zapf.ntg.nl (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 84BAA281D5F; Thu, 25 Mar 2021 00:00:40 +0100 (CET) Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 59A52281D5A for ; Thu, 25 Mar 2021 00:00:39 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id tar-6tU14iLv for ; Thu, 25 Mar 2021 00:00:38 +0100 (CET) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=209.85.221.44; helo=mail-wr1-f44.google.com; envelope-from=jairoadelrio6@gmail.com; receiver= Original-Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by zapf.ntg.nl (Postfix) with ESMTPS id 68CBE281D55 for ; Thu, 25 Mar 2021 00:00:38 +0100 (CET) Original-Received: by mail-wr1-f44.google.com with SMTP id o16so468154wrn.0 for ; Wed, 24 Mar 2021 16:00:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Nr4IGp3Tmog2v4jY/CLMx8yWNiwJFfDzYLLy8+84pfM=; b=rIxuwuqLDGeM4KC9CDU1Wy5fge9ZDfJ9akiXZTEqulhmpSJ5bkPYZ67jYZl0QwtePP Als9oD1iU7EMSHNiB7baanfcQVIvc3LySIaKcU51Sk8WY6lD0H9fwZD7pYKFDgWXAinb bvCHKePQZ37WmRf6abGnbqUYkav1E/RjXc/0dIg4GGJXiwgSGSSjYA0W7wzw0IKZcqA3 0f60niozgfYlGDvMDtFnNb3/nQYcQKA62hjwWgJKhUGle7VFxj30qP91H8siXB+A4OKB y8AXMwUZrNVZf5QywwQvSI/9zxHnR/mH4aUaQe2W8L7W0+xsr2EX4K7zaNjC81N4+XPd DUOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Nr4IGp3Tmog2v4jY/CLMx8yWNiwJFfDzYLLy8+84pfM=; b=LF03RBGrwCMcBgbVTmCjR2TK3ymXXDUv4MJ6OlxatIGs6IgbRmKA5QOuJzmKZ0O6D/ y2/AgPStMye2E9NSrmzw563KNcuVmyML4EqbEf2X8+FGEusDl0qiO0b2auZ9Ak3cFo3n eatQIY6t6U5Ah2Eqki9HBP4/NI5Zh9g1jAFTXJT9WjueEwynUrwBN3D3Nldit85bguOS 2w+2ZCxWDimL6wd1wPWmm7Pu2Us4xrVCCY4zTeU7ur+gJzHGk2p7YoMxBm/EGrtX9nMX 6OeYXsKNScazT/PEPiwCUSmoY6uQTIDXORWAujodSzLseblGa6Y4M39vaU3aHm/WzLxU hhHA== X-Gm-Message-State: AOAM5309v85ZMnpM3zAsKjdbmJRxN4zNEswgrktZWMCeQ0CaFsudP4ft GMGjQ2S1oF9TmtISevlLk2drdUP9OBTR8FLNfZo= X-Google-Smtp-Source: ABdhPJxfX07RYG3QWrFKG2OI+ugLN7dWTIdFqiRubjxPA1EJ5h2KAfBaUEAOMua97gC9l2beNmZggoS3AxXbVD/8KkA= X-Received: by 2002:a5d:65d2:: with SMTP id e18mr5652248wrw.349.1616626837973; Wed, 24 Mar 2021 16:00:37 -0700 (PDT) In-Reply-To: X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.26 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ntg-context-bounces@ntg.nl Original-Sender: "ntg-context" Xref: news.gmane.io gmane.comp.tex.context:110963 Archived-At: --===============4694779678928543378== Content-Type: multipart/alternative; boundary="000000000000696c1f05be50443f" --000000000000696c1f05be50443f Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable El mi=C3=A9, 24 de mar. de 2021 a la(s) 16:47, Hans Hagen (j.hagen@xs4all.n= l) escribi=C3=B3: > 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 =3D "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 =3D tostring(n) > > > > if job.datasets.collected[name] then > > > > return job.datasets.collected[name].factorial[stringn] > > > > else > > > > local mydata =3D {[stringn] =3D tostring(dofactorial(n))} > > > > job.datasets.setdata{ > > > > name =3D name, > > > > tag =3D "factorial", > > > > data =3D mydata > > > > } > > > > return mydata[stringn] > > > > end > > > > end > > > > > > interfaces.implement{ > > > > name =3D "factorial", > > > > public =3D true, > > > > arguments =3D {"string"}, > > > > actions =3D {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 =3D job.datasets.getdata("factorials","list") > if not data then > data =3D { } > end > local f =3D data[n] > if not f then > f =3D dofactorial(n) > data[n] =3D f > end > job.datasets.setdata { > name =3D "factorials", > tag =3D "list", > data =3D data > } > return f > end > > interfaces.implement{ > name =3D "factorial", > public =3D true, > arguments =3D { "integer" }, > actions =3D { 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 --000000000000696c1f05be50443f Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
El mi=C3=A9, 24 de mar. de 2021 a la(= s) 16:47, Hans Hagen (j.hagen@xs4all.n= l) escribi=C3=B3:
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 =3D "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 =3D tostring(n)
>
> if job.datasets.collected[name] then
>
> return job.datasets.collected[name].factorial[stringn]
>
> else
>
> local mydata =3D {[stringn] =3D tostring(dofactorial(n))}
>
> job.datasets.setdata{
>
> name =3D name,
>
> tag =3D "factorial",
>
> data =3D mydata
>
> }
>
> return mydata[stringn]
>
> end
>
> end
>
>
> interfaces.implement{
>
> name =3D "factorial",
>
> public =3D true,
>
> arguments =3D {"string"},
>
> actions =3D {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)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 local function inner(c,m)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if m < 2 then =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return c
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0else
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return inner(c*m, m-1)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0end
=C2=A0 =C2=A0 =C2=A0 =C2=A0 end
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return inner(1,n)
end

local function factorial(n)
=C2=A0 =C2=A0 =C2=A0local data =3D job.datasets.getdata("factorials&qu= ot;,"list")
=C2=A0 =C2=A0 =C2=A0if not data then
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0data =3D { }
=C2=A0 =C2=A0 =C2=A0end
=C2=A0 =C2=A0 =C2=A0local f =3D data[n]
=C2=A0 =C2=A0 =C2=A0if not f then
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 f =3D dofactorial(n= )
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0data[n] =3D f
=C2=A0 =C2=A0 =C2=A0end
=C2=A0 =C2=A0 =C2=A0job.datasets.setdata {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0name =3D "factorials",
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0tag=C2=A0 =3D "list",
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0data =3D data
=C2=A0 =C2=A0 =C2=A0}
=C2=A0 =C2=A0 =C2=A0return f
end

interfaces.implement{
=C2=A0 =C2=A0 =C2=A0 =C2=A0 name=C2=A0 =C2=A0 =C2=A0 =3D "factorial&qu= ot;,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 public=C2=A0 =C2=A0 =3D true,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 arguments =3D { "integer" },
=C2=A0 =C2=A0 =C2=A0 =C2=A0 actions=C2=A0 =C2=A0=3D { factorial, context }<= br> }

\stopluacode

\dorecurse {20} {
=C2=A0 =C2=A0 =C2=A0\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!
<= /div>
=C2=A0
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. :)
=C2=A0
Hans


-----------------------------------------------------------------
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0Hans Hagen | PRAGMA ADE
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Ridderstraat 27 | 80= 61 GH Hasselt | The Netherlands
=C2=A0 =C2=A0 =C2=A0 =C2=A0 tel: 038 477 53 69 | www.pragma-ade.nl | www.= pragma-pod.nl
-----------------------------------------------------------------

Best regards,

Jairo=
--000000000000696c1f05be50443f-- --===============4694779678928543378== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX18KSWYgeW91ciBxdWVzdGlvbiBpcyBvZiBpbnRlcmVz dCB0byBvdGhlcnMgYXMgd2VsbCwgcGxlYXNlIGFkZCBhbiBlbnRyeSB0byB0aGUgV2lraSEKCm1h aWxsaXN0IDogbnRnLWNvbnRleHRAbnRnLm5sIC8gaHR0cDovL3d3dy5udGcubmwvbWFpbG1hbi9s aXN0aW5mby9udGctY29udGV4dAp3ZWJwYWdlICA6IGh0dHA6Ly93d3cucHJhZ21hLWFkZS5ubCAv IGh0dHA6Ly9jb250ZXh0LmFhbmhldC5uZXQKYXJjaGl2ZSAgOiBodHRwczovL2JpdGJ1Y2tldC5v cmcvcGhnL2NvbnRleHQtbWlycm9yL2NvbW1pdHMvCndpa2kgICAgIDogaHR0cDovL2NvbnRleHRn YXJkZW4ubmV0Cl9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCg== --===============4694779678928543378==--