Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.64 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.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.         Dim lines = "It was the best of times, it was the worst of times, it was the age of wisdom,
  40. it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light,
  41. it was the season of Darkness, it was the spring of hope, it was the winter of despair …, we had nothing before us,
  42. we were all going direct to Heaven, we were all going direct the other way …".Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
  43.  
  44.         Dim words = From w In lines.GetWords
  45.                     Where w.ToLower.StartsWith("t")
  46.  
  47.  
  48.         words = doubles.Select(Function(d) d.Caption).GetWords
  49.  
  50.  
  51.  
  52.         Dim range = Enumerable.Range(0, Integer.MaxValue \ 2)
  53.         Dim EvenNums = range.IsMod(2)
  54.         Dim even10s = EvenNums.IsMod(10)
  55.         even10s.Take(20).Dump("Even Tens")
  56.  
  57.  
  58.         Console.ReadKey()
  59.     End Sub
  60.  
  61.     <Extension>
  62.     Sub Dump(Of T)(items As IEnumerable(Of T), caption As String)
  63.         Console.WriteLine(caption)
  64.         For Each item In items
  65.             Console.WriteLine(item)
  66.         Next
  67.         Console.WriteLine()
  68.     End Sub
  69.     <Extension>
  70.     Function GetWords(strings As IEnumerable(Of String)) As IEnumerable(Of String)
  71.         Return strings.SelectMany(Function(s) If(s, "").Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries))
  72.     End Function
  73.  
  74.     'Could be done with where but I want to show how linq queries work
  75.     <Extension()>
  76.     Iterator Function IsMod(numbers As IEnumerable(Of Integer), Modulus As Integer) As IEnumerable(Of Integer)
  77.         For Each num In numbers
  78.             If num Mod Modulus = 0 Then Yield num
  79.         Next
  80.     End Function
  81.  
  82. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement