counter in lambda expression streamwriter - linq

I have the follow expression:
myGrid.ForEach(Sub(Model) myStreamWriter.WriteLine(Function(x As Integer) (x + 1)))
Of course it's not working. I want to write in a text file (WriteLine) a sequential number (1, 2, 3, etc.) for each record collected in the variable varGrid type: List (of mytable)
Any idea? It's fine CSharp code too.
Regards.

You could declare the counter outside of the foreach lambda, just as you would declare it outside of the scope of a foreach loop.
var x = 1;
myGrid.ForEach(model => myStreamWriter.WriteLine(x++.ToString()));

Related

Sorting table in alphabetical order

I am absolutely new in lua and just want to modify an existing script.
There is a function that writes values in a list. I would like to sort them by name:
function display_moments()
local counter = 1
if(moments[media_name]~=nill) then
moments_list = main_layout:add_list(1,4,4,1) -- empty moments_list widget to prevent duplicate entries
for i,j in pairs(moments[media_name]) do
moments_list:add_value(i,counter)
counter = counter + 1
end
end
end
Do I have the chance to get my list sorted in any way?
From Lua table.sort (ref manual) if your list is as follows
local _list = {1,4,4,1}
print(unpack(_list)) -- 1, 4, 4, 1
table.sort(_list)
print(unpack(_list)) -- 1, 1, 4, 4
you can add following line after the loop, given that your list is an array
table.sort(moments_list)

how do I use stateless iterators and start from an index other than 1 at the same time

I love lua's for loops and it's encouragement of stateless iterators like pairs/ipairs, but I have no idea how to start from indexes other than 1 when using them.
While iterators is stateless, meaning they're not holding any state, there's still state of the loop. See manuals for the details on generic loop. You can set your own initial values your custom iterator:
local function iter(table, idx)
idx = idx + 1
local v = table[idx]
if v then
return idx, v
end
end
local function start_at(table, idx)
return iter, table, idx-1
end
local values = {33,42,77,91}
for k,v in start_at(values, 3) do
print(k,v)
end
Assuming ipairs implementation will never change, you can hack in like this:
local values = {33,42,77,91}
for k,v in ipairs(values), values, 3-1 do
print(k,v)
end
This last example will use default iterator, returned by ipairs, while dropping other values in loop state, substituting it with altered initial values. Not to be actually used in your code, but it illustrates an idea.

Why I can't dynamically set CPU to execute task in Ada?

I am trying to make an array of Integers, which is accessed inside the task.
with CPU=>CPUs(1) doesn't work, as doesn't work any expression at all.
Plain integers work fine, though.
procedure Lab1 is
n: Integer:=222;
CPUs: array (1..3) of Integer := (1, 1, 1);
pri: array (1..3) of Integer := (1, 5, 10);
task T3
with CPU=>1+1
is
pragma Priority(pri(1));
pragma Task_Name ("T3");
end T3;
task body T3 is
int1:Integer:=generate_random_number(4)+n;
I read that it should be possible.
The expression giving the processor for a task can be dynamic.
Adding use System.Multiprocessors.CPU_Range; gives me:
Lab1.ada:20:05: "System" is not visible Lab1.ada:20:05: non-visible
declaration at system.ads:37 Lab1.ada:22:11: warning: file name does
not match unit name, should be "lab1.adb" Lab1.ada:30:14: operator for
type "System.Multiprocessors.CPU_Range" is not directly visible
Lab1.ada:30:14: use clause would make operation legal
I think I figured it.
with System.Multiprocessors;
use System.Multiprocessors;
procedure Lab1 is
n: Integer:=222;
CPUs: array (1..3) of CPU_Range := (1, 1, 1);
pri: array (1..3) of Integer := (1, 5, 10);
task T3
with CPU=>CPUs(1)
is

Linq how parameters are passed in select

I am learning LINQ and I found this example.
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var numsInPlace = numbers.Select((num, index) => new { Num = num, InPlace = (num == index) });
Console.WriteLine("Number: In-place?");
foreach (var n in numsInPlace)
{
Console.WriteLine("{0}: {1}", n.Num, n.InPlace);
}
I don't understand execution of the below line:
var numsInPlace = numbers.Select((num, index) => new { Num = num, InPlace = (num == index) });
As per my understanding num and index are parameters, but I don't understand where we decide that first parameter will be a number and second parameter will be index of the number?
Is it something that is fixed for int type of arrays?
Can anybody please help me understanding this?
Thanks in advance.
The definition of method Select decides it.
There are at lest 2 definitions for Select and one says it takes Func<MyInput,int,MyReturn>. We know that all but the last generic parameter of Func are inputs, and the last one is output.
Func is a special type of object (type of delegate) that has a method called Invoke() to run it and a special syntax-sugar - shorthand () which says that you can omit the Invoke and just write ().
Func<object, int> d = x => 1
d.Invoke(null) // will always return 1
d(new Object()) // also the same
So compiler takes your function and tries to find one of Selects that can accept this kind of function. Then it compiles. And then in run-time the Select just takes first, second etc. element from collection and runs your function by passing the element (and the index if the overload of Select with the function that accepts index has been chosen).
(num, index, TResult) => new { .... } is an anonymous function of type Func delegate.
It is defined by .Net Framework. Thus the First parameter is an object, where as second parameter is index of an object in an array.
Func<T, Index, TResult> Delegate
Func translated to English is: “A method that takes an T and Index of T in an array, and returns a TResult.
There is another thing called Action delegate, where there is no return.
Please have a reference to this link:
http://simpleprogrammer.com/2010/09/24/explaining-what-action-and-func-are/
See this link.
The first argument to selector represents the element to process. The second argument to selector represents the zero-based index of that element in the source sequence. This can be useful if the elements are in a known order and you want to do something with an element at a particular index, for example. It can also be useful if you want to retrieve the index of one or more elements.

Does a function in VBscript take variable number of arguments?

For example, if I have a function in VBscript:
Function sum(a, b, c)
sum = a + b + c
End function
Now, in the main, I make two variables and pass them into the function sum as the following:
Dim a : a = 1
Dim b : b = 2
Call sum(a, b)
Will this work or not, and why? Thanks.
It will not work, VBScript doesn't support optional arguments.
I'd use a function that takes an array of numbers instead vary number of arguments to getting sum.
Function sum(nums)
Dim i, out
For i = 0 To UBound(nums)
out = out + nums(i)
Next
sum = out
End function
Call sum(Array(1, 2, 3, 4))
According to this, VBscript does not support optional arguments. You can do what they suggest and pass null values to your function.
I hope this might help.
I use dictionary object to pass variables to function so I can add new arguments without the need for refactoring existing code.
dim params
set params = CreateObject("Scripting.Dictionary")
'...when I want to call a function
params.add "variable_name", value: params.add "variable_name_2", value ': ...
call fn_function_name(params)
'...declaring a function
function fn_function_name(byRef params_in)
'here I usually make sure that variable is of correct type, or that is set
params_in("variable_name") = fn_check(params_in("variable_name"), "number") ' fn_check is a separate function
' ... function code goes here ...
' in order to user external dictionary "params" multiple times, I empty dictionary before exiting the function. This is possible because I set it as a reference (byRef) instead of value
params_in.removeAll()
end function
VBScript doesn't support optional arguments or method overloading. You can pass in null values to your function call, however.

Resources