ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Hans Hagen <j.hagen@xs4all.nl>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>,
	Jeong Dal <haksan@me.com>
Subject: Re: Code lua in a table
Date: Mon, 20 Jul 2020 10:36:33 +0200	[thread overview]
Message-ID: <31495829-66d7-c670-75e2-d1c538f38e9c@xs4all.nl> (raw)
In-Reply-To: <D512FBE4-D72C-4DE6-BE4E-19D2E8266604@me.com>

On 7/20/2020 7:56 AM, Jeong Dal wrote:
> Dear Fabrice,
> 
> You may split Binom(n,k) function into two functions as following:
> 
 > see original mail
 >
> Dalyoung
Best stay in a protected namespace ...

\startluacode

     local function fact (n)
         if n <= 0 then
             return 1
         else
             return n * fact(n-1)
         end
     end

     local function ncr(n,r)
         return fact(n)/(fact(r)*fact(n-r))
     end

     userdata.P = {
         fact = fact,
         ncr  = ncr,
     }

     function MP.pascal_ncr(n, r)
         mp.print(ncr(n,r))
     end

\stopluacode

Watch the last definition. This permits

       % tt := lua("mp.print(userdata.P.ncr(" & decimal n & "," & 
decimal r & " ))");

replaced by

         tt := lua.MP.pascal_ncr(n,r);

which looks nicer.

\startbuffer[pt1]
     numeric n, r, s, u, dx, dy, tt;
     path p, q;
     pair A, B, start, now;
     u := 1.8cm;
     A := dir(210)*u;
     B := dir(-30)*u;
     dy := sind(30)*u;
     dx := 2*cosd(30)*u;
     for n=0 upto 4:
         start := n*dir(210)*u;
         for r=0 upto n:
             s := n-r;
           % tt := lua("mp.print(userdata.P.ncr(" & decimal n & "," & 
decimal r & " ))");
           tt := lua.MP.pascal_ncr(n,r);
             now := start+r*right*dx;
             dotlabel.top(textext("$\displaystyle {" & decimal n & 
"\choose" & decimal r & "} = "& decimal tt & "$"),now);
             draw (now+A) -- now -- (now+B);
         endfor;
     endfor;
\stopbuffer

Now, in context lmtx we can have a different kind of abstraction. We can 
do this:

     function MP.pascal_ncr_x()
         mp.print(ncr(mp.scan.pair()))
     end

and then use:

     tt := runscript("MP.pascal_ncr_x()") (n,r) ;

Of course one can decide to pick to two numerics instead, like

     tt := runscript("MP.pascal_ncr_x()") n r ;

but i leave that as exercise.

           % tt := runscript mp_pascal_ncr (n,r) ;
             tt := pascal_ncr (n,r) ;

However, we still have the rather verbose runscript here, so we go 
further, we register pascal as script:

\startluacode
     metapost.registerscript("pascal_ncr",MP.pascal_ncr_x)
\stopluacode

And then define an alias at the metafun end:

\startMPextensions
     newinternal mp_pascal_ncr ; mp_pascal_ncr := scriptindex "pascal_ncr" ;

     def pascal_ncr =
         runscript mp_pascal_ncr
     enddef ;
\stopMPextensions

The internal permits this:

    tt := runscript mp_pascal_ncr (n,r) ;

while the additional def permits

             tt := pascal_ncr (n,r) ;

Now watch out, because we define pascal_ncr here, something
lua.MP.pascal_ncr(n,r) won't work because the last part gets expanded 
because that is what mp does (i'll probably cook something for that some 
day).

Now, to come back to

    "I couldn’t wikify it at that time because I don’t know
     how to.  I’ll do it soon."

looks like you suddenly have an additional challenge,

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
___________________________________________________________________________________

  reply	other threads:[~2020-07-20  8:36 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.1.1595152801.23549.ntg-context@ntg.nl>
2020-07-20  5:56 ` Jeong Dal
2020-07-20  8:36   ` Hans Hagen [this message]
2020-07-20 10:13     ` Fabrice Couvreur
2020-07-20 12:40     ` Jeong Dal
2020-07-20 12:54       ` Hans Hagen
2020-07-21 13:08         ` Fabrice Couvreur
2020-07-18 18:05 Fabrice Couvreur
2020-07-18 18:32 ` Wolfgang Schuster
2020-07-18 20:09   ` Fabrice Couvreur
2020-07-18 20:22     ` Wolfgang Schuster
2020-07-18 20:25       ` Fabrice Couvreur
2020-07-18 20:34         ` Fabrice Couvreur
2020-07-18 20:36         ` Wolfgang Schuster
2020-07-18 21:35         ` Otared Kavian
2020-07-18 22:05           ` Otared Kavian

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=31495829-66d7-c670-75e2-d1c538f38e9c@xs4all.nl \
    --to=j.hagen@xs4all.nl \
    --cc=haksan@me.com \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).