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
- Dim lines = "It was the best of times, it was the worst of times, it was the age of wisdom,
- it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light,
- it was the season of Darkness, it was the spring of hope, it was the winter of despair …, we had nothing before us,
- we were all going direct to Heaven, we were all going direct the other way …".Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
- Dim words = From w In lines.GetWords
- Where w.ToLower.StartsWith("t")
- words = doubles.Select(Function(d) d.Caption).GetWords
- Dim range = Enumerable.Range(0, Integer.MaxValue \ 2)
- Dim EvenNums = range.IsMod(2)
- Dim even10s = EvenNums.IsMod(10)
- even10s.Take(20).Dump("Even Tens")
- 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
- <Extension>
- Function GetWords(strings As IEnumerable(Of String)) As IEnumerable(Of String)
- Return strings.SelectMany(Function(s) If(s, "").Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries))
- End Function
- 'Could be done with where but I want to show how linq queries work
- <Extension()>
- Iterator Function IsMod(numbers As IEnumerable(Of Integer), Modulus As Integer) As IEnumerable(Of Integer)
- For Each num In numbers
- If num Mod Modulus = 0 Then Yield num
- Next
- End Function
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement