ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* How to define such a function in lua? Sorry I don't have a name on it.
@ 2008-12-04  6:10 Zhichu Chen
  2008-12-04  9:28 ` Hans Hagen
  0 siblings, 1 reply; 6+ messages in thread
From: Zhichu Chen @ 2008-12-04  6:10 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi,

I'm learning to use luatex these days, one day actually. It's very
interesting. Although reference manuals are available, I still don't
know how to implement my designation.

The thing is, I know there's a "self" variable for object-oriented
programming, like
==================================================================
\startluacode
testdata = { a = 0,
             b = 0,
             c = 0,
             plus = function (self)
                        self.c = self.a + self.b
                    end
           }
mytest = testdata
mytest.a=10
mytest.b=100
mytest:plus()
tex.print(mytest.c)
\stopluacode
==================================================================
OK, but what if I wanna do something more, like substract? I would
like to use another object, like
==================================================================
\startluacode
testdata = { a = 0,
             b = 0,
             c = 0,
             calc = Calc
           }
Calc     = { plus = function (self)
                        self.c = self.a + self.b
                    end,
             substract = function (self)
                        self.c = self.a - self.b
                    end
           }
mytest = testdata
mytest.a=10
mytest.b=100
mytest.calc:plus()
tex.print(mytest.c)
\stopluacode
==================================================================
Of course this won't work, for the "self" in Calc refers to mytest.calc
rather than mytest. How to implement things like that?

Thanks in advance.

-- 
Best Regards
Chen
----------------------------------------------------------------

          Zhi-chu Chen | Shanghai Synchrotron Radiation Facility
         No. 2019 | Jialuo Rd. | Jiading | Shanghai | P.R. China
             tel: 086 21 5955 3405 | zhichu.chen.googlepages.com
                                               | www.sinap.ac.cn
----------------------------------------------------------------
___________________________________________________________________________________
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  : https://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: How to define such a function in lua? Sorry I don't have a name on it.
  2008-12-04  6:10 How to define such a function in lua? Sorry I don't have a name on it Zhichu Chen
@ 2008-12-04  9:28 ` Hans Hagen
  2008-12-04 10:17   ` Zhichu Chen
  0 siblings, 1 reply; 6+ messages in thread
From: Hans Hagen @ 2008-12-04  9:28 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Zhichu Chen wrote:
> Hi,
> 
> I'm learning to use luatex these days, one day actually. It's very
> interesting. Although reference manuals are available, I still don't
> know how to implement my designation.
> 
> The thing is, I know there's a "self" variable for object-oriented
> programming, like
> ==================================================================
> \startluacode
> testdata = { a = 0,
>              b = 0,
>              c = 0,
>              plus = function (self)
>                         self.c = self.a + self.b
>                     end
>            }
> mytest = testdata
> mytest.a=10
> mytest.b=100
> mytest:plus()
> tex.print(mytest.c)
> \stopluacode
> ==================================================================
> OK, but what if I wanna do something more, like substract? I would
> like to use another object, like
> ==================================================================
> \startluacode
> testdata = { a = 0,
>              b = 0,
>              c = 0,
>              calc = Calc
>            }
> Calc     = { plus = function (self)
>                         self.c = self.a + self.b
>                     end,
>              substract = function (self)
>                         self.c = self.a - self.b
>                     end
>            }
> mytest = testdata
> mytest.a=10
> mytest.b=100
> mytest.calc:plus()
> tex.print(mytest.c)
> \stopluacode
> ==================================================================
> Of course this won't work, for the "self" in Calc refers to mytest.calc
> rather than mytest. How to implement things like that?

Calc     = { add = function (self)
                         self.c = self.a + self.b
                     end,
              substract = function (self)
                         self.c = self.a - self.b
                     end
            }

testdata = { a = 0,
              b = 0,
              c = 0,
              calc = function(self,f) return Calc[f](self) end
            }

mytest = testdata
mytest.a=10
mytest.b=100
Calc.add(testdata)
print(mytest.c)
testdata:calc("substract")
print(mytest.c)

but it might make more sense to look into metatables (i assume that you 
own the programming in lua book which explains that)

Hans


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
      tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
                                              | 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://tex.aanhet.net
archive  : https://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: How to define such a function in lua? Sorry I don't have a name on it.
  2008-12-04  9:28 ` Hans Hagen
@ 2008-12-04 10:17   ` Zhichu Chen
  2008-12-05  0:05     ` Arthur Reutenauer
  0 siblings, 1 reply; 6+ messages in thread
From: Zhichu Chen @ 2008-12-04 10:17 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On Thu, Dec 4, 2008 at 5:28 PM, Hans Hagen <pragma@wxs.nl> wrote:
> Zhichu Chen wrote:
>> Hi,
>>
>> I'm learning to use luatex these days, one day actually. It's very
>> interesting. Although reference manuals are available, I still don't
>> know how to implement my designation.
>>
>> The thing is, I know there's a "self" variable for object-oriented
>> programming, like
>> ==================================================================
>> \startluacode
>> testdata = { a = 0,
>>              b = 0,
>>              c = 0,
>>              plus = function (self)
>>                         self.c = self.a + self.b
>>                     end
>>            }
>> mytest = testdata
>> mytest.a=10
>> mytest.b=100
>> mytest:plus()
>> tex.print(mytest.c)
>> \stopluacode
>> ==================================================================
>> OK, but what if I wanna do something more, like substract? I would
>> like to use another object, like
>> ==================================================================
>> \startluacode
>> testdata = { a = 0,
>>              b = 0,
>>              c = 0,
>>              calc = Calc
>>            }
>> Calc     = { plus = function (self)
>>                         self.c = self.a + self.b
>>                     end,
>>              substract = function (self)
>>                         self.c = self.a - self.b
>>                     end
>>            }
>> mytest = testdata
>> mytest.a=10
>> mytest.b=100
>> mytest.calc:plus()
>> tex.print(mytest.c)
>> \stopluacode
>> ==================================================================
>> Of course this won't work, for the "self" in Calc refers to mytest.calc
>> rather than mytest. How to implement things like that?
>
> Calc     = { add = function (self)
>                         self.c = self.a + self.b
>                     end,
>              substract = function (self)
>                         self.c = self.a - self.b
>                     end
>            }
>
> testdata = { a = 0,
>              b = 0,
>              c = 0,
>              calc = function(self,f) return Calc[f](self) end
>            }
>
> mytest = testdata
> mytest.a=10
> mytest.b=100
> Calc.add(testdata)
> print(mytest.c)
> testdata:calc("substract")
> print(mytest.c)
Thanks
>
> but it might make more sense to look into metatables (i assume that you
> own the programming in lua book which explains that)
I don't have one :(, I'll take a good at it later.
>
> Hans
>
>
> -----------------------------------------------------------------
>                                           Hans Hagen | PRAGMA ADE
>               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
>      tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
>                                              | 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://tex.aanhet.net
> archive  : https://foundry.supelec.fr/projects/contextrev/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________
>



-- 
Best Regards
Chen
----------------------------------------------------------------

          Zhi-chu Chen | Shanghai Synchrotron Radiation Facility
         No. 2019 | Jialuo Rd. | Jiading | Shanghai | P.R. China
             tel: 086 21 5955 3405 | zhichu.chen.googlepages.com
                                               | www.sinap.ac.cn
----------------------------------------------------------------
___________________________________________________________________________________
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  : https://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: How to define such a function in lua? Sorry I don't have a name on it.
  2008-12-04 10:17   ` Zhichu Chen
@ 2008-12-05  0:05     ` Arthur Reutenauer
  2008-12-05 15:36       ` Zhichu Chen
  0 siblings, 1 reply; 6+ messages in thread
From: Arthur Reutenauer @ 2008-12-05  0:05 UTC (permalink / raw)
  To: Mailing list for ConTeXt users

>> but it might make more sense to look into metatables (i assume that you
>> own the programming in lua book which explains that)
>
> I don't have one :(, I'll take a good at it later.

  You really should look into the book, it has a whole chapter about
object-oriented programming in Lua, and explains useful conventions.

	Arthur
___________________________________________________________________________________
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  : https://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: How to define such a function in lua? Sorry I don't have a name on it.
  2008-12-05  0:05     ` Arthur Reutenauer
@ 2008-12-05 15:36       ` Zhichu Chen
  2008-12-06 14:41         ` Yue Wang
  0 siblings, 1 reply; 6+ messages in thread
From: Zhichu Chen @ 2008-12-05 15:36 UTC (permalink / raw)
  To: Mailing list for ConTeXt users

On Fri, Dec 5, 2008 at 8:05 AM, Arthur Reutenauer
<arthur.reutenauer@normalesup.org> wrote:
>>> but it might make more sense to look into metatables (i assume that you
>>> own the programming in lua book which explains that)
>>
>> I don't have one :(, I'll take a good at it later.
>
>  You really should look into the book, it has a whole chapter about
> object-oriented programming in Lua, and explains useful conventions.

Oh it's a long document. I almost took a day to finish it. I've got the main
idea now. Thanks.

I'm sorry, I got another question now. How to link two variables? Like if I
change one, the other would be changed as well. This may be a piece of cake
for you and please forgive my laziness.

>
>        Arthur
> ___________________________________________________________________________________
> 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  : https://foundry.supelec.fr/projects/contextrev/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________
>



-- 
Best Regards
Chen
----------------------------------------------------------------

          Zhi-chu Chen | Shanghai Synchrotron Radiation Facility
         No. 2019 | Jialuo Rd. | Jiading | Shanghai | P.R. China
             tel: 086 21 5955 3405 | zhichu.chen.googlepages.com
                                               | www.sinap.ac.cn
----------------------------------------------------------------
___________________________________________________________________________________
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  : https://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: How to define such a function in lua? Sorry I don't have a name on it.
  2008-12-05 15:36       ` Zhichu Chen
@ 2008-12-06 14:41         ` Yue Wang
  0 siblings, 0 replies; 6+ messages in thread
From: Yue Wang @ 2008-12-06 14:41 UTC (permalink / raw)
  To: mailing list for ConTeXt users

variables are only symbols in lua.
they are reference to values (a place in the memory).

On 12/5/08, Zhichu Chen <zhichu.chen@gmail.com> wrote:
> On Fri, Dec 5, 2008 at 8:05 AM, Arthur Reutenauer
> <arthur.reutenauer@normalesup.org> wrote:
> >>> but it might make more sense to look into metatables (i assume that you
> >>> own the programming in lua book which explains that)
> >>
> >> I don't have one :(, I'll take a good at it later.
> >
> >  You really should look into the book, it has a whole chapter about
> > object-oriented programming in Lua, and explains useful conventions.
>
> Oh it's a long document. I almost took a day to finish it. I've got the main
> idea now. Thanks.
>
> I'm sorry, I got another question now. How to link two variables? Like if I
> change one, the other would be changed as well. This may be a piece of cake
> for you and please forgive my laziness.
>
> >
> >        Arthur
> > ___________________________________________________________________________________
> > 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  : https://foundry.supelec.fr/projects/contextrev/
> > wiki     : http://contextgarden.net
> > ___________________________________________________________________________________
> >
>
>
>
> --
> Best Regards
> Chen
> ----------------------------------------------------------------
>
>          Zhi-chu Chen | Shanghai Synchrotron Radiation Facility
>         No. 2019 | Jialuo Rd. | Jiading | Shanghai | P.R. China
>             tel: 086 21 5955 3405 | zhichu.chen.googlepages.com
>                                               | www.sinap.ac.cn
> ----------------------------------------------------------------
> ___________________________________________________________________________________
> 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  : https://foundry.supelec.fr/projects/contextrev/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________
>
___________________________________________________________________________________
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  : https://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

end of thread, other threads:[~2008-12-06 14:41 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-12-04  6:10 How to define such a function in lua? Sorry I don't have a name on it Zhichu Chen
2008-12-04  9:28 ` Hans Hagen
2008-12-04 10:17   ` Zhichu Chen
2008-12-05  0:05     ` Arthur Reutenauer
2008-12-05 15:36       ` Zhichu Chen
2008-12-06 14:41         ` Yue Wang

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).