lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, 12 Dec 2001, Luiz Henrique de Figueiredo wrote:

> Could someone please volunteer and add Lua entries to the study below?
> 
>   http://merd.net/pixel/language-study/syntax-across-languages.html

OK, here's my take on it.  I'm not going to send it in; feel free to
forward it if you think it looks reasonable.

Jay

VARIOUS OPERATORS

assignment/declaration: =

function call: f(a,b,...)
Lua supports sugar for function calls as follows:
Table constructors: f{a=7} => f({a=7}), f{1,2,3} => f({1,2,3})
Strings: f"a", f'a', f[[a]] => f("a")

sequence: The empty string.  ; is an *optional* statement separator.

grouping expressions: ( ... )

block: begin ... end
Note that syntax has implicit blocks:
    while 1 do
        ...
    end     
    if foo then
        ...
    else
        ...
    end
    for i,v in dict do
        ...
    end

method invocation: object:method( ... )
Note that object:method(...) is sugar for object.method(self, ...)

package scope: no packages
people often use tables as packages: HTTP.get(...)

record selector: .

shallow equality/non-equality: == ~=

deep eq/non-eq: none (although strings are values; "a" == "a")

comparison: < > <= >= 

3-value compare: none

if_then_else: none
Quoting the Lua 4.0 manual:
    The second idiom is
        x = a and b or c
    which should be read as x = (a and b) or c. This idiom is equivalent to
        if a then x = b else x = c end
    provided that b is not nil.

type annotation: none

type cast: none

dictionary constructor: {a=b, c=d}
Arbitrary keys can be used with {[7]=b, ["c"]=>d}

range: none

tuple constructor: {a, b, c}
...depending on your definition of tuple.  {a, b, c} is another way of 
writing the table constructor {[1]=a, [2]=b, [3]=c}.

dictionary access: a[e]
Also a.foo => a["foo"]

throw: error()

catch: none
Well, there's call(), but that's not used casually.

runtime evaluation: dostring("...")

STRINGS

verbatim: "...", '...', [[...]]

with interpolation: none

convert something to a string: tostring

concatenation: ..

duplicate n times: strrep(s, n)

upper/lower case character: none

upper/lower case string: strupper(), strlower()

ascii to character: strchar

char to ascii: strbyte

BOOLEANS

false: nil

true: anything not nil

logical not: not

logical or/and, short circuit: or/and

non short circuit: none

BAGS AND LISTS

list concatenation: none

list flattening: none

list constructor: {a, b, c}
...depending on your definition of list.  {a, b, c} is another way of 
writing the table constructor {[1]=a, [2]=b, [3]=c}.

list/array indexing: a[i]

list cons: none

iterate: foreachi(l, f)
Common practice is:
    for i=1,getn(l) do
        do_something_with(l[i])
    end

transform: none

transform two lists in parallel: none

find an element: none

keep elements matching: none

f(... f(f(init, e1), e2) ..., en): none

f(e1, f(e2, ... f(en, init) ...)): none

split a list in 2 based on a predicate: none

is an element in the list: none

is the predicate true for an element: none

is the predicate true for every element: none

smallest/biggest element: none

list out of a bag: none

list size: getn(l)

iterate with index: none
Again, common practice is:
    for i=1,getn(l) do
        do_something_with(l[i])
    end

sort: sort

remove duplicates: none

list of couples from 2 lists: none

2 lists from a list of couples: none

reverse: none

lookup an element in an association list: none

MATHEMATICS

power: ^

square root/exponental/absolute value: sqrt/exp/abs

trigonometry: cos/sin/tan

logarithm: log/log10

euclidian division: none

modulo of -3/2 is 1: none

modulo of -3/2 is -1: mod

round/floor/ceil: none/floor/ceil