Subscribe to The Madman Scribblings        RSS Feed

Bottles Of Beer (VB11 Stylee)

Icon 2 Comments
Bottles Of Beer (VB11 Stylee)

Just been messing around with VB11 and did an implements of the Bottles Of Beer Song

Imports System.Runtime.CompilerServices
Module BottleSong
  Sub Main()
<%= Iterator Function()
For i=99 to 0 Step -1:Yield i:Next 
End Function().Select(Function(Bottles)<Verse>
<%= Bottles.OfBeer %> on the wall, <%= Bottles.OfBeer %>.
<%= If(Bottles > 0, "Take one down and pass it around", "Go to the store and buy some more") %>.
<%= (Bottles - 1).OfBeer %> on the wall,<%= (Bottles - 1).OfBeer %>.

 End Sub
<Extension()>Public Function OfBeer(Bottles As Integer) As XNode
    Bottles = If(Bottles < 0, 99, Bottles)
    Return <Bottles><%= Bottles %> bottle<%= If(Bottles <> 1, "s", "") %> of Beer</Bottles>
End Function
End Module

It utilises the following features of the language.
  • XML Literals
  • Extension Method
  • LINQ
  • Iterator Lambda (VB11)

I had to configure the IDE feature of pretty printing, to make the text align properly.
This is because the auto-indentation of the XML introduced unwanted significant white-space.

2 Comments On This Entry

Page 1 of 1

Amrykid Icon

27 May 2012 - 01:40 PM
The code on this page looks misaligned. Nice post though.

AdamSpeight2008 Icon

30 May 2012 - 02:06 AM
It looks misaligned because (as I stated the post), I had to disable the pretty printing to eliminate its effects. Since XML has the notation of significant and insignificant whitespace. So I had to align it to the left margin.
Page 1 of 1