Language C#
(C# Gratuitously Functional)
Date: | 04/14/09 |
Author: | Paul Stancer |
URL: | https://www.ohloh.net/p/ximura |
Comments: | 1 |
Info: | n/a |
Score: | (2.96 in 48 votes) |
using System; using System.Collections.Generic; using System.Linq; namespace Bottles99 { /// <summary> /// A gratuitously functional implementation of the 99 bottle of beer on the wall program. /// </summary> static class Program { static void ForEach<T>(this IEnumerable<T> items, Action<T> action) { foreach (var item in items) action(item); } static void Main(string[] args) { Console.WriteLine("99 bottles of beer on the wall, 99 bottles of beer."); Func<int, bool, string> bottle = (v, upper) => { return string.Format("{0} bottle{1}", (v == 0) ? ((upper ? "N" : "n") + "o more"):v.ToString() , ((v == 1) ? "" : "s")); }; Enumerable.Range(0, 99).Reverse() .ForEach(i => { Console.WriteLine(string.Format( "Take one down and pass it around, {0} of beer on the wall.\r\n", bottle(i, false))); Console.WriteLine(string.Format( "{0} of beer on the wall, {1} of beer.", bottle(i, true), bottle(i, false))); }); Console.WriteLine("Go to the store and buy some more, 99 bottles of beer on the wall."); Console.ReadLine(); } } }
Download Source | Write Comment
Alternative Versions
Version | Author | Date | Comments | Rate |
---|---|---|---|---|
Concise, using C# 3.5 features | Jeff Dietrich | 10/26/07 | 14 | |
2 | Paul M. Parks | 04/20/05 | 9 | |
corporate style | veteran corporate coder | 08/16/09 | 0 | |
4 | Mark Hurley | 05/31/05 | 0 | |
Uses Linq | Sudipta | 11/19/10 | 0 | |
Shows new features of C# 2.0. | Bradley Tetzlaff | 11/24/05 | 0 | |
v3.0 Easy Functional Recursive | Yelinna Pulliti Carrasco | 08/04/09 | 0 |
Download Source | Write Comment
Add Comment
Please provide a value for the fields Name,
Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.
Please don't post large portions of code here! Use the form to submit new examples or updates instead!
Comments
Tom B said on 06/02/09 04:20:24
Here's the best I could come up with. It uses C# 1.0 features only.
I think this best illustrates the structure of the song without any of my own personality leaking through.
class Beer {
static void Main()
{
const string X0 = "{0}";
const string X213 = "{2}{1}{3}";
const string X4c = "{4}, ";
const string X4p0 = "{4}.\r\n";
const string X4p = X4p0 + "\r\n";
const string X5 = ".\r\n{5}, ";
const string x6 = "n{6}";
const string X6 = "N{6}";
const string F = X0 + X213 + X4c + X0 + X213 + X5;
const string F0 = X0 + X213 + X4p + F;
const string FN = x6 + X213 + X4p + X6 + X213 + X4c + x6 + X213 + X5 + "99" + X213 + X4p0;
for(int i = 99; 0 <= i; i--)
{
System.Console.Write(
(i == 99) ? F : (i == 0) ? FN : F0,
i,
(i == 1) ? null : "s",
" bottle",
" of beer",
" on the wall",
(i == 0) ? "Go to the store and buy some more" : "Take one down and pass it around",
"o more"
}
}}