summaryrefslogtreecommitdiff
path: root/Data/DefaultContent/Libraries/luafun/doc/operators.rst
blob: 2f3edd433665578ad072d15d5d7b03bd6f22067f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
Operators
=========

.. module:: fun.operator

This auxiliary module exports a set of Lua operators as intrinsic functions
to use with the library high-order primitives.

.. contents::

.. note:: **op** can be used as a shortcut to **operator**.

Comparison operators
--------------------

.. seealso:: `Lua Relational Operators
              <http://www.lua.org/manual/5.2/manual.html#3.4.3>`_

.. function:: le(a, b)

   :returns: **a** <= **b**

.. function:: lt(a, b)

   :returns: **a** < **b**

.. function:: eq(a, b)

   :returns: **a** == **b**

.. function:: ne(a, b)

   :returns: **a** ~= **b**

.. function:: ge(a, b)

   :returns: **a** >= **b**

.. function:: gt(a, b)

   :returns: **a** > **b**

Arithmetic operators
--------------------

.. seealso:: `Lua Arithmetic Operators 
              <http://www.lua.org/manual/5.2/manual.html#3.4.1>`_

.. function:: add(a, b)

   :returns: **a** + **b**

.. function:: div(a, b)

    An alias for :func:`truediv`.

.. function:: truediv(a, b)

   :returns: **a** / **b**

   Performs "true" float division.
   Examples:

   .. code-block:: lua

    > print(operator.div(10, 3))
    3.3333333333333
    > print(operator.div(-10, 3))
    -3.3333333333333

.. function:: floordiv(a, b)

   :returns: math.floor(**a** / **b**)

   Performs division where a result is rounded down. Examples:

   .. code-block:: lua

    > print(operator.floordiv(10, 3))
    3
    > print(operator.floordiv(12, 3))
    4
    > print(operator.floordiv(-10, 3))
    -4
    > print(operator.floordiv(-12, 3))
    -4

.. function:: intdiv(a, b)

   Performs C-like integer division.

   Equvalent to:

   .. code-block:: lua

    function(a, b)
        local q = a / b
        if a >= 0 then return math.floor(q) else return math.ceil(q) end
    end

   Examples:

   .. code-block:: lua

    > print(operator.floordiv(10, 3))
    3
    > print(operator.floordiv(12, 3))
    4
    > print(operator.floordiv(-10, 3))
    -3
    > print(operator.floordiv(-12, 3))
    -4

.. function:: mod(a, b)

   :returns: **a** % **b**

   .. note:: Result has same sign as **divisor**. Modulo in Lua is defined as
             ``a % b == a - math.floor(a/b)*b``.

   Examples:

   .. code-block:: lua
    :emphasize-lines: 5-6

    > print(operator.mod(10, 2))
    0
    > print(operator.mod(10, 3))
    2
    print(operator.mod(-10, 3))
    2 -- == -1 in C, Java, JavaScript and but not in Lua, Python, Haskell!

.. function:: neq(a)

   :returns: -**a**

.. function:: unm(a)

   Unary minus. An alias for :func:`neq`.

.. function:: pow(a, b)

   :returns: math.pow(**a**, **b**)

.. function:: sub(a, b)

   :returns: **a** - **b**

String operators
----------------

.. seealso:: `Lua Concatenation Operator
              <http://www.lua.org/manual/5.2/manual.html#3.4.5>`_

.. function:: concat(a, b)

   :returns: **a** .. **b**

.. function:: len(a)

   :returns: # **a**

.. function:: length(a)

   An alias for :func:`len`.

Logical operators
-----------------

.. seealso:: `Lua Logical Operators
              <http://www.lua.org/manual/5.2/manual.html#3.4.4>`_

.. function:: land(a, b)

   :returns: **a** and **b**

.. function:: lor(a, b)

   :returns: **a** or **b**

.. function:: lnot(a)

   :returns: not **a**

.. function:: truth(a)

   :returns: not not **a**

   Return ``true`` if **a** is true, and ``false`` otherwise. Examples:

   .. code-block:: lua

    > print(operator.truth(1))
    true
    > print(operator.truth(0))
    true -- It is Lua, baby!
    > print(operator.truth(nil))
    false
    > print(operator.truth(""))
    true
    > print(operator.truth({}))
    true