Skip to content

Commit f45a2aa

Browse files
committed
more std lib docs
1 parent d50ff29 commit f45a2aa

File tree

1 file changed

+112
-9
lines changed

1 file changed

+112
-9
lines changed

docs/standard_lib.md

Lines changed: 112 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ of a table.
4242
### `run_with_scope(fn, scope, [args...])`
4343

4444
Mutates the environment of function `fn` and runs the function with any extra
45-
args in `args...`. Returns the result of the function.
45+
arguments in `args...`. Returns the result of the function.
4646

4747
The environment of the function is set to a new table whose metatable will use
4848
`scope` to look up values. `scope` must be a table. If `scope` does not have an
@@ -71,20 +71,123 @@ environment has been shadowed by the local variable `say_hi`.
7171
Sets the `__index` of table `tbl` to use the function `fn` to generate table
7272
values when a missing key is looked up.
7373

74-
### `extend`
75-
### `copy`
74+
### `extend(arg1, arg2, [rest...])`
75+
76+
Chains together a series of tables by their metatable's `__index` property.
77+
Overwrites the metatable of all objects exept for the last with a new table
78+
whose `__index` is set to the next table.
79+
80+
Returns the first argument.
81+
82+
a = { hello: "world" }
83+
b = { okay: "sure" }
84+
85+
extend a, b
86+
87+
print a.okay
88+
89+
### `copy(tbl)`
90+
91+
Creates a shallow copy of a table, equivalent to:
92+
93+
copy = (arg) -> {k,v for k,v in pairs self}
7694

7795
## Class/Object Functions
7896

79-
### `bind_methods`
80-
### `mixin`
81-
### `mixin_object`
82-
### `mixin_table`
97+
### `is_object(value)`
98+
99+
Returns true if `value` is an instance of a MoonScript class, false otherwise.
100+
101+
### `type(value)`
102+
103+
If `value` is an instance of a MoonScript class, then return it's class object.
104+
Otherwise, return the result of calling Lua's type method.
105+
106+
class MyClass
107+
nil
108+
109+
x = MyClass!
110+
assert type(x) == MyClass
111+
112+
### `bind_methods(obj)`
113+
114+
Takes an instance of an object, returns a proxy to the object whose methods can
115+
be called without providing self as the first argument.
116+
117+
obj = SomeClass!
118+
119+
bound_obj = bind_methods obj
120+
121+
-- following have the same effect
122+
obj\hello!
123+
bound_obj.hello!
124+
125+
It lazily creates and stores in the proxy table the bound methods when they
126+
are first called.
127+
128+
### `mixin(obj, class, [args...])`
129+
130+
Copies the methods of a class `cls` into the table `obj`, then calls the
131+
constructor of the class with the `obj` as the receiver.
132+
133+
In this example we add the functionality of `First` to an instance of `Second`
134+
without ever instancing `First`.
135+
136+
class First
137+
new: (@var) =>
138+
show_var: => print "var is:", @var
139+
140+
class Second
141+
new: =>
142+
mixin self, First, "hi"
143+
144+
a = Second!
145+
a\show_var!
146+
147+
Be weary of name collisions when mixing in other classes, names will be
148+
overwritten.
149+
150+
### `mixin_object(obj, other_obj, method_names)`
151+
152+
Inserts into `obj` methods from `other_obj` whose names are listen in
153+
`method_names`. The inserted methods are bound methods that will run with
154+
`other_obj` as the receiver.
155+
156+
class List
157+
add: (item) => -- ...
158+
remove: (item) => -- ...
159+
160+
class Encapsulation
161+
new: =>
162+
@list = List!
163+
mixin_object self, @list, {"add", "remove"}
164+
165+
e = Encapsulation!
166+
e.add "something"
167+
168+
### `mixin_table(a, b, [names])`
169+
170+
Copies the elements of table `b` into table `a`. If names is provided, then
171+
only those names are copied.
83172

84173
## Misc Functions
85174

86-
### `fold`
175+
### `fold(items, fn)`
176+
177+
Calls function `fn` repeatedly with the accumulated value and the current value
178+
by iterating over `items`. The accumulated value is the result of the last call
179+
to `fn`, or, in the base case, the first value. The current value is the value
180+
being iterated over starting with the second item.
181+
182+
`items` is a normal array table.
183+
184+
For example, to sum all numbers in a list:
185+
186+
numbers = {4,3,5,6,7,2,3}
187+
sum = fold numbers, (a,b) -> a + b
87188

88189
## Debug Functions
89190

90-
### `debug.upvalue`
191+
### `debug.upvalue(fn, key[, value])`
192+
193+
Gets or sets the value of an upvalue for a function by name.

0 commit comments

Comments
 (0)