Subscribe to The Madman Scribblings        RSS Feed

FizzBuzz in Single Line Of Code

Icon 16 Comments
FizzBuzz in Single Line Of Code

Enumerable.Range(1, 100).
   Select(Function(Number) New Tuple(Of Integer, String)(Number, If(Number Mod 3 = 0, "Fizz", ""))).
   Select(Function(Tuple) New Tuple(Of Integer, String)(Tuple.Item1, If(Tuple.Item1 Mod 5 = 0, Tuple.Item2 & "Buzz", Tuple.Item2))).
   Select(Function(Tuple) If(Tuple.Item2 <> "", Tuple.Item2, Tuple.Item1.ToString)).
   ForEach(AddressOf Console.WriteLine)

16 Comments On This Entry

Page 1 of 1


16 November 2012 - 08:09 AM
That looks like six lines of code from where I'm sitting. :)


16 November 2012 - 08:35 AM
I thought I was just crazy. It's definitely 6 lines.


16 November 2012 - 08:42 AM
I would retitle this. "Fizzbuzz in a single expression"


16 November 2012 - 08:47 AM
It actually is one single line of code in VB.NET!


16 November 2012 - 08:51 AM
A line is 80 columns. :)


16 November 2012 - 09:01 AM
Implicit Line Continuation: http://msdn.microsof...o/ee681551.aspx


16 November 2012 - 09:49 AM
lucky3:- It is a single line of code (spread over 6 editor lines.) and also right about the Implicit Line Continuations which has has since VS2010. There are still places where the explicit line continuation are still required to ensure correct understanding by the compiler. and vb6 used to be line orientated but that distinction is a little blurry now. In C# it would be akin to allow the insertion of ; anywhere. It wasn't an easily feature to add to especially with the XML Literals (which follow different lexical rules, then there the code that is embedded in the xml).

What is deemed an Expression is complex and subtle, for example a Multi-Line Lambda are still a single Lambda Expression.


16 November 2012 - 09:56 AM
In most C style languages a line of code is terminated by the ; symbol.

Var s = @"Line 1
Line 2
Line 3"; /* Is a singe line of code with verbatim string literal  */



16 November 2012 - 10:19 AM
The ForEach( is a Subroutine member method (aka a Void returning Function) of the class List(Of T) so it has to be the end of the expression / LOC. All of the rest are Functions which permits us to use method call chaining, based off the output / result of the previous function.


16 November 2012 - 10:34 AM
This is one of those weird FP tics, that if you can cram a bunch of operations in to a line, it's somehow easier to understand. It's nothing new, though. Here's how it would look in scheme:

(define fizzbuzz (lambda (x) (if (= 0 (modulo x 15)) "Fizzbuzz" (if (= 0 (modulo x 5)) "Buzz" (if (= 0 (modulo x 3)) "Fizz" " ")))))

However, it seems more sensible to format it so as to reflect the actual process:

(define fizzbuzz (lambda (x) 
(if (= 0 (modulo x 15)) "Fizzbuzz" 
  (if (= 0 (modulo x 5)) "Buzz" 
    (if (= 0 (modulo x 3)) "Fizz" " ")


16 November 2012 - 11:16 AM
jon.kiparsky It is not correct to say it doing it same way. Mine produce a stream of number (1 to 100) from that produce a new stream of the "Fizz". From that produces another stream it the "Buzz" parts. From that produce the final stream of output. This final stream is then outputted to the Console window. Simples.

Plus you FizzBuzz wrong. Ha! Ha!
Your is effectively doing
If x % 15 = 0 Then
  If x % 5 = 0 Then
    If x % 3 = 0 Then
      Rem " " is Wrong '
      x  ' Should out the number '
    End If
  End if
End If



16 November 2012 - 11:34 AM
Wrong? Oh, okay, you want the number as return. Whatever. The point is, it's a pretty trivial program, and making it more complicated than it needs to be doesn't seem like much of a gain.


16 November 2012 - 02:16 PM
jon.kiparsky:- If your "trivial program" produces the incorrect output, it doesn't matter how simple the code it still a failure. I did the "FizzBuzz" program mainly as an exercise and to try out different approaches or methods of thinking about the problem. From what I see a large percentage seem take the approach of a basic for loop and a couple of If ... Then .


... making it more complicated than it needs to be doesn't seem like much of a gain.

I exercised my mind. A version that starts with a string.

I could easily exploit parallelism in mine.
FizzBuzz (Parallel Version)

Here is a head-scratcher for you. Devised a version without a single If Statement.

FizzBuzz (No If Version)



16 November 2012 - 03:04 PM
map fb [1..100] where fb x
    | x % 15 == 0 = "FizzBuzz"
    | x % 5 == 0  = "Fizz"
    | x % 3 == 0  = "Buzz"
    | otherwise   = show x

or could also use list comprehension I suppose.

I think you need to define what a line is and what an expression is. I call a line an editor line and only well formatted short lines(e.g. 80 characters) really count. a semi-colon in C syntax langues is just an ending of different kinds of statements and has nothing to do with lines other than the fact that it is generally placed at the end of lines for good formatting.


16 November 2012 - 03:32 PM
ishkabible:- Thank you for coming we'll be in touch. :no:


30 November 2012 - 03:45 PM

jon.kiparsky, on 16 November 2012 - 09:51 AM, said:

A line is 80 columns. :)/>

You... You... Fortranner you!
Page 1 of 1

Search My Blog

Recent Entries

Recent Comments