Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.25 KB | None | 0 0
  1. Imports System.Runtime.CompilerServices
  2.  
  3. Module Module1
  4.  
  5.     Sub Main()
  6.         Dim rand As New Random
  7.         Dim numbers = Enumerable.Range(0, 5000).Select(Function(n) rand.Next(Integer.MinValue, Integer.MaxValue)).Concat({Integer.MinValue, Integer.MaxValue}).
  8.             Select(Function(r)
  9.                        Dim bb() As Byte = BitConverter.GetBytes(r)
  10.                        Return BitConverter.ToUInt32(bb, 0)
  11.                    End Function) '.ToArray
  12.         'numbers  is just an ienumerable.  it hasn't been iterated and has not generated values
  13.  
  14.         Dim numbersList = numbers.ToList
  15.         Dim numbersArray = numbers.ToArray
  16.         Dim evens = numbers.Where(Function(n) n Mod 2 = 0)
  17.         'You will notice if you but a breakpoint here and examine these variables that you got different values in the ienumerables
  18.         'Uncomment the .toarray in the numbers declaration
  19.         'Try again.  Understand what is happening?
  20.         'This is functional programming.  I defined something that will generate 5000 values and each value is an integer converted to a UInteger(UInt32)
  21.         'CUInt(rand.Next(Integer.MinValue, Integer.MaxValue))
  22.         'So .ToList,.ToArray, for each ...will iterate over the ienumerable and generate the right type if it is a to.. method.
  23.         Dim doubles = {
  24.             New With {.Values = numbers.Select(Function(n) CDbl(n)), .SourceType = numbers.GetType, .Name = NameOf(numbers)},
  25.             New With {.Values = numbersArray.Select(Function(n) CDbl(n)), .SourceType = numbersArray.GetType, .Name = NameOf(numbersArray)},
  26.             New With {.Values = evens.Select(Function(n) CDbl(n)), .SourceType = evens.GetType, .Name = NameOf(evens)}
  27.         }.Select(Function(d) New With {.Values = d.Values, .Caption = $"{d.Name}- {d.SourceType.FullName}{Environment.NewLine}{vbTab}Value type- {d.Values.GetType.FullName}"})
  28.  
  29.         For Each dbl In doubles
  30.             dbl.Values.Take(10).Dump(dbl.Caption)
  31.         Next
  32.  
  33.         Console.ReadKey()
  34.     End Sub
  35.  
  36.     <Extension>
  37.     Sub Dump(Of T)(items As IEnumerable(Of T), caption As String)
  38.         Console.WriteLine(caption)
  39.         For Each item In items
  40.             Console.WriteLine(item)
  41.         Next
  42.         Console.WriteLine()
  43.     End Sub
  44.  
  45.  
  46. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement