But this simplicity can have a downside, as there might not be canonical ways of doing things that users are accustomed to. For example, object orientation. It could be a hard sell to someone coming from an object-oriented language, when you have to understand, and choose between multiple different styles of object-orientation and implement them consistently in your project, or use one of the many libraries made just for this. For example, see http://lua-users.org/wiki/ObjectOrientedProgramming
Lua really is simple, but it not always easy.
It just happen that for most projects any of.them are "right" except the most esoteric ones...
You can fully pull some crazy "clever" stuff, but requires effort, usually the first idea in your head is already good enough.
list = ["1", "2", "3"]
sum1 = sum(int(num) for num in list)
sum2 = reduce(map(list, int), operator.add)
sum3 = 0
for num in list:
sum3 += int(num)
Lua only has the third of these three options. I don't really consider this a particularly compelling advantage as a user.At least, I know I sure spend a lot more time reading code than I do writing code; so I appreciate any language that tries to optimize for that use-case and helps me minimize my cognitive overhead.
function curry(f,x)
return function(...)
return f(x, ...)
end
end
function reduce(f,t,r)
r = r or t[1]
for i=2,#t do
r = f(r,t[i])
end
return r
end
addf = function(a,b) return a+b end
sum = curry(reduce, addf)
t = {"1", "2", "12"}
print(reduce(addf, t)) -- 15
print(sum(t)) -- 15
res = 0
for i=1,#t do
res = res + t[i]
end
print(res) -- 15
res = 0
for k,v in ipairs(t) do
res = res + v
end
print(res) -- 15