Last active
August 29, 2015 14:04
-
-
Save szensk/ef4d24a9b770afe6c641 to your computer and use it in GitHub Desktop.
MoonScript example #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local Object | |
do | |
local _base_0 = { | |
__inherited = function(self, child) | |
for k, v in pairs(self.__base) do | |
child.__base[k] = child.__base[k] or v | |
end | |
end | |
} | |
_base_0.__index = _base_0 | |
local _class_0 = setmetatable({ | |
__init = function() end, | |
__base = _base_0, | |
__name = "Object" | |
}, { | |
__index = _base_0, | |
__call = function(cls, ...) | |
local _self_0 = setmetatable({}, _base_0) | |
cls.__init(_self_0, ...) | |
return _self_0 | |
end | |
}) | |
_base_0.__class = _class_0 | |
Object = _class_0 | |
end | |
local Animal | |
do | |
local _parent_0 = Object | |
local _base_0 = { | |
meep = function(self) | |
return print(tostring(self.name) .. " is currently " .. tostring(self.status) .. ".") | |
end, | |
__tostring = function(self) | |
return tostring(self.__class.__name) .. ": " .. tostring(self.name) | |
end | |
} | |
_base_0.__index = _base_0 | |
setmetatable(_base_0, _parent_0.__base) | |
local _class_0 = setmetatable({ | |
__init = function(self, name, status) | |
if name == nil then | |
name = 'Unknown animal' | |
end | |
if status == nil then | |
status = 'not on meep' | |
end | |
self.name, self.status = name, status | |
end, | |
__base = _base_0, | |
__name = "Animal", | |
__parent = _parent_0 | |
}, { | |
__index = function(cls, name) | |
local val = rawget(_base_0, name) | |
if val == nil then | |
return _parent_0[name] | |
else | |
return val | |
end | |
end, | |
__call = function(cls, ...) | |
local _self_0 = setmetatable({}, _base_0) | |
cls.__init(_self_0, ...) | |
return _self_0 | |
end | |
}) | |
_base_0.__class = _class_0 | |
if _parent_0.__inherited then | |
_parent_0.__inherited(_parent_0, _class_0) | |
end | |
Animal = _class_0 | |
end | |
local SpeakingAnimal | |
do | |
local _parent_0 = Animal | |
local _base_0 = { | |
speak = function(self) | |
return print(tostring(self.name) .. " says " .. tostring(self.sound) .. ".") | |
end | |
} | |
_base_0.__index = _base_0 | |
setmetatable(_base_0, _parent_0.__base) | |
local _class_0 = setmetatable({ | |
__init = function(self, super) | |
if _parent_0 == nil then | |
super = 'giraffe noises' | |
end | |
self.super = super | |
return _parent_0.__init(self, self.name, self.status) | |
end, | |
__base = _base_0, | |
__name = "SpeakingAnimal", | |
__parent = _parent_0 | |
}, { | |
__index = function(cls, name) | |
local val = rawget(_base_0, name) | |
if val == nil then | |
return _parent_0[name] | |
else | |
return val | |
end | |
end, | |
__call = function(cls, ...) | |
local _self_0 = setmetatable({}, _base_0) | |
cls.__init(_self_0, ...) | |
return _self_0 | |
end | |
}) | |
_base_0.__class = _class_0 | |
if _parent_0.__inherited then | |
_parent_0.__inherited(_parent_0, _class_0) | |
end | |
SpeakingAnimal = _class_0 | |
end | |
local Dog | |
do | |
local _parent_0 = SpeakingAnimal | |
local _base_0 = { } | |
_base_0.__index = _base_0 | |
setmetatable(_base_0, _parent_0.__base) | |
local _class_0 = setmetatable({ | |
__init = function(self, name, status) | |
if status == nil then | |
status = 'barking' | |
end | |
self.name, self.status = name, status | |
self.sound = ('roof '):rep(4) .. 'roof' | |
return _parent_0.__init(self, self.sound) | |
end, | |
__base = _base_0, | |
__name = "Dog", | |
__parent = _parent_0 | |
}, { | |
__index = function(cls, name) | |
local val = rawget(_base_0, name) | |
if val == nil then | |
return _parent_0[name] | |
else | |
return val | |
end | |
end, | |
__call = function(cls, ...) | |
local _self_0 = setmetatable({}, _base_0) | |
cls.__init(_self_0, ...) | |
return _self_0 | |
end | |
}) | |
_base_0.__class = _class_0 | |
local self = _class_0 | |
self.alignment = 'good' | |
if _parent_0.__inherited then | |
_parent_0.__inherited(_parent_0, _class_0) | |
end | |
Dog = _class_0 | |
end | |
local Cat | |
do | |
local _parent_0 = SpeakingAnimal | |
local _base_0 = { } | |
_base_0.__index = _base_0 | |
setmetatable(_base_0, _parent_0.__base) | |
local _class_0 = setmetatable({ | |
__init = function(self, name, status) | |
if status == nil then | |
status = 'sleeping' | |
end | |
self.name, self.status = name, status | |
self.sound = 'meow' | |
return _parent_0.__init(self, self.sound) | |
end, | |
__base = _base_0, | |
__name = "Cat", | |
__parent = _parent_0 | |
}, { | |
__index = function(cls, name) | |
local val = rawget(_base_0, name) | |
if val == nil then | |
return _parent_0[name] | |
else | |
return val | |
end | |
end, | |
__call = function(cls, ...) | |
local _self_0 = setmetatable({}, _base_0) | |
cls.__init(_self_0, ...) | |
return _self_0 | |
end | |
}) | |
_base_0.__class = _class_0 | |
local self = _class_0 | |
self.alignment = 'evil' | |
if _parent_0.__inherited then | |
_parent_0.__inherited(_parent_0, _class_0) | |
end | |
Cat = _class_0 | |
end | |
local suushi | |
do | |
local _with_0 = Cat('Suushi') | |
_with_0.age = 7 | |
_with_0:meep() | |
_with_0:speak() | |
_with_0.status = 'scared' | |
_with_0:meep() | |
suushi = _with_0 | |
end | |
local ozzy | |
do | |
local _with_0 = Dog('Ozzy') | |
_with_0.age = 10 | |
_with_0:meep() | |
_with_0:speak() | |
ozzy = _with_0 | |
end | |
local unknown = Animal() | |
unknown:meep() | |
print(suushi) | |
print(ozzy) | |
return print(unknown) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Meep! It's twitter for animals. | |
class Object | |
-- __inherited method is necessary if you want children to inherit metamethods | |
__inherited: (child) => | |
for k,v in pairs @__base | |
-- the or= operator only assigns if the left-hand-side is false/nil | |
child.__base[k] or= v | |
class Animal extends Object | |
-- functions/methods can take default arguments | |
-- new is called with the creation of a new object | |
-- @ is an alias for 'self' | |
-- if followed by a variable name it is an alias for 'self.' | |
new: (@name='Unknown animal', @status='not on meep') => | |
-- this is empty but that's OK! | |
-- methods are defined by the fat arrow => | |
meep: => | |
print "#{ @name } is currently #{ @status }." | |
-- __tostring metamethod is necessary to pretty print the object | |
-- @@ is an alias for self.__class (ie the object's class) | |
__tostring: => "#{ @@__name }: #{ @name }" | |
class SpeakingAnimal extends Animal | |
new: (@super='giraffe noises') => | |
super @name, @status | |
speak: => | |
print "#{ @name } says #{ @sound }." | |
class Dog extends SpeakingAnimal | |
@alignment = 'good' --class variable | |
new: (@name, @status='barking') => | |
@sound = 'roof '\rep(4) .. 'roof' -- object variable | |
super @sound -- call our parent's new method | |
class Cat extends SpeakingAnimal | |
@alignment = 'evil' | |
new: (@name, @status='sleeping') => | |
@sound = 'meow' | |
super @sound | |
-- the with block is a shortcut suushi.age = 7 | |
suushi = with Cat 'Sushi' | |
.age = 7 | |
\meep! -- and suushi\meep and so on | |
\speak! -- \ is method invokation | |
.status = 'scared' | |
\meep! | |
oswald = with Dog 'Oswald' | |
.age = 10 | |
\meep! | |
\speak! | |
-- objects can also be created without the with block | |
unknown = Animal! -- ! is shorthand for () | |
--Animal() calls __call() which in turn calls the class new method | |
unknown\meep! | |
print suushi -- print invokes an object's __tostring metamethod | |
print oswald | |
print unknown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local create, yield, resume | |
do | |
local _obj_0 = coroutine | |
create, yield, resume = _obj_0.create, _obj_0.yield, _obj_0.resume | |
end | |
local permgen | |
permgen = function(a, n) | |
if n == 0 then | |
yield(a) | |
end | |
for i = 1, n do | |
a[n], a[i] = a[i], a[n] | |
permgen(a, n - 1) | |
a[n], a[i] = a[i], a[n] | |
end | |
end | |
local permute | |
permute = function(a) | |
local coro = create(function() | |
return permgen(a, #a) | |
end) | |
return function() | |
local code, res = resume(coro) | |
return res | |
end | |
end | |
local evens | |
do | |
local _accum_0 = { } | |
local _len_0 = 1 | |
for i = 1, 8 do | |
if i % 2 == 0 then | |
_accum_0[_len_0] = i | |
_len_0 = _len_0 + 1 | |
end | |
end | |
evens = _accum_0 | |
end | |
local permutations | |
do | |
local _accum_0 = { } | |
local _len_0 = 1 | |
for t in permute(evens) do | |
_accum_0[_len_0] = table.concat(t, ",") | |
_len_0 = _len_0 + 1 | |
end | |
permutations = _accum_0 | |
end | |
print("We have " .. tostring(#permutations) .. " permutations:") | |
table.sort(permutations, function(a, b) | |
return a < b | |
end) | |
return print(table.concat(permutations, "\n")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import create, yield, resume from coroutine | |
-- generate permutations | |
permgen = (a, n) -> | |
if n == 0 | |
yield(a) --yield this permutation | |
for i=1, n | |
-- put i-th element as the last one | |
a[n], a[i] = a[i], a[n] | |
-- generate all permutations of the other elements | |
permgen(a, n - 1) | |
-- restore i-th element | |
a[n], a[i] = a[i], a[n] | |
-- an iterator over all permutations of table | |
permute = (a) -> | |
--coroutine.create takes a function | |
coro = create -> permgen(a, #a) | |
return -> --create an iterator | |
code, res = resume(coro) | |
return res | |
-- i = 1, 8 is range of the list comprehension | |
-- it yields i for each integer in the range when the condition is true. | |
evens = [i for i = 1, 8 when i % 2 == 0] | |
-- table.concat concatenates a table into a single string. | |
-- The second argument is the separator between each element. | |
-- in this case it will concatenate the table {2,4,6,8} into the string "2,4,6,8". | |
permutations = [table.concat t, "," for t in permute evens] | |
-- #{expression} is string iterpolation. It ONLY works within strings delimited by double quotes. | |
print "We have #{ #permutations } permutations:" | |
-- table.sort takes an optional second argument: a sorting function. | |
table.sort permutations, (a, b) -> a < b | |
-- and now we print them out in sorted order. | |
print table.concat permutations, "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment