C
C#8mo ago
koray bey

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
reflectronic8mo ago
this is pretty difficult to do with LINQ i would not use LINQ for this
koray bey
koray beyOP8mo ago
Alright, what do you think about rest of the question? The main part I'm stuck is at learning these concepts.
Angius
Angius8mo ago
You mean LINQ and lambdas?
koray bey
koray beyOP8mo ago
Yes both.
Angius
Angius8mo 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
reflectronic8mo 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]);
koray bey
koray beyOP8mo 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
Angius8mo 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
Servator8mo ago
Best explanation is with foreach i think atleast thats how i learned
MODiX
MODiX8mo 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
Servator8mo 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
olayk8mo 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
Want results from more Discord servers?
Add your server