YellowLion
(YellowLion)
September 9, 2020, 8:34am
1
I ones saw something like this in C# which somehow works. They had a list of values and instead of doing a for-loop they just used the + operator which I cannot do somehow.
So, do you might know how it could have been written, adding summing up one value to all other values in the list
System.Linq
Enumerable.Sum Method
I am getting crazy about it (for reference see profile picture).
List<int> aInts = new List<int>{1,2,4,4,5,9,9};
aInts += 2;
Print(aInts.ToString());
question4.gh (3.9 KB)
Try
int sum = list.Sum();
Or use the long way round:
int sum = 0;
foreach(int i in list) sum+=i;
YellowLion
(YellowLion)
September 9, 2020, 8:56am
3
What I am trying to do is
1+2=3
2+2=4
4+2=6
4+2=6
5+2=7
9+2=11
9+2=11
Returning a new list without a for-loop
List<int> aInts = new List<int>{1,2,4,4,5,9,9};
aInts += 2;
A = aInts;
rgr
September 9, 2020, 9:04am
4
1 Like
list = list.Select(c => {c +=2; return c;}).ToList();
1 Like
I have to say I don’t get it. You have to go through each element in the memory anyway, so what is the good reason for avoiding a for loop?
for (int i = 0; i < aInts.Count; i++) aInts[i]+=2;
1 Like
C#
list=(from val in list select val+2).ToList();
Python
list=[val+2 for val in list]
2 Likes
Sledgehammers and nuts spring to mind
2 Likes
(sorry, different topic but…) Why does this code even work? aInts[i] is just an int, not a reference…
1 Like
YellowLion
(YellowLion)
September 10, 2020, 6:54am
11
List<int> numberInts = new List<int>{1,1,1,2,2,3,4,4,5};
int addingIn = 2;
//A
List<int> aNewInts = numberInts.Select(v => v + addingIn).ToList();
A = aNewInts;
//B
List<int> bEmptyInts = new List<int>();
foreach(int i in numberInts){
bEmptyInts.Add(numberInts[i] + addingIn);
}
B = bEmptyInts;
//C
List<int> cEmptyInts = new List<int>();
for(int i = 0;i < numberInts.Count;i++){
cEmptyInts.Add(numberInts[i] + addingIn);
}
C = cEmptyInts;
//D
List<int> dNewInts = numberInts;
for(int i = 0;i < dNewInts.Count;i++){
dNewInts[i] = dNewInts[i] + addingIn;
}
D = dNewInts;
//E problem
//List<int> eNewInts = numberInts;
//eNewInts.ForEach(v => (v + addingIn));
//E = eNewInts;
//F
var fNewInts = numberInts.Select(v => v + addingIn);
F = fNewInts;
//G
List <int> gNewInts = numberInts.Select(v => {v += addingIn; return v;}).ToList();
G = gNewInts;
//H
List<int> hNewInts = numberInts;
for (int i = 0; i < hNewInts.Count; i++) hNewInts[i] += addingIn;
H = hNewInts;
//I
List<int> iNewInts = numberInts;
iNewInts = (from v in iNewInts select v + addingIn).ToList();
question4b.gh (5.1 KB)
Are we being trolled here?
YellowLion
(YellowLion)
September 10, 2020, 7:11am
13
Who you talking about? I still cannot comprehend the word ‘trolling.’
You tell us you would like to write the code as short as possible, we try and help you, then you post a huge chunk of code that doesn’t relate to the original question without any explanation. What’s going on?
Someone is super-sugared.
1 Like
YellowLion
(YellowLion)
September 10, 2020, 7:18am
16
It is a summary of all the ways to sum a value to all values in a list.
A summary of all the information you provided me
YellowLion
(YellowLion)
September 10, 2020, 7:20am
17
What you mean by super-sugared? Hyper-active?
I already have difficulty with understanding the word ‘trolled.’ So, please do not further confuse me (see my already confused profile picture face).
Almost all of them are repeated. You may have 3 forms although I think only the for loop is understood by the machine and the rest are syntactic sugar understood by the compiler, but not completely sure.
1 Like
The end result is the sum of all the sum options. Everyone gets a prize for participation, a free confused cat.
1 Like
YellowLion
(YellowLion)
September 10, 2020, 7:50am
20
I made it for myself but I thought I also could share it. Not that you need it because you are super-pro’s but just for if others would need it
2 Likes