Lua: How can I concatenate methods, as with the string methods? - methods

The string funtions can used with this ways:
string.FUNCTION('myString', PARAMETER)
or replace 'string' with the string to use and call it as method
('myString'):METHOD(PARAMETER)
The last way is very fine to read and allows to concatenate methods.
-- example string operation
some_str, pos = ' some string', 1
-- default string syntax
while string.find(string.sub(some_str, pos, pos), '%s') do pos = pos +1 end
-- the same with syntactic sugar
while some_str:sub(pos, pos):find('%s') do pos = pos +1 end
So I tried to get the same behaviour with my own functions. But this fails.
The only way I found, was to use an additional parameter to say: return the object itself or the result.
Here a simple example for this.
calc = {
result = 0,
operator = '',
run = function(self, a, b, r) -- return self with r='s'
if b == 's' then r, b = b, nil end
if not b then b, a = a, self.result end
if self.operator == '+' then self.result = (a) + (b)
elseif self.operator == '-' then self.result = (a) - (b)
elseif self.operator == '*' then self.result = (a) * (b)
elseif self.operator == '/' then self.result = (a) / (b) end
if r ~= nil then return self else return self.result end
end,
add = function(self, a, b, r) self.operator = '+' return self:run(a, b, r) end,
sub = function(self, a, b, r) self.operator = '-' return self:run(a, b, r) end,
mul = function(self, a, b, r) self.operator = '*' return self:run(a, b, r) end,
div = function(self, a, b, r) self.operator = '/' return self:run(a, b, r) end
}
-- single operation
result = calc:add(12, 5)
-- concatenated operations
result = calc:add(12, 5, 's'):sub(3, 's'):mul(2, 's'):div(7)
Exists any way to do it same like in string operations?
Thanks in advance.

Your subsequent calls assign 's' to b parameter, not to r. Of course check for return self fails. Rather than give different behaviour to methods with some flags make them always return self instead and make a separate method to return current result - it will be much cleaner to read and program.
After that your call will look like:
result = calc:new(12):add(5):sub(3):mul(2):div(7):result()
Also, you don't really need proxy functions that go into one big function that splits into ifs anyway - just do everything inside add/sub/mul/div themselves.
You'll probably want more than one calc object as well, with each one having its own separate current result. Store common functions in a metatable and make :new create new instances with this metatable and separate entry for result.
local calc_meta = { __index = {
add = function(self, number) self._r = self._r + number return self end,
sub = function(self, number) self._r = self._r - number return self end,
mul = function(self, number) self._r = self._r * number return self end,
div = function(self, number) self._r = self._r / number return self end,
result = function(self) return self._r end
}}
local calc = {
new = function(self, number)
return setmetatable({
_r = number or 0
}, calc_meta) end
}
result = calc:new(12):add(5):sub(3):mul(2):div(7):result()
print(result)
-- 4
You can't completely duplicate Lua's behavior with strings - it is built-in into VM to treat string table as metatable for string values and cannot be programmed without modifying VM itself. You can get rid of extra result at end though if you add __add/__sub and other numeric methods to metatable so they would automatically "unwrap" your object to basic number value. Of course you won't be able to apply your methods to "unwrapped" value after that.

Related

Given: aabcdddeabb => Expected: [(a,2),(b,1),(c,1),(d,3),(e,1),(a,1),(b,1)] in Scala

I'm really interested in how this algorithm can be implemented. If possible, it would be great to see an implementation with and without recursion. I am new to the language so I would be very grateful for help. All I could come up with was this code and it goes no further:
print(counterOccur("aabcdddeabb"))
def counterOccur(string: String) =
string.toCharArray.toList.map(char => {
if (!char.charValue().equals(char.charValue() + 1)) (char, counter)
else (char, counter + 1)
})
I realize that it's not even close to the truth, I just don't even have a clue what else could be used.
First solution with using recursion. I take Char by Char from string and check if last element in the Vector is the same as current. If elements the same I update last element by increasing count(It is first case). If last element does not the same I just add new element to the Vector(second case). When I took all Chars from the string I just return result.
def counterOccur(string: String): Vector[(Char, Int)] = {
#tailrec
def loop(str: List[Char], result: Vector[(Char, Int)]): Vector[(Char, Int)] = {
str match {
case x :: xs if result.lastOption.exists(_._1.equals(x)) =>
val count = result(result.size - 1)._2
loop(xs, result.updated(result.size - 1, (x, count + 1)))
case x :: xs =>
loop(xs, result :+ (x, 1))
case Nil => result
}
}
loop(string.toList, Vector.empty[(Char, Int)])
}
println(counterOccur("aabcdddeabb"))
Second solution that does not use recursion. It works the same, but instead of the recursion it is using foldLeft.
def counterOccur2(string: String): Vector[(Char, Int)] = {
string.foldLeft(Vector.empty[(Char, Int)])((r, v) => {
val lastElementIndex = r.size - 1
if (r.lastOption.exists(lv => lv._1.equals(v))) {
r.updated(lastElementIndex, (v, r(lastElementIndex)._2 + 1))
} else {
r :+ (v, 1)
}
})
}
println(counterOccur2("aabcdddeabb"))
You can use a very simple foldLeft to accumulate. You also don't need toCharArray and toList because strings are implicitly convertible to Seq[Char]:
"aabcdddeabb".foldLeft(collection.mutable.ListBuffer[(Char,Int)]()){ (acc, elm) =>
acc.lastOption match {
case Some((c, i)) if c == elm =>
acc.dropRightInPlace(1).addOne((elm, i+1))
case _ =>
acc.addOne((elm, 1))
}
}
Here is a solution using foldLeft and a custom State case class:
def countConsecutives[A](data: List[A]): List[(A, Int)] = {
final case class State(currentElem: A, currentCount: Int, acc: List[(A, Int)]) {
def result: List[(A, Int)] =
((currentElem -> currentCount) :: acc).reverse
def nextState(newElem: A): State =
if (newElem == currentElem)
this.copy(currentCount = this.currentCount + 1)
else
State(
currentElem = newElem,
currentCount = 1,
acc = (this.currentElem -> this.currentCount) :: this.acc
)
}
object State {
def initial(a: A): State =
State(
currentElem = a,
currentCount = 1,
acc = List.empty
)
}
data match {
case a :: tail =>
tail.foldLeft(State.initial(a)) {
case (state, newElem) =>
state.nextState(newElem)
}.result
case Nil =>
List.empty
}
}
You can see the code running here.
One possibility is to use the unfold method. This method is defined for several collection types, here I'm using it to produce an Iterator (documented here for version 2.13.8):
def spans[A](as: Seq[A]): Iterator[Seq[A]] =
Iterator.unfold(as) {
case head +: tail =>
val (span, rest) = tail.span(_ == head)
Some((head +: span, rest))
case _ =>
None
}
unfold starts from a state and applies a function that returns, either:
None if we want to signal that the collection ended
Some of a pair that contains the next item of the collection we want to produce and the "remaining" state that will be fed to the next iteration.
In this example in particular, we start from a sequence of A called as (which can be a sequence of characters) and at each iteration:
if there's at least one item
we split head and tail
we further split the tail into the longest prefix that contains items equal to the head and the rest
we return the head and the prefix we got above as the next item
we return the rest of the collection as the state for the following iteration
otherwise, we return None as there's nothing more to be done
The result is a fairly flexible function that can be used to group together spans of equal items. You can then define the function you wanted initially in terms of this:
def spanLengths[A](as: Seq[A]): Iterator[(A, Int)] =
spans(as).map(a => a.head -> a.length)
This can be probably made more generic and its performance improved, but I hope this can be an helpful example about another possible approach. While folding a collection is a recursive approach, unfolding is referred to as a corecursive one (Wikipedia article).
You can play around with this code here on Scastie.
For
str = "aabcdddeabb"
you could extract matches of the regular expression
rgx = /(.)\1*/
to obtain the array
["aa", "b", "c", "ddd", "e", "a", "bb"]
and then map each element of the array to the desired string.1
def counterOccur(str: String): List[(Char, Int)] = {
"""(.)\1*""".r
.findAllIn(str)
.map(m => (m.charAt(0), m.length)).toList
}
counterOccur("aabcdddeabb")
#=> res0: List[(Char, Int)] = List((a,2), (b,1), (c,1), (d,3), (e,1), (a,1), (b,2))
The regular expression reads, "match any character and save it to capture group 1 ((.)), then match the content of capture group 1 zero or more times (\1*).
1. Scala code kindly provided by #Thefourthbird.

Does Function return multiple value in VBScript? [duplicate]

This question already has answers here:
Return more than one value from a function
(2 answers)
Closed 5 years ago.
We are trying to return multiple values from a single function.
Sub testing()
a = 2
b = 5
temp = func(a, b)
MsgBox temp
End Sub
Function func(a, b)
'we are validating the multiple returns from the function
func = a + b
func = a * b
func = b / a
End Function
VBScript functions return just a single value. If you assign multiple values to the function name as in your example:
func = a + b
func = a * b
func = b / a
only the last value (the result of b / a) will be returned.
To have a function return multiple values you need to wrap the values in a datastructure and return that one datastructure. That could be an array
func = Array((a+b), (a*b), (b/a))
a dictionary
Set d = CreateObject("Scripting.Dictionary")
d.Add "add", (a+b)
d.Add "mul", (a*b)
d.Add "div", (b/a)
Set func = d
a custom object
Class CData
Public add, mul, div
End Class
...
Set c = New CData
c.add = a+b
c.mul = a*b
c.div = b/a
Set func = c
an ArrayList
Set a = CreateObject("System.Collections.ArrayList")
a.Add (a+b)
a.Add (a*b)
a.Add (b/a)
Set func = a
or some other collection.
Note that for returning objects you need to use the Set keyword when assigning the return value of the function:
Set temp = func(a, b)
Yes, with the use of the Array function you can return multiple values from single function.
Function func(a, b)
mul = a * b
div = a / b
add = a + b
sub = a - b
func = Array(mul, div, add, sub)
End Function
then call function like:
val = func(3,4)
print val(0) 'will give multiply of 3*4 = 12
print val(1) 'will give division
print val(2) 'will give addition
print val(3) 'will give subtraction.

Lua sorting values of tables within table

I am very new to Lua, so please be gentle.
I want a sorted results based on the "error" key. For this example, the output should be:
c 50 70
d 25 50
b 30 40
a 10 20
Here is my script:
records = {}
records["a"] = {["count"] = 10, ["error"] = 20}
records["b"] = {["count"] = 30, ["error"] = 40}
records["c"] = {["count"] = 50, ["error"] = 70}
records["d"] = {["count"] = 25, ["error"] = 50}
function spairs(t, order)
-- collect the keys
local keys = {}
for k in pairs(t) do keys[#keys+1] = k end
-- if order function given, sort by it by passing the table and keys a, b,
-- otherwise just sort the keys
if order then
table.sort(keys, function(a,b) return order(t, a, b) end)
else
table.sort(keys)
end
-- return the iterator function
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
for k, v in pairs(records) do
for m, n in pairs(v) do
for x, y in spairs(v, function(t,a,b) return t[b] < t[a] end) do
line = string.format("%s %5s %-10d", k, n, y)
end
end
print(line)
end
I found this about sorting a table and tried to implement it. But it does not work, results are not sorted.
table.sort only works when the table elements are integrally indexed. In your case; when you try to call spairs, you are actually calling table.sort on the count and error indices.
First off; remove the ugly, irrelevant nested for..pairs loops. You only need the spairs for your task.
for x, y in spairs(records, function(t, a, b) return t[b].error < t[a].error end) do
print( x, y.count, y.error)
end
And that is all.

pseudocode function to return two results for processing a tree

I'm trying to write a pseudocode for a recursive function that should process a binary tree. But the problem is that the function should return two variables. I know that functions are supposed to return on variable, and for more return values they should use list, array or vector, but I don't know how to present it as a pseudocode.
Does it look correct for a pseudocode?
function get_min(node *p)
begin
if (p==NULL) then
return list(0,0);
else
(vl,wl) = get_min(p->left)
(vr,wr) = get_min(p->right)
if (vl > vr) then
return list(vr + p->cost, 1)
else
return list(vl + p->cost, 0)
end if
end if
end function
Since it's pseudo-code, just about anything goes.
However, I'd rather go for omitting "list":
return (0, 0)
return (vr + p->cost, 1)
return (vl + p->cost, 0)
There doesn't seem to be any real benefit to putting "list" there - the (..., ...) format pretty clearly indicates returning two values already - there's no need to explicitly say you're returning them in a list.
Side note: You mention list, array or vector, but pair is another option in some languages, or wrapping the two in an object (typically giving the advantage of compile-time type checking - not really applicable in pseudo-code, obviously).
You could consider replacing "list" with "pair" instead of removing it if you wish to make it clear that the function only ever returns exactly 2 values.
If you pass parameters as reference , then there is no need to use lists as #Dukeling suggested .
void function get_min(node *p , int *cost , int * a)
begin
if (p==NULL) then
*cost =0 ; *a =0 ; return ;
else
get_min(p->left ,vl ,vw)
get_min(p->right , vr , wr)
if (vl > vr) then
*cost = vl + p->cost , *a =0 ; return;
else
*cost = vl + p->cost , *a =0 ; return ;
end if
end if
end function

Strange Ruby String Selection

The string in question (read from a file):
if (true) then
{
_this = createVehicle ["Land_hut10", [6226.8901, 986.091, 4.5776367e-005], [], 0, "CAN_COLLIDE"];
_vehicle_10 = _this;
_this setDir -2.109278;
};
Retrieved from a large list of similar (all same file) strings via the following:
get_stringR(string,"if","};")
And the function code:
def get_stringR(a,b,c)
b = a.index(b)
b ||= 0
c = a.rindex(c)
c ||= b
r = a[b,c]
return r
end
As so far, this works fine, but what I wanted to do is select the array after "createVehicle", the following (I thought) should work.
newstring = get_string(myString,"\[","\];")
Note get_string is the same as get_stringR, except it uses the first occurrence of the pattern both times, rather then the first and last occurrence.
The output should have been: ["Land_hut10", [6226.8901, 986.091, 4.5776367e-005], [], 0, "CAN_COLLIDE"];
Instead it was the below, given via 'puts':
["Land_hut10", [6226.8901, 986.091, 4.5776367e-005], [], 0, "CAN_COLLIDE"];
_vehicle_10 = _this;
_this setDir
Some 40 characters past the point it should have retrieve, which was very strange...
Second note, using both get_string and get_stringR produced the exact same result with the parameters given.
I then decided to add the following to my get_string code:
b = a.index(b)
b ||= 0
c = a.index(c)
c ||= b
if c > 40 then
c -= 40
end
r = a[b,c]
return r
And it works as expected (for every 'block' in the file, even though the strings after that array are not identical in any way), but something obviously isn't right :).
You want r = a[b..c] instead of r = a[b,c].
Difference is: b..c = start from b, go to c, while b,c = start from b and move c characters to the right.
Edit: You don't have to/shouldn't escape the [ and ] either, because you are using strings and not regexen. Also, you have to take the length of the end ("];") into consideration, or you will cut off parts of the end.
def get_stringR(a,b,c)
bp = a.index(b) || 0
cp = a.rindex(c) || bp
r = a[bp..cp + c.size - 1]
return r
end
def get_string(a,b,c)
bp = a.index(b) || 0
cp = a.index(c) || bp
r = a[bp..cp + c.size - 1]
return r
end

Resources