11 target: reference/standard_lib
22 template: reference
33 title: MoonScript v0.2.0 - Standard Library
4+ short_name: stdlib
45--
56
67The MoonScript installation comes with a small kernel of functions that can be
@@ -9,15 +10,19 @@ used to perform various common things.
910The entire library is currently contained in a single object. We can bring this
1011` moon ` object into scope by requiring ` "moon" ` .
1112
13+ ```moon
1214 require "moon"
1315 -- `moon.p` is the debug printer
1416 moon.p { hello: "world" }
17+ ```
1518
1619If you prefer to just inject all of the functions into the current scope, you
1720can require ` "moon.all" ` instead. The following has the same effect as above:
1821
22+ ```moon
1923 require "moon.all"
2024 p { hello: "world" }
25+ ```
2126
2227All of the functions are compatible with Lua in addition to MoonScript, but
2328some of them only make sense in the context of MoonScript.
@@ -33,7 +38,7 @@ All of the examples assume that the standard library has been included with
3338
3439### ` p(arg) `
3540
36- Prints a formatted version of an object. Excellent for introspecting the contents
41+ Prints a formatted version of an object. Excellent for inspecting the contents
3742of a table.
3843
3944
@@ -48,6 +53,7 @@ The environment of the function is set to a new table whose metatable will use
4853` scope ` to look up values. ` scope ` must be a table. If ` scope ` does not have an
4954entry for a value, it will fall back on the original environment.
5055
56+ ```moon
5157 my_env = {
5258 secret_function: -> print "shhh this is secret"
5359 say_hi: -> print "hi there!"
@@ -60,6 +66,7 @@ entry for a value, it will fall back on the original environment.
6066 say_hi!
6167
6268 run_with_scope fn, my_env
69+ ```
6370
6471
6572Note that any closure values will always take precedence against global name
@@ -79,18 +86,22 @@ whose `__index` is set to the next table.
7986
8087Returns the first argument.
8188
89+ ```moon
8290 a = { hello: "world" }
8391 b = { okay: "sure" }
8492
8593 extend a, b
8694
8795 print a.okay
96+ ```
8897
8998### ` copy(tbl) `
9099
91100Creates a shallow copy of a table, equivalent to:
92101
102+ ```moon
93103 copy = (arg) -> {k,v for k,v in pairs self}
104+ ```
94105
95106## Class/Object Functions
96107
@@ -103,24 +114,28 @@ Returns true if `value` is an instance of a MoonScript class, false otherwise.
103114If ` value ` is an instance of a MoonScript class, then return it's class object.
104115Otherwise, return the result of calling Lua's type method.
105116
117+ ```moon
106118 class MyClass
107119 nil
108120
109121 x = MyClass!
110122 assert type(x) == MyClass
123+ ```
111124
112125### ` bind_methods(obj) `
113126
114127Takes an instance of an object, returns a proxy to the object whose methods can
115128be called without providing self as the first argument.
116129
130+ ```moon
117131 obj = SomeClass!
118132
119133 bound_obj = bind_methods obj
120134
121135 -- following have the same effect
122136 obj\hello!
123137 bound_obj.hello!
138+ ```
124139
125140It lazily creates and stores in the proxy table the bound methods when they
126141are first called.
@@ -133,6 +148,7 @@ constructor of the class with the `obj` as the receiver.
133148In this example we add the functionality of ` First ` to an instance of ` Second `
134149without ever instancing ` First ` .
135150
151+ ```moon
136152 class First
137153 new: (@var) =>
138154 show_var: => print "var is:", @var
@@ -143,6 +159,7 @@ without ever instancing `First`.
143159
144160 a = Second!
145161 a\show_var!
162+ ```
146163
147164Be weary of name collisions when mixing in other classes, names will be
148165overwritten.
@@ -153,9 +170,10 @@ Inserts into `obj` methods from `other_obj` whose names are listen in
153170` method_names ` . The inserted methods are bound methods that will run with
154171` other_obj ` as the receiver.
155172
173+ ```moon
156174 class List
157- add: (item) => -- ...
158- remove: (item) => -- ...
175+ add: (item) => print "adding to", self
176+ remove: (item) => print "removing from", self
159177
160178 class Encapsulation
161179 new: =>
@@ -164,6 +182,7 @@ Inserts into `obj` methods from `other_obj` whose names are listen in
164182
165183 e = Encapsulation!
166184 e.add "something"
185+ ```
167186
168187### ` mixin_table(a, b, [names]) `
169188
@@ -183,8 +202,10 @@ being iterated over starting with the second item.
183202
184203For example, to sum all numbers in a list:
185204
205+ ```moon
186206 numbers = {4,3,5,6,7,2,3}
187207 sum = fold numbers, (a,b) -> a + b
208+ ```
188209
189210## Debug Functions
190211
0 commit comments