ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Jeong Dal <haksan@me.com>
To: Hans Hagen <j.hagen@xs4all.nl>
Cc: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: running lua in metafun and in ConTeXt
Date: Sun, 12 Apr 2020 08:19:30 +0900	[thread overview]
Message-ID: <BAE69044-E5FD-4BE5-8481-B10DD5DDC755@me.com> (raw)
In-Reply-To: <97a35742-0ec2-2d9a-eec6-648c288fc073@xs4all.nl>

Dear Hans,

Thank you for your nice code.
lua.MP.Whatever is something!

Have a nice weekend.

Best regards,

Dalyoung

> 2020. 4. 12. 오전 2:08, Hans Hagen <j.hagen@xs4all.nl> 작성:
> 
> On 4/11/2020 4:15 PM, Jeong Dal wrote:
>> Dear all,
>> The problem is solved by using the namespace of lua as below:
>> I am not sure what I did is the right way.
>> If it is not the right way, please let me know.
>> Thanks for reading.
>> Best regards,
>> Dalyoung
>> \startluacode
>> P={}
>> combi = P
>> function P.fact (n)
>> if n <= 0 then
>> return 1
>> else
>> return n * P.fact(n-1)
>> end
>> end
>> function P.ncr(n,r)
>> return P.fact(n)/(P.fact(r)*P.fact(n-r))
>> end
>> combi = {
>> fact = fact,
>> ncr = ncr,
>> }
>> \stopluacode
>> \startbuffer[fig121]
>> numeric n,r,s,u,dx,dy,tt; u := 1.8cm;
>> path p, q;
>> pair A,B,start,now;
>> 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(P.fact(" & decimal n & ")/(P.fact(" & decimal r & ")*P.fact(" & decimal s &" )))");
>> tt := lua("mp.print(P.ncr(" & decimal n & "," & decimal r & " ))");
>> now := start+r*right*dx;
>> dotlabel.top(textext("$\displaystyle {" & decimal n & "\choose" & decimal r & "} = "& decimal tt & "$"),now);
>> draw now -- (now+A);
>> draw now -- (now+B);
>> endfor;
>> endfor;
>> \stopbuffer
> Sunday afternoon educational moment (that you can wikify), four variants:
> 
> 1 : A more metafunish alternative of your example.
> 2 : The same but avoiding a temporary variable.
> 3 : Less code and clutter, the real deal.
> 4 : Idem, but permits more tuning at the TeX end.
> 
> \unexpanded\def\MyWhatever#1#2#3%
>  {$\displaystyle{#1\choose #2} = #3$}
> 
> \startluacode
> local function fact(n)
>    if n <= 0 then
>        return 1
>    else
>        return n * fact(n - 1)
>    end
> end
> 
> local function whatever(n,r)
>   return fact(n) / (fact(r) * fact(n-r))
> end
> 
> MP.WhateverA = whatever
> 
> function MP.WhateverB(n,r)
>   mp.quoted("%.0f",whatever(n,r))
> end
> 
> function MP.WhateverC(n,r)
>   mp.quoted([[$\displaystyle{%.0f\choose %.0f} = %.0f$]],n,r,whatever(n,r))
> end
> 
> function MP.WhateverD(n,r)
>   mp.quoted([[\MyWhatever{%.0f}{%.0f}{%.0f}]],n,r,whatever(n,r))
> end
> \stopluacode
> 
> \startbuffer[fig121]
>    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;
>            now := start + r*right*dx;
>            draw (now + A) -- now -- (now + B);
> 
>            tt := lua.MP.WhateverA(n,r) ;
> 
>            dotlabel.top(textext("$\displaystyle {" & decimal n & "\choose" & decimal r & "} = "& decimal tt & "$"),now);
> 
>            dotlabel.top(textext("$\displaystyle {" & decimal n & "\choose" & decimal r & "} = "& lua.MP.WhateverB(n,r) & "$"),now);
> 
>            dotlabel.top(textext(lua.MP.WhateverC(n,r)),now);
> 
>            dotlabel.top(textext(lua.MP.WhateverD(n,r)),now);
> 
>        endfor;
>    endfor;
> \stopbuffer
> 
> \starttext
> 
>    {\switchtobodyfont[11pt]\processMPbuffer[fig121]}
> 
> \stoptext
> 
> 
> 
> -----------------------------------------------------------------
>                                          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-04-11 23:19 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.2082.1586557063.1332.ntg-context@ntg.nl>
2020-04-11 12:32 ` Jeong Dal
2020-04-11 12:48   ` Otared Kavian
2020-04-11 14:15 ` Jeong Dal
2020-04-11 17:08   ` Hans Hagen
2020-04-11 23:19     ` Jeong Dal [this message]
2020-04-12  9:44       ` Hans Hagen
2020-04-12 13:02         ` Jeong Dal

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=BAE69044-E5FD-4BE5-8481-B10DD5DDC755@me.com \
    --to=haksan@me.com \
    --cc=j.hagen@xs4all.nl \
    --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).