[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Fizzbuzz thread. Why haven't you joined the ML master race
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 92
Thread images: 2
File: ocaml.png (58 KB, 420x420) Image search: [Google]
ocaml.png
58 KB, 420x420
Fizzbuzz thread.
Why haven't you joined the ML master race already, /g/?

let rec f n s =
if n = 101
then print_string s
else f (n+1) (s ^ "\n" ^ (
(fun x ->
if x = ""
then string_of_int n
else x)
(List.fold_left
(fun x y -> x ^
if (n mod (fst y) = 0)
then (snd y)
else "" )
""
[3,"fizz";5,"buzz"] )
))
in f 1 "";;
>>
If you insist on using meme languages, at least use proper variable names and don't obfuscate your code.
Your FizzBuzz is unacceptable.
>>
>>55602486
I replaced dolist with a while. Now it should look cleaner.
(defun fizzbuzz ()
(interactive)
(let ((fizz nil)
(buzz nil)
(counter 1))
(while (< counter 101)
(insert (format "%s%s%s\n"
(if (setq fizz (= (mod counter 3) 0))
"Fizz" "")
(if (setq buzz (= (mod counter 5) 0))
"Buzz" "")
(if (not (or fizz buzz))
(number-to-string counter) "")))
(setq counter (1+ counter)))))
>>
>>55602514
>OCaml
>Meme Language
Good try, m8.
Might as well disregard the fact that it's used by a very succesful HFT trading firm
https://www.youtube.com/watch?v=hKcOkWzj0_s
Or the fact that it's dialect has been adopted by Microsoft for F#
Or the fact that you're obviously talking out of your ass to make up for the fact that you couldn't understand functional paradigm if your life depended on it.
>>
function p(x){console.log(x)} function d(x,n){return x%n==0} for (i=1; i<100; i++) {d(i,15) ? p("fizzbuzz") : d(i,3) ? p("fizz") : d(i,5) ? p("buzz") : p(i)}
>>
Runtime-loopless fizzbuzz
import std.stdio;
import std.conv;

string fizzbuzz()
{
auto code = "auto fizzbuzz = \"";
foreach (i; 0..100)
{
auto fizz = i % 3 == 0;
auto buzz = i % 5 == 0;
if (fizz && buzz) code ~= "fizzbuzz";
else if (fizz) code ~= "fizz";
else if (buzz) code ~= "buzz";
else code ~= to!string(i);
code ~= "\n";
}
code ~= "\";";
return code;
}

void main()
{
mixin(fizzbuzz());
writeln(fizzbuzz);
}
>>
>>55602693
That works, but I was going for the more pure functional stuff and code reusability. (in the context that you have to evaluate (n mod x = 0) multiple times).
I still have some pretty limited knowledge and OCaml is a beast of a language for the functional stuff.
>>
function d3(x){return x%3==0 ? console.log("fizz") : true}
function d5(x){return x%5==0 ? console.log("buzz") : true}
for (i = 1; i < 100; i++) { !d3(i) || !d5(i) || console.log(i) }
>>
fkin n00bz

let me show ya how it's done in 2016

 let fizzbuzz = function*(){
for (var val = 1; val < 100; val++) {
if(val % 15 === 0){
yield "FizzBuzz";
}
else if(val % 3 === 0){
yield "Fizz";
}
else if(val % 5 === 0){
yield "Buzz";
}
yield val;
}
}();

for (var n of fizzbuzz) {
console.log(n);
}
>>
>>55602486
main(i){printf((char[3][9]){"%d\n","Fizz%s","%s"}[!(i%3)?1:!(i%5)*2],!(i%5)?"Buzz\n":!(i%3)?"\n":i);return i<100?main(++i):0;}

i win
>>
>>55602486
CSS MASTER RACE
<!DOCTYPE html>
<html><body>
<style>body{counter-reset: item; font-family: Helvetica; font-weight: 300}
div{counter-increment: item}
div:not(:nth-of-type(5n)):before{content: counter(item) ""}
div:nth-of-type(3n):before{content: "Fizz"}
div:nth-of-type(5n):after{content: "Buzz"}</style>
<script>document.write(Array.apply(null, Array(100)).map(() => '<div></div>').join(''))</script>
</body></html>
>>
VS100|+*"Fizz"!%N3*"Buzz"!%N5N
>>
>>55602486
Rust is the best ML language, and your fizzbuzz is so bad it's insulting.
fn fizzbuzz(n: i64) -> String {
match (n % 3, n % 5) {
0, 0 => "fizzbuzz".to_string(),
0, _ => "fizz".to_string(),
_, 0 => "buzz".to_string(),
_, _ => n.to_string(),
}
}

for n in (1..101) {
println!("{}", fizzbuzz(n));
}
>>
>>55602486
In for python.
 a = input 
For n in range (a):
If a % 3 && a % 5:
Print 'FizzBuzz'
Elif a % 3:
Print 'Fizz'
Elif a % 5:
Print 'Buzz'
>>
>>55602901
>font-family: Helvetica; font-weight: 300
Why?
It makes no difference, nobody cares about what font you use.
>>
>>55602693
>(interactive)

My CL is a little rusty, what does this do?
>>
>>55602778
>pure functional
>code re-usability
>the code can't be used anywhere except inside the whole
>printing in the inner function itself
>if-then-else
Absolutely disgusting.
>>
>>55603025
Forgot the () after input. Doing this on phone is shit.
>>
>>55603047
Pretty sure that's elisp, not cl.
>>
>>55603047
That isn't Common Lisp, it's Emacs Lisp.
(interactive)
allows the function to be called interactively, so you can do M-x fizzbuzz.
>>
>>55603029
>Serif fonts on the Web
Ew

I use OS X, appearance matters a lot to me, can't ya tell?
>>
>>55603025
0/10, doesn't even get interpreted and even if it did it'd be wrong.
>>
>>55603092
font-family: monospace;

The only right choice if you want your stuff to look good.
>>
>>55603110
Does anyone on web use monospace except for code?
>>
>>55602711
That is literally the OCaml meme
>>
>>55603094
What's wrong other than the input ()? Also, shebangs aren't necessary as running $python <program> would work just as well.
>>
>>55603156
Not sure if retarded or just pretending.
>url says 4chan.org/g/
Oh.
>>
>>55603060
>in the context that you have to evaluate (n mod x = 0
Did you even read my post?
Just how fucking stupid can you be?
>>
>>55603216
Not as stupid as you that's for sure. Hell, even the pytard is smarter than you I'd bet.
>>
>>55603154
Okay, then, guy. Sure.
>>
(define (fizzbuzz x)
(map (lambda (i)
(cond ((= (modulo i 3) 0) 'Fizz)
((= (modulo i 5) 0) 'Buzz)
((= (modulo i 15) 0) 'FizzBuzz)
(else i)))
x))
>>
>>55603256
Let me spell that out for you.
THE CODE TO REUSE IS THE FUCKING IF STATEMENT FOR
N MOD X = 0
Holy fuck.
>>
for i in range(1,100):
if i % 15 == 0:
print("fizzbuzz")
elif i % 3 == 0:
print("fizz")
elif i % 5 == 0:
print("buzz")
else:
print(i)

python masterrace.
>>
>>55603304
Have you tried having an IQ of 10 or above? They say people in that range are not as retarded as you.
>>
>>55603325
you're shit at python.
 for i in range(1,101): print "FizzBuzz"[i*i%3*4:8--i**4%5] or i 
>>
>>55603342
At least mine's readable.
>>
>>55603358
Who gives a fuck if your fizzbuzz is readable. It will never be useful.
>>
>>55603376
desu this, anybody can make a readable fizzbuzz, the obfuscated and silly ones are the ones slightly amusing
>>
>>55602486
Please don't use ^ here for string concatenation. Turns the algorithm into O(n^2)
>>
>>55603025
>using input() in python 2
>>
>>55603342
python newbie here.
can you explain the logic for the computer behind why when i is not a multiple of 3 or 5 it gets printed?
Also im assuming [i*i%3*4:8--i**4%5] determines the lenght of the string. Is it because the string is null so it prints i?

Thanks
>>
>>55603520
or is a short circuit operator that can operate on truthy and falsy values.
a string with chars is truthy. an empty string is falsy.
boolA or boolB teaches us that boolB only needs to be evaluated if boolA is false.
the same is true in this case: if "FizzBuzz" is empty (falsy), the second part of the or is evaluated.
[i*i%3*4:8--i**4%5] determines which part of the string to use (slicing: [lower:upper], where upper is an exclusive bound).
>>
ACLS
LOX=0
FOR I=1 TO 100
M=I MOD 30
IF M==0 THEN
LOCATE LOX,29
LOX=LOX+9
ELSE
LOCATE LOX,M-1
ENDIF

THREED=FALSE
IF I MOD 3==0 THEN
THREED=TRUE
? "Fizz";
ENDIF
IF I MOD 5==0 THEN
? "Buzz";
ELSEIF !THREED THEN
? I;
ENDIF
NEXT
LINPUT _$
LOCATE 38,28


Fucking around with SmileBASIC. The shit at the top is to move the cursor around so everything gets printed on the same screen and you can actually read it.
>>
>>55603618
thank you bro. Also why the double - in algorithm .
>>
>>55603023
Damn, this is actually pretty tight.
>>
>>55603859
a unary - has higher precedence than the binary - (or +) (and higher precedence than %, which is why it's applied before %).
8--i**4%5 is equivalent to (8-((-(i**4))%5))
>>
>>55603996
you've been so helpful, thanks
>>
>>55603023
>"fizzbuzz".to_string()
JUST.fuck!().my::shit().up!()
>>
>>55604169
(If[Mod[#,3]==0,"Fizz",""]~~If[Mod[#, 5]==0,"Buzz",""])/.""->#&/@Range[100]


Golf this, bitch.
>>
>>55604270
...did not mean to reply, but I suppose it's sort of appropriate.
>>
>>55604270
(JUST[fuck[#], my~~shit])/.""->@Up
>>
>>55604169
fizzbuzz is an &'static str, i.e. a string which is in the txt segment of the program. to_string() returns a String, which is a heap- (or stack-)allocated object which points to the string. Instead of this, another option was to -> &str for the function signature, and n.to_string().as_slice() instead of just n.to_string().
>>
>>55602486
reading that felt like being waterboarded
>>
>>55603023
Like I said in another post, I was aiming at code reusability, not the most obvious or readable solution.
Notice how you use "n %" twice?
Notice how your function returns "fizzbuzz" instead of concatenating "fizz" and "buzz"
>>
console.log(Array.apply(null, Array(100)).map((a, b) => (++b%3?'':'Fizz')+(b%5?'':'Buzz')||b).join('\n'));

perfection
>>
>>55604377
Sounds like a mistake copied from C++.
>>
>>55604866
inbred >>>/reddit/
>>
>>55604964
That doesn't exist in C++ and isn't a mistake either.
>>
>>55605071
String literals aren't
std::string
because of backwards compatibility with C.
>>
>>55605102
Completely unrelated.
>>
>>55602711
Meme or not, you're code a shit. It's hard to read, yet not impressive or clever in any way.

If this was my introduction to OCaml I'd say OP answers itself.
>>
>>55603333
Sweet quads, mate.
>>
>>55603092
>Sans-serif fonts anywhere
Disgusting.
>>
>>55605173
Thanks my dude, I was afraid nobody noticed for a second there.
>>
>>55605138
>>55604866
>>
>>55605364
Have you tried getting a brain or do you think repeating "I'm clinically retarded! Look at me!" is making you look smart?
>>
>>55605454
gr8 retort, m8
>>
>>55603023
Oh shit, I didn't know Rust had this. Time to jump on the meme train.
>>
Elixir master race coming through

whichfizz = fn
(0, 0, _) -> "FizzBuzz"
(0, _, _) -> "Fizz"
(_, 0, _) -> "Buzz"
(_, _, n) -> n
end

fizzbuzz = fn (n) ->
whichfizz.(rem(n, 3), rem(n, 5), n)
end

IO.inspect Enum.map(1..100, fizzbuzz)


Thanks, mods can close the thread now that the defacto solution has been found.
>>
If your code has the "FizzBuzz" string, you're doing it wrong.
>>
>>55605938
>ruby syntax
why
>>
>>55605938
You're a faggot.
Why the hell are you passing 3 fucking arguments to your first function instead of stating n mod 3/5/15/whatever in your pattern matching conditions?
>>
>>55602778
>oh no, I called the same function twice with different arguments
>better wrap it in a pointless function that just calls it anyway
You're retarded. I know that modulus is resource intensive, and that's why I store the results in variables so I don't have to do the same thing twice.
>>
>>55606153
also, what's wrong with ruby syntax?
>>
>>55606547
too oop-y
too much trying to be like the english language
>>
>>55606571
well, always better than an average Java/C#/PHP cancer.
>>
>>55606532
>Pretending the real performance hurdle isn't printing.
>>
>>55606681
Well then, what am I supposed to do? Manually inspect the memory to see the results?
>>
>>55606724
You're supposed to not care because it'll always be slow as fuck
>>
>>55606659
those suffer from similar issues
>>
Back with more OCamemes
let rec f n s =
if n > 100
then print_string s
else
f (n+1) (s ^ "\n" ^ (
(fun x -> if x = "" then string_of_int n else x) (
(fun x -> if n mod 5 = 0 then x^"buzz" else x^"") (
(fun x -> if n mod 3 = 0 then x^"fizz" else "")
""))))
in f 0 "";;
>>
<html><body>fizzbuzz   fizz  buzz fizz   fizz buzz  fizz   fizzbuzz   fizz  buzz fizz   fizz buzz  fizz   fizzbuzz   fizz  buzz fizz   fizz buzz  fizz   fizzbuzz   fizz  buzz fizz   fizz buzz  fizz   fizzbuzz   fizz  buzz fizz   fizz buzz  fizz   fizzbuzz   fizz  buzz fizz   fizz buzz  fizz   fizzbuzz   fizz  buzz fizz   fizz 
</body></html>
>>
More OCaMemes!
let rec f n s =
if n > 100 then print_string s
else
f (n+1) (s ^ "\n" ^ (
let cond n x s2 s1 =
if n mod x = 0 then s2^s1 else s1 in
(fun x -> if x = "" then string_of_int n else x)
(
(cond n 3 "fizz")
(cond n 5 "buzz" "")
)
))
in f 0 "";;
>>
I'm enjoying myself way too much
let rec f n s = match n with
|100 -> print_string s
|_ ->
f (n+1) (s ^ "\n" ^ (
let cond n x s2 s1 = match n mod x with
|0 -> s2^s1
|_ -> s1
in
(fun x -> match x with
| "" -> string_of_int n
|_ -> x
)
((cond n 3 "fizz")
(cond n 5 "buzz" "")
)
))
in f 0 "";;
>>
>>55605938
That's not how you FizzBuzz in Elixir.

1..100 |> Stream.map(fn(x) ->
case {rem(x, 3), rem(x, 5)} do
{0, 0} -> "FizzBuzz"
{0, _} -> "Fizz"
{_, 0} -> "Buzz"
{_, _} -> x
end
end) |> Enum.each(&IO.puts/1)


And here it is with CHICKEN:

(use streams matchable clojurian-syntax)

(->> (stream-range 1 101)
(stream-map (lambda (x)
(match (list (remainder x 3)
(remainder x 5))
((0 0) "FizzBuzz")
((0 _) "Fizz")
((_ 0) "Buzz")
((_ _) x))))
(stream-for-each print))
>>
>>55608415
THIS is one good elixir implementation.
Also, what's with
clojurian-syntax
in scheme?
>>
>>55608578
Retard.
>>
>>55608589
why?
>>
>>55608578
clojurian-syntax
is a CHICKEN egg which provides the ->> and -> macros, which are from Clojure. It lets you write things in a pipeline kind of like how you use |> in Elixir.
>>
>no haskell
% = mod

fizzbuzz n | n % 15 == 0 = "FizzBuzz"
| n % 5 == 0 = "Buzz"
| n % 3 == 0 = "Fizz"
| otherwise = show n

fizzbuzz n = case (n % 5, n % 3) of
(0, 0) -> "FizzBuzz"
(0, _) -> "Buzz"
(_, 0) -> "Fizz"
(_, _) -> show n

there, two different wa to fizzbuzz
>>
>>55608977
>doesn't print anything
Haskelfags everybody!
>>
>>55609037
I know that, it just said that those are two ways to implement fizzbuzz in haskell.

And its not valid haskell code because it has to names that conflict, you dumb negro.
Thread replies: 92
Thread images: 2

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.