Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Runtime.CompilerServices
- Module Module1
- Sub Main()
- Dim rand As New Random
- Dim numbers = Enumerable.Range(0, 5000).Select(Function(n) rand.Next(Integer.MinValue, Integer.MaxValue)).Concat({Integer.MinValue, Integer.MaxValue}).
- Select(Function(r)
- Dim bb() As Byte = BitConverter.GetBytes(r)
- Return BitConverter.ToUInt32(bb, 0)
- End Function) '.ToArray
- 'numbers is just an ienumerable. it hasn't been iterated and has not generated values
- Dim numbersList = numbers.ToList
- Dim numbersArray = numbers.ToArray
- Dim evens = numbers.Where(Function(n) n Mod 2 = 0)
- 'You will notice if you but a breakpoint here and examine these variables that you got different values in the ienumerables
- 'Uncomment the .toarray in the numbers declaration
- 'Try again. Understand what is happening?
- 'This is functional programming. I defined something that will generate 5000 values and each value is an integer converted to a UInteger(UInt32)
- 'CUInt(rand.Next(Integer.MinValue, Integer.MaxValue))
- 'So .ToList,.ToArray, for each ...will iterate over the ienumerable and generate the right type if it is a to.. method.
- Dim doubles = {
- New With {.Values = numbers.Select(Function(n) CDbl(n)), .SourceType = numbers.GetType, .Name = NameOf(numbers)},
- New With {.Values = numbersArray.Select(Function(n) CDbl(n)), .SourceType = numbersArray.GetType, .Name = NameOf(numbersArray)},
- New With {.Values = evens.Select(Function(n) CDbl(n)), .SourceType = evens.GetType, .Name = NameOf(evens)}
- }.Select(Function(d) New With {.Values = d.Values, .Caption = $"{d.Name}- {d.SourceType.FullName}{Environment.NewLine}{vbTab}Value type- {d.Values.GetType.FullName}"})
- For Each dbl In doubles
- dbl.Values.Take(10).Dump(dbl.Caption)
- Next
- Console.ReadKey()
- End Sub
- <Extension>
- Sub Dump(Of T)(items As IEnumerable(Of T), caption As String)
- Console.WriteLine(caption)
- For Each item In items
- Console.WriteLine(item)
- Next
- Console.WriteLine()
- End Sub
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement