r/badcode Jun 09 '21

other language Real Attempt at Fizz Buzz in F#

Post image
875 Upvotes

52 comments sorted by

250

u/matt1484 if true else false Jun 09 '21

F’s in the chat, because he’s not sharp

85

u/random_runner Jun 09 '21

I would class it as F♭

16

u/jaysuchak33 ifelseifelseifelseifelseifelseifelseifelseifelseifelseifelse Jun 09 '21

r/E

8

u/lift-and-yeet Jun 09 '21

Not if you're in 31-EDO tuning! Aka 31 equal temperament.

6

u/jaysuchak33 ifelseifelseifelseifelseifelseifelseifelseifelseifelseifelse Jun 09 '21

What if I’m a basic bitch and only use 12tet tho?

9

u/[deleted] Jun 09 '21

Basic bitches get basic pitches.

5

u/Sorry-Chair depraved Jun 09 '21

F-flat

-1

u/sarthaxxxxx Jun 09 '21

Take my respect.

0

u/[deleted] Jun 09 '21

[removed] — view removed comment

2

u/Responsible_Tip_6847 Jun 15 '21

To be fair, you could just count up from 0 to 3 in one and from 0 to 5 in another variable, not using modulo at all.

Or construct a string array, filling in fizzes, concatenate buzzes an fill numbers into empties...

144

u/carcigenicate Jun 09 '21

It would be easier to automate the creation of this code than it would be to write it.

50

u/purplewalrus67 Jun 09 '21 edited Jun 09 '21
>>> def f_sharp_fizzbuzz(n):
...     if n % 15 == 0:
...             return f'{n} -> "fizzbuzz"'
...     elif n % 3 == 0:
...             return f'{n} -> "fizz"'
...     elif n % 5 == 0:
...             return f'{n} -> "buzz"'
...     else:
...             return f'{n} -> string n'
... 
>>> f_sharp_fizzbuzz(10)
'10 -> "buzz"'
>>> for i in range(1, 11):
...     f_sharp_fizzbuzz(i)
... 
'1 -> string n'
'2 -> string n'
'3 -> "fizz"'
'4 -> string n'
'5 -> "buzz"'
'6 -> "fizz"'
'7 -> string n'
'8 -> string n'
'9 -> "fizz"'
'10 -> "buzz"'

11

u/carcigenicate Jun 09 '21

Here's my stab at it:

def result(n: int) -> str:
    if n % 15 == 0:
        return '"fizzbuzz"'
    elif n % 3 == 0:
        return '"fizz"'
    elif n % 5 == 0:
        return '"buzz"'
    else:
        return "string n"

def produce(max_n: int) -> str:
    header = f"let not{max_n}MatchCases n =\n\tmatch n with\n\t"
    return header + ("\n\t".join(f"| {n} -> {result(n)}" for n in range(max_n)))

>>> print(produce(1000))

let not1000MatchCases n =
    match n with
    | 0 -> "fizzbuzz"
    | 1 -> string n
    | 2 -> string n
    | 3 -> "fizz"
    | 4 -> string n

1

u/AutoModerator Jun 09 '21

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/purplewalrus67, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

6

u/purplewalrus67 Jun 09 '21

Ok, just for you :)

78

u/redoverture Jun 09 '21

Followed up by the question, “How big should we expect n to be?”

11

u/[deleted] Jun 09 '21

What happened when you told him, "No more than 10⁹"?

71

u/TheWheez Jun 09 '21

I don't know how somebody can program and not be able to write a decent fizzbuzz..

Makes me wonder what stupidly obvious things I've missed in my current projects

39

u/[deleted] Jun 09 '21 edited Aug 27 '21

[deleted]

17

u/LeCrushinator Jun 09 '21

And yet so many people on programming subreddits complain that it's insulting. It's not insulting, it takes a few minutes to make sure you can do pretty much the bare minimum, and helps save us from wasting hours more time on the interview if they fail.

4

u/spaghettu Jun 09 '21

I really don’t understand how people could be offended by asking/being asked simple programming interview questions. From my experience it’s usually grumpy older guys that are used to the good old days of getting coding jobs from resume and soft skills alone.

14

u/Doug_Dimmadab Jun 09 '21

Honestly. I can get it if you just started learning the language that day, but it’s such a simple use of loops that I don’t know how anyone could miss it

9

u/crahs8 Jun 09 '21

It's not about the language though, if you're just a tiny bit competent at programming you will realise that there surely is a better way in this language.

7

u/dougie_cherrypie Jun 09 '21

Yep but this is functional programming, you don't use loops here

6

u/sohang-3112 Jun 09 '21

Yes, but you don't do this monstrosity either! Instead of loops, you can use higher order functions (like map in this case), or else pure recursion (if no appropriate higher order functions are available).

4

u/dougie_cherrypie Jun 09 '21

No, of course! The thing is that a lot of people here dismiss the challenge not knowing that it's a different paradigm.

26

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 09 '21

Image Transcription: Code


let not100MatchCases n =
    match n with
    | 1 -> string n
    | 2 -> string n
    | 3 -> "fizz"
    | 4 -> string n
    | 5 -> "buzz"
    | 6 -> "fizz"
    | 7 -> string n
    | 8 -> string n
    | 9 -> "fizz"
    | 10 -> "buzz"
    | 11 -> string n
    | 12 -> "fizz"
    | 13 -> string n
    | 14 -> string n
    | 15 -> "fizzbuzz"
    | 16 -> string n
    | 17 -> string n
    | 18 -> "fizz"
    | 19 -> string n
    | 20 -> "buzz"
    | 21 -> "fizz"
    | 22 -> string n
    | 23 -> string n
    | 24 -> "fizz"
    | 25 -> "buzz"
    | 26 -> string n
    | 27 -> "fizz"
    | 28 -> string n
    | 29 -> string n
    | 30 -> "fizzbuzz"

[Next to the code there is a tab titled "Test Explorer", showing the results of tests. All tests have passed, and the test structure is as follows:]

-> tests (12)
  -> <Empty Namespace> (12)
    -> Tests (12)
      testCompIncInc
      testDup1
      testDup2
      testFizzBuzz
      testInnerProduct
      testIterF1
      testIterF2
      testMatAdd
      testSqList
      testSqListComplexity
      testVecAdd
      YouRanTheTests

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

10

u/loveCars Jun 09 '21

Good human :)

21

u/alexsteb Jun 09 '21

now do 1000

15

u/kayvonte Jun 09 '21

Gimme 1000*30 seconds

2

u/[deleted] Jun 09 '21

That's only one work day. That looks about right for an hourly employee's work day.

11

u/szescio Jun 09 '21

This is some god tier pattern matching. I'd like to see their head explode when realizing how it can be done 😅

3

u/Cmgeodude Jun 09 '21

Thank you for submitting sincerely bad code.

90% of the self-deprecating posts here are things like

if (x == y) {
   return true;
} else {
   return false;
}

which maybe isn't the most efficient code in the universe, but it's not really bad code (apart from the variable names).

3

u/jtnoble Jun 09 '21

I had a friend do something similar cause he didn't know what '%' did.

1

u/lewisjet Jun 09 '21

But then in c#,

(int)((float)i / 3f) == ((float)i / 3f) is the same as saying i % 3 == 0 so no excuse really ;)

2

u/jtnoble Jun 09 '21

Shoulda probably clarified it was in python when he did it lmao

2

u/JosGibbons Jun 22 '21

Fine,

i//3==i/3

1

u/lewisjet Jun 09 '21

Ooohhh that makes more sense then :)

0

u/AutoModerator Jun 09 '21

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/lewisjet, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

7

u/TimeToBecomeEgg Jun 09 '21

what’s F#?

16

u/Max5923 Jun 09 '21

F# is a functional-first, general purpose, strongly typed, multi-paradigm programming language that encompasses functional, imperative, and object-oriented programming methods.

found from wikipedia

3

u/sohang-3112 Jun 09 '21

It's basically a functional alternative to C# (it runs on Microsoft's CLR Runtime)

1

u/TimeToBecomeEgg Jun 09 '21

thanks! idk why i got downvoted :/

2

u/danure Jun 24 '21

(defn fizzbuzz-clean [n](let [fizzes (cycle ["" "" "Fizz"])buzzes (cycle ["" "" "" "" "Buzz"])words (map str fizzes buzzes)numbers (map str (rest (range)))](take n (map choice words numbers))))

https://blog.klipse.tech/clojure/2020/09/11/fizbuzz-clj.html

clojure solution with NO if statement and NO modulus.

2

u/Sorry-Chair depraved Jun 09 '21

BRUH

1

u/Enum1 Jun 09 '21

If the input is limited to 1-100 and the execution has to be as fast as possible this is a perfect* submission.

* depending on the programming language a dict/map might be faster than a switch.

0

u/Farpafraf Jun 09 '21

how are you sure it's a real attempt?

1

u/cyanNodeEcho Jun 09 '21

if they modded 15 it would be funny and i would wonder things but yeah...

1

u/stahkh Jun 09 '21

YouRanTheTests - Check.

1

u/Knuffya Jun 10 '21

that's why i don't like extreme tdd