Lua, seial number replacement - lua

Part of my code is like this:
Load_name:addLoad({'incrementalnodalload', 7, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
the last part (I mean 1,2,...,10) can be extended as much as required (for example 1,2,...,1000).
Thus I want to replace this part with something like this:
Load_name:addLoad({'incrementalnodalload', 7, 1, inc_number})
inc_number = 1:1000
However, it does not work!
Any suggestion is highly appreciated!

Here is inc_number function that accepts two parameters and does what you need in this context:
function inc_number(f,t)
if f > t then return else return f,inc_number(f+1,t) end
end
Load_name:addLoad({'incrementalnodalload', 7, 1, inc_number(1,100)})
Note that it only works when the result of inc_number call is the last parameter in the list of parameters. Example:
print(table.concat({inc_number(1,10)}, ","))
-- prints: 1,2,3,4,5,6,7,8,9,10

Related

Lua - Find value between 1 - 4 , but only return others

I’m not quite sure how to explain this, but I’m trying to find a way to return all the values between 1 to 4 , that are not the value provided.
For example, let say I provide value ‘2’, I want the process to return 1,3,4 for me to process individually.
To give you a more specific explanation, I’m putting together a script to retrieve the input in use against each output of a 4x4 HDMI matrix. I can retrieve the input in use e.g 2, and enable that button on a UI, but I can’t work out to get the other 3 values, to request that those buttons are turned off on the UI too.
Discover what’s on..(value returned 2)
luup.variable_set("urn:upnp-net:serviceId:Matrix1", "input2", ‘true’,
Turn off the others..
luup.variable_set("urn:upnp-net:serviceId:Matrix1", "input1", ‘false’,
luup.variable_set("urn:upnp-net:serviceId:Matrix1", "input3", ‘false’,
luup.variable_set("urn:upnp-net:serviceId:Matrix1", "input4", ‘false’,
Hope that helps someone to help me ?
function foo(n)
tbl = {1, 2, 3, 4}
table.remove(tbl, n)
return tbl
end
or to also call those functions
function foo(n)
tbl = {1, 2, 3, 4}
luup.variable_set("urn:upnp-net:serviceId:Matrix1", "input"..table.remove(tbl, n), ‘true’)
for num, _ in ipairs(tbl) do
luup.variable_set("urn:upnp-net:serviceId:Matrix1", "input"..num, ‘false’)
end
end

Why is tuple formatting limited to 12 items in Rust?

I just started a tutorial in Rust and I can't get my head around the limitation of tuple printing:
fn main() {
// Tuple definition
let short = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
let long = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
println!("{:?}", short); // Works fine
println!("{:?}", long); // ({integer}...{integer})` cannot be formatted using `:?` because it doesn't implement `std::fmt::Debug`
}
In my ignorant view the printing could be easily achieved by iterating over the entire tuple — this would allow displaying without size constraint. If the solution would be that simple it would be implemented, what am I missing here?
Printing tuples is currently implemented using a macro that only works up to 12 elements.
Functionality to statically iterate/manipulate tuples has been proposed, but has been postponed (see e.g. this RFC). There was some concerns about the implementation of these (e.g. you'd expect to be able to get the head & tail of a tuple, but there is actually no guarantee that a tuple will be stored in the same order as you specified, because the compiler is allowed to optimize for space, which means getting the tail wouldn't be a trivial operation).
As for why you need special support for that, consider the following tuple:
let mixed = (42, true, 3.14, "foo");
How you would iterate this tuple, given that all its elements have a different type? This can't simply be done using regular iterators and a for loop. You would need some new type-level syntax, which Rust is currently lacking.
Debug is only implemented on tuples up to 12 elements. This is why printing short works, but long fails.

Converting indexed table to keyed table in Lua

I'm a newbie in Lua.
I wonder how to convert indexed table to a key-based table.
For example, Let's say I have the following table.
t = {5, 6, 7, 8}
Now, I understand t[1] is 5, t[2] is 6, t[3] is 7, and t[4] is 8.
What should I do to convert the table t to the following key-based style? (without re-constructing the table again)
t = {x=5, y=6, z=7, w=8}
What would be the most simplest and performant solution to do this?
Try this code:
t = {5, 6, 7, 8}
f = {"x", "y", "z", "w"}
for k=1,#t do
t[f[k]]=t[k]
t[k]=nil
end
for k,v in pairs(t) do
print(k,v)
end

How does it work the increase of Array Capacity in Swift?

I read in Apple Docs:
"When you add elements to an array and that array begins to exceed its reserved capacity, the array allocates a larger region of memory and copies its elements into the new storage. The new storage is a multiple of the old storage’s size."
So, I opened the Playground and created some examples. The first example seems correct:
var array = [1, 2, 3, 4, 5]
array.capacity //5
array.append(contentsOf: [6, 7, 8, 9, 10])
array.capacity //10
array.append(11)
array.capacity //20
But I didn't understand the second example:
var array = [1, 2, 3, 4, 5]
array.capacity //5
array.append(contentsOf: [6, 7, 8, 9, 10, 11])
array.capacity //12
array.append(12)
array.capacity //12
Why is the capacity 12 in the second example? I didn't understand even reading the documentation and searching in Google.
I recommend you to check Rob and Matt's answers here
Even though there's a reserveCapacity function, it's not advised
from the docs:
The Array type’s append(:) and append(contentsOf:) methods take care of this detail for you, but reserveCapacity(:) allocates only as much space as you tell it to (padded to a round value), and no more. This avoids over-allocation, but can result in insertion not having amortized constant-time performance.

RSpec expect to receive method with array but order does not matter

Lets say I have method #sum which takes an array and calculates sum of all elements. I'm stubbing it:
before do
expect(calculation_service).to receive(:sum?).with([1, 2, 3]) { 6 }
end
unfortunately my test suit passes array in random order. Because of that error is raised:
Failure/Error: subject { do_crazy_stuff! }
#<InstanceDouble() (CalculationService)> received :sum? with unexpected arguments
expected: ([1, 2, 3])
got: ([3, 2, 1])
Is it possible to stub method call ignoring order of an array elements? array_including(1, 2, 3) does not ensure about array size, so it probably is not the best solution here
You can pass any RSpec matcher to with, and contain_exactly(1, 2, 3) does exactly what you want, so you can pass that to with:
expect(calculation_service).to receive(:sum?).with(contain_exactly(1, 2, 3)) { 6 }
However, "with contain exactly 1, 2, 3" doesn't read very well (and the failure message will be similarly grammatically awkward), so RSpec 3 provides aliases that solve both problems. In this case, you can use a_collection_containing_exactly:
expect(calculation_service).to receive(:sum?).with(
a_collection_containing_exactly(1, 2, 3)
) { 6 }
You can also use the method match_array. This way, you don't need to first split the elements of the array; instead, you can just use the whole array to match.
So you can use:
expect(calculation_service).to receive(:sum?).with(match_array([1, 2, 3])) { 6 }

Resources