C
C#2mo ago
naber top

LINQ Help

bool IsSmooth(string input)
{
var inputWords = input.Split();

bool allCharsMatch = true;
for (int i = 1; i < inputWords.Length; i++)
{
if (inputWords[i][0] != inputWords[i - 1].Last())
{
allCharsMatch = false;
break;
}
}
return allCharsMatch;
}
bool IsSmooth(string input)
{
var inputWords = input.Split();

bool allCharsMatch = true;
for (int i = 1; i < inputWords.Length; i++)
{
if (inputWords[i][0] != inputWords[i - 1].Last())
{
allCharsMatch = false;
break;
}
}
return allCharsMatch;
}
How can I get the same functionality with LINQ, I tried looking it up but its usage of lambda functions confused me. Should I stick with this type of loops instead of LINQ and where can I learn LINQ, Microsoft's LINQ documentation is too complex for me. If I need to spend more time before I get into LINQ what should I accomplish / know?
12 Replies
reflectronic
reflectronic2mo ago
this is pretty difficult to do with LINQ i would not use LINQ for this
naber top
naber top2mo ago
Alright, what do you think about rest of the question? The main part I'm stuck is at learning these concepts.
Angius
Angius2mo ago
You mean LINQ and lambdas?
naber top
naber top2mo ago
Yes both.
Angius
Angius2mo ago
The latter is very simple once you understand they're just functions
things.Where(t => t.Count > 10);
things.Where(t => t.Count > 10);
is equivalent to
things.Where(CountMoreThanTen);

public static bool CountMoreThanTen(Thing t)
{
return t.Count > 10;
}
things.Where(CountMoreThanTen);

public static bool CountMoreThanTen(Thing t)
{
return t.Count > 10;
}
reflectronic
reflectronic2mo ago
though, i guess one way to do it is
var words = input.Split();

bool allCharsMatch = words
.Zip(words.Skip(1))
.All(pair => pair.First[^1] == pair.Second[0]);
var words = input.Split();

bool allCharsMatch = words
.Zip(words.Skip(1))
.All(pair => pair.First[^1] == pair.Second[0]);
or you can use a name... i don't know which i prefer
bool allCharsMatch = words
.Zip(words.Skip(1), (previousWord, word) => (previousWord, word))
.All(pair => pair.previousWord[^1] == pair.word[0]);
bool allCharsMatch = words
.Zip(words.Skip(1), (previousWord, word) => (previousWord, word))
.All(pair => pair.previousWord[^1] == pair.word[0]);
naber top
naber top2mo ago
yeah the .Zip and .All methods I don't know what are they used for also I am not familiar with lambdas, I will work on it.
Angius
Angius2mo ago
.All() returns true if the predicate (the lambda passed into it) returns true for all elements
[1, 2, 3, 4, 5].All(number => number % 2 == 0);
[1, 2, 3, 4, 5].All(number => number % 2 == 0);
1 % 2 == 0 => false
2 % 2 == 0 => true
3 % 2 == 0 => false
4 % 2 == 0 => true
5 % 2 == 0 => false
1 % 2 == 0 => false
2 % 2 == 0 => true
3 % 2 == 0 => false
4 % 2 == 0 => true
5 % 2 == 0 => false
not all elements result in a true from the lambda, so the entire .All() will return false
Servator
Servator2mo ago
Best explanation is with foreach i think atleast thats how i learned
MODiX
MODiX2mo ago
Servator
REPL Result: Success
int[] array = { 1, 2, 3, 4, 5 };

foreach (var element in array)
{
Console.WriteLine(element + 1);
}

Console.WriteLine("####################");

var collection = array.Select(element => element + 1);

Console.WriteLine(string.Join(Environment.NewLine, collection));
int[] array = { 1, 2, 3, 4, 5 };

foreach (var element in array)
{
Console.WriteLine(element + 1);
}

Console.WriteLine("####################");

var collection = array.Select(element => element + 1);

Console.WriteLine(string.Join(Environment.NewLine, collection));
Console Output
2
3
4
5
6
####################
2
3
4
5
6
2
3
4
5
6
####################
2
3
4
5
6
Compile: 506.149ms | Execution: 88.701ms | React with ❌ to remove this embed.
Servator
Servator2mo ago
int[] array = { 1, 2, 3, 4, 5 };

foreach (var element in array)
{
Console.WriteLine(element + 1);
}

Console.WriteLine("####################");

var collection = array.Select(element => element + 1);

Console.WriteLine(string.Join(Environment.NewLine, collection));
int[] array = { 1, 2, 3, 4, 5 };

foreach (var element in array)
{
Console.WriteLine(element + 1);
}

Console.WriteLine("####################");

var collection = array.Select(element => element + 1);

Console.WriteLine(string.Join(Environment.NewLine, collection));
olayk
olayk2mo ago
I am not the most advanced at C#, but if you want to get better about solving this type of problem I would recommend looking up folds / reduces and maps. A map you go over all the values in a collection and perform some function on them. A fold you have an accumulator and a function that takes your accumulator and each value and returns a new value In this case, All is a type of fold that takes a predicate and returns the AND of all the predicates applied to the values In LINQ, they call a fold the Aggregate function Where is a filter And it looks like Select is a type of map hope this helps