Okay, after having bashed my head against this long enough I thought of you guys... I thought having exhausted just about every other .NET concept I would come back to predicates because Microsoft's "EndsWithSaurus " demonstration is beyond useless at explaining what this is useful for. Having previously understood delegates and got my head around those, predicates finally makes sense...althoug h I still think Microsoft's example is ridiculous. I still can't make head nor tail of what they would use such an example for...shouldn't you be able to pass a variable into the predicate for the search instead of having to hard-code the search string? Anyway...moving on...
So on to actions...Actio n<T>: Array.ForEach appears to use Action<T> which is the same conceptually as Predicate<T> except that it now tells us to do something instead of just find out if we've got a match.
I threw a 30 second example together that I thought should work...but for the life of me I can't figure out what I'm missing!
[Code=vbnet]Function IsPrime(ByVal Data As Integer) As Boolean
If IsEven(Data) Then Return False
If Data <= 3 Then Return True
For i As Integer = 3 To CInt(Math.Sqrt( Data)) Step 2
If (Data Mod i = 0) Then Return False
Next
Return True
End Function
Public Sub AddOne(ByVal Data As Integer)
Data += 1
End Sub
Private Sub Main()
'Compose some array of integers
Dim AllValues() As Integer = New Integer() {1, 3, 5, 6, 8, 9, 10, 12, 13, 15, 17}
'Grab just the prime numbers from our array
Dim MyPredicate As New Predicate(Of Integer)(Addres sOf IsPrime)
Dim FilteredValues( ) As Integer = Array.FindAll(A llValues, MyPredicate)
'Do something with each item in our filtered values list
Dim MyAction As New Action(Of Integer)(Addres sOf AddOne)
Array.ForEach(O f Integer)(Filter edValues, MyAction)
'Grab our newly modified data
Dim ModifiedValues( ) As Integer = FilteredValues
End Sub[/Code]
So stepping through the code, it would appear that my predicate is running on my array and filtering the primes for me just as I expect...but my action is not adding one to my filtered values...when I get to the end, I would expect modified values to have a list of prime numbers incremented by 1, but instead, I have the list of raw prime numbers.
I'm obviously missing something, but I can't for the life of me see what it is...
So on to actions...Actio n<T>: Array.ForEach appears to use Action<T> which is the same conceptually as Predicate<T> except that it now tells us to do something instead of just find out if we've got a match.
I threw a 30 second example together that I thought should work...but for the life of me I can't figure out what I'm missing!
[Code=vbnet]Function IsPrime(ByVal Data As Integer) As Boolean
If IsEven(Data) Then Return False
If Data <= 3 Then Return True
For i As Integer = 3 To CInt(Math.Sqrt( Data)) Step 2
If (Data Mod i = 0) Then Return False
Next
Return True
End Function
Public Sub AddOne(ByVal Data As Integer)
Data += 1
End Sub
Private Sub Main()
'Compose some array of integers
Dim AllValues() As Integer = New Integer() {1, 3, 5, 6, 8, 9, 10, 12, 13, 15, 17}
'Grab just the prime numbers from our array
Dim MyPredicate As New Predicate(Of Integer)(Addres sOf IsPrime)
Dim FilteredValues( ) As Integer = Array.FindAll(A llValues, MyPredicate)
'Do something with each item in our filtered values list
Dim MyAction As New Action(Of Integer)(Addres sOf AddOne)
Array.ForEach(O f Integer)(Filter edValues, MyAction)
'Grab our newly modified data
Dim ModifiedValues( ) As Integer = FilteredValues
End Sub[/Code]
So stepping through the code, it would appear that my predicate is running on my array and filtering the primes for me just as I expect...but my action is not adding one to my filtered values...when I get to the end, I would expect modified values to have a list of prime numbers incremented by 1, but instead, I have the list of raw prime numbers.
I'm obviously missing something, but I can't for the life of me see what it is...
Comment