{ target: “reference/standard_lib” template: “reference” title: “Standard Library” short_name: “stdlib” }

The MoonScript installation comes with a small kernel of functions that can be used to perform various common things.

The entire library is currently contained in a single object. We can bring this moon object into scope by requiring "moon".

moon = require "moon"
-- `moon.p` is the debug printer
moon.p { hello: "world" }

If you prefer to just inject all of the functions into the current scope, you can require "moon.all" instead. The following has the same effect as above:

require "moon.all"
p { hello: "world" }

All of the functions are compatible with Lua in addition to MoonScript, but some of them only make sense in the context of MoonScript.

MoonScript Standard Library

This is an overview of all the included functions. All of the examples assume that the standard library has been included with require "moon.all".

Printing Functions


Prints a formatted version of an object. Excellent for inspecting the contents of a table.

Table Functions

run_with_scope(fn, scope, [args...])

Mutates the environment of function fn and runs the function with any extra arguments in args.... Returns the result of the function.

The environment of the function is set to a new table whose metatable will use scope to look up values. scope must be a table. If scope does not have an entry for a value, it will fall back on the original environment.

my_env = {
  secret_function: -> print "shhh this is secret"
  say_hi: -> print "hi there!"

say_hi = -> print "I am a closure"

fn = ->

run_with_scope fn, my_env

Note that any closure values will always take precedence against global name lookups in the environment. In the example above, the say_hi in the environment has been shadowed by the local variable say_hi.

defaultbl([tbl,] fn)

Sets the __index of table tbl to use the function fn to generate table values when a missing key is looked up.

extend(arg1, arg2, [rest...])

Chains together a series of tables by their metatable’s __index property. Overwrites the metatable of all objects except for the last with a new table whose __index is set to the next table.

Returns the first argument.

a = { hello: "world" }
b = { okay: "sure" }

extend a, b

print a.okay


Creates a shallow copy of a table, equivalent to:

copy = (arg) -> {k,v for k,v in pairs self}

Class/Object Functions


Returns true if value is an instance of a MoonScript class, false otherwise.


If value is an instance of a MoonScript class, then return it’s class object. Otherwise, return the result of calling Lua’s type method.

class MyClass

x = MyClass!
assert type(x) == MyClass


Takes an instance of an object, returns a proxy to the object whose methods can be called without providing self as the first argument.

obj = SomeClass!

bound_obj = bind_methods obj

-- following have the same effect

It lazily creates and stores in the proxy table the bound methods when they are first called.

mixin(obj, class, [args...])

Copies the methods of a class cls into the table obj, then calls the constructor of the class with the obj as the receiver.

In this example we add the functionality of First to an instance of Second without ever instancing First.

class First
  new: (@var) =>
  show_var: => print "var is:", @var

class Second
  new: =>
    mixin self, First, "hi"

a = Second!

Be weary of name collisions when mixing in other classes, names will be overwritten.

mixin_object(obj, other_obj, method_names)

Inserts into obj methods from other_obj whose names are listed in method_names. The inserted methods are bound methods that will run with other_obj as the receiver.

class List 
  add: (item) => print "adding to", self
  remove: (item) => print "removing from", self

class Encapsulation
  new: =>
    @list = List!
    mixin_object self, @list, {"add", "remove"}

e = Encapsulation!
e.add "something"

mixin_table(a, b, [names])

Copies the elements of table b into table a. If names is provided, then only those names are copied.

Misc Functions

fold(items, fn)

Calls function fn repeatedly with the accumulated value and the current value by iterating over items. The accumulated value is the result of the last call to fn, or, in the base case, the first value. The current value is the value being iterated over starting with the second item.

items is a normal array table.

For example, to sum all numbers in a list:

numbers = {4,3,5,6,7,2,3}
sum = fold numbers, (a,b) -> a + b

Debug Functions

debug.upvalue(fn, key[, value])

Gets or sets the value of an upvalue for a function by name.





**目录**{: #toc }* TOC{:toc}# 蜜罐在风控中经常要面临这个问题:哪部分流量来自黑产?可信度高吗?这关系到现有策略效果评估、问题定位、新策略效果评估、模型训练等多个环节。解决这类问题时,蜜罐就起到了一定作用。## 什么是蜜罐?通过一些手段,引诱黑灰产...… Continue reading

Apache APISIX在SAE应用市场发布

Published on December 09, 2019


Published on December 06, 2019