What is This?

An AJAX service which runs a given script. The result is returned as JSONP.

http://api.dan.co.jp/lleval.cgi?c=callback&s=source

When l=language is ommited, #! is used.

With jQuery the code would be as follows. LLEval(srcNode, dstNode, lang) where srcNode is the node in which the source code resides, dstNode where you want to put the result, and lang (optional) in which language the code gets executed.

(function($){

var keysOf = function(o){ 
    var keys = [];
    for (var k in o) keys[keys.length] = k;
    return keys;
};

json2dl = function(json){
    var dl = $('<dl/>');
    $.each(keysOf(json).sort(), function(i, k){
        $('<dt/>').text(k).appendTo(dl);
        var dd = $('<dd/>').text(json[k])
        if (k.match(/^std/)) dd.css({fontFamily:'monospace',whiteSpace:'pre'});
        dd.appendTo(dl);
    });
    return dl;
};

LLEval = function(srcNode, dstNode, lang){
    var query = {s:(srcNode.value || $(srcNode).text())};
    if (lang) query['l'] = lang;
    var url = 'http://api.dan.co.jp/lleval.cgi';
    var proto = 'json';
    if ($.browser.msie || $.browser.opera){ 
        url   += '?c=?';
        proto += 'p';
    } /* falls back to JSONP */
    $.get(url, query, function(json){
        $(dstNode).html(json2dl(json));
    }, proto);
};

})(jQuery);

Without jQuery it would be like:

(function(d){

LLEval = function(src, dst, ll){
    runJSON(ll || 'll', function(json){
        dst.innerHTML = '';
        dst.appendChild(json2dl(json));
    }, src.value, ll);
};
runJSON = function(name, func, src, ll){
    if (! this['JSONP']) JSONP = {};
    JSONP[name] = func;
    (function(uri){
        var s = d.createElement('script');
        s.charset = 'UTF-8';
        s.src = uri + '&s=' + encodeURIComponent(src);
        if (ll) s.src += '&l=' + encodeURIComponent(ll)
        s.id = s.src;
        d.body.appendChild(s);
    })('http://api.dan.co.jp/lleval.cgi?c=JSONP.' + name, src, ll);
};

var appendText = function(e, t){
    e.appendChild(d.createTextNode(t));
};

json2dl = function(json){
    var keys = [];
    for (var k in json) keys[keys.length] = k;
    keys.sort();
    var dl = d.createElement('dl');
    dl.style.width = '100%';
    for (var i = 0, l = keys.length; i < l; i++){
        var k = keys[i]; var v = json[k];
        var dt = d.createElement('dt');
        appendText(dt, k);
        dl.appendChild(dt);
        var dd = d.createElement('dd');
        appendText(dd, v);
        if (keys[i].match(/^std/)){
            dd.style.fontFamily = 'monospace';
            dd.style.whiteSpace = 'pre';
        }
        dl.appendChild(dd);
    }
    return dl;
};

})(document);

To get a list of languages suppoted, access

http://api.dan.co.jp/lleval.cgi?q=1
(function($){

askLanguages = function(dstNode){
    var query = {q:1};
    var url = 'http://api.dan.co.jp/lleval.cgi';
    var proto = 'json';
    if ($.browser.msie || $.browser.opera){ 
        url   += '?c=?';
        proto += 'p';
    }
    $.get(url, query, function(json){
        delete json['error'];
        $(dstNode).html(json2dl(json));
    }, proto);
}

})(jQuery);

Caveat

NO WARRANTY

Whatsoever.

Enjoy!

Dan the LL Monger

Notes on specific languages

Perl 5

Best Supported. As a matter of fact this service is written in perl. Not only core modules but also popular modules like LWP are available

use v5.16;
use warnings;
for my $n ( 1 .. 30 ) {
    my $fz = '';
    $fz .= 'Fizz' unless $n % 3;
    $fz .= 'Buzz' unless $n % 5;
    say $fz || $n;
}

Perl 5.20

use v5.20;             # this does not suffice.
use feature ':all';    # you need this, too.
use warnings;
# no warnings 'experimental';   # if you want to suppress the warnings
sub fizz($n)     { ('Fizz')[ $n % 3 ]  || '' }
sub buzz($n)     { ('Buzz')[ $n % 5 ]  || '' }
sub fizzbuzz($n) { fizz($n) . buzz($n) || $n }
say fizzbuzz $_ for 1 .. 30;

Perl 6 (rakudo)

Very slow. So slow I gave twofour seconds.

[1..30].map({
  { $^n % 3 ?? '' !! 'Fizz' }($_)
  ~
  { $^n % 5 ?? '' !! 'Buzz' }($_)
  || $_
}).join("\n").say;

awk

-f is needed to run as a script

BEGIN {
   for (NUM = 1; NUM <= 30; NUM++)
       if     (NUM % 15 == 0) { print "FizzBuzz" }
       else if (NUM % 3 == 0) { print "Fizz" }
       else if (NUM % 5 == 0) { print "Buzz"}
       else                   { print NUM }
}

BASIC (bwbasic)

bwbasic does not support shebang but this service does (kinda).

10 FOR N=1 TO 30
20 LET F$ = ""
30 IF N MOD 3 = 0 THEN LET F$ = "Fizz"
40 IF N MOD 5 = 0 THEN LET F$ = F$ + "Buzz"
50 IF F$ = "" THEN LET F$ = N
60 PRINT F$
70 NEXT N
80 QUIT

Brainf.ck

! notation supported. Use it for stdin replacement.

++++++[->++++> >+>+>-<<<<<]>[<++++> >+++>++++> >+++>+++++>++
+++> > > > > >++> >++<<<<<<<<<<<<<<-]<++++>+++>-->+++>-> >--
->++> > >+++++[->++>++<<]<<<<<<<<<<[->-[> > > > > > >]>[<+++
>.>.> > > >..> > >+<]<<<<<-[> > > >]>[<+++++>.>.>..> > >+<]>
 > > >+<-[<<<]<[[-<<+> >]> > >+>+<<<<<<[-> >+>+>-<<<<]<]>>[[
-]<]>[> > >[>.<<.<<<]<[.<<<<]>]>.<<<<<<<<<<<]

C

You need to specify #!/usr/bin/tcc -run if you want to use shebang.

#include <string.h>
char *fizz(int n){ return n % 3 ? "" : "Fizz"; }
char *buzz(int n){ return n % 5 ? "" : "Buzz"; }
char *fizzbuzz(char *buf, int n){
  strcpy(buf, fizz(n));
  strcat(buf, buzz(n));
  if (!strlen(buf)) sprintf(buf, "%d", n);
  return buf;
}
#include <stdio.h>
void main(){
  int i;
  char buf[12];
  for (i = 1; i <= 30; i++){
    printf("%s\n", fizzbuzz(buf, i));
  }
}

Emacs Lisp

You need to specify #!/usr/bin/emacs --script if you want to use shebang.

(print (mapcar (lambda (num)
    (cond
     ((= (% num 15) 0) "FizzBuzz")
     ((= (% num  3) 0) "Fizz")
     ((= (% num  5) 0) "Buzz")
     (t num)))
  '( 1  2  3  4  5  6  7  8  9 10
    11 12 13 14 15 16 17 18 19 20
    21 22 23 24 25 26 27 28 29 30)))

ECMAScript 6

node.js 0.11 with --harmony

function *fizzBuzz() {
    for (var i = 1; i <= 30; i++) {
        var fz = '';
        if (i % 3 == 0) fz += 'Fizz';
        if (i % 5 == 0) fz += 'Buzz';
        yield fz || i;
    }
}
for (n of fizzBuzz()) {
    console.log(n);
}

Common Lisp (ecl)

You need to specify #!/usr/bin/ecl -shell if you want to use shebang.

(print (loop for i from 1 to 30
 collect
 (cond
  ((= 0 (mod i 15)) "FizzBuzz")
  ((= 0 (mod i 3)) "Fizz")
  ((= 0 (mod i 5)) "Buzz")
  (t i))))

Scheme (gauche)

(use srfi-1)
(for-each 
 print
 (map
  (lambda (x) 
    (cond 
     ((= (modulo x 15) 0) "FizzBuzz")
     ((= (modulo x 5) 0) "Buzz")
     ((= (modulo x 3) 0) "Fizz")
     (else x)))
  (iota 30 1)))

Grass

wvwwWWwWWWwvWwwwwWWwWWWwWWWWwWWWWWwWWWWWWwWWWWWWWwWwwwwwwwwwwwwW
WWWwWWWWWWWwWWWWWWWWWWWWWWwWWWWWWWWWWWwwWWWWWWWWWWwwWWWWWWWWWWWW
wWWWWWWWWWWwwWWWWWWWWWWwwwwwwWWWWWWWWWWWWWWWwWWWWWWWWWWWWWWWWWWW
WWwWWWWWWWWWWWWWWWWWWwwWWWWWWWWWWWWWWWWWwwWWWWWWWWWWWWWWWWWwwwww
WWWWWWWWWWWWWWWWWWWWwwWWWWWWWWWWWWWWWWWWWWWWwWWWWWWWWWWWWWWWWWWW
WWWWWWwwwwwwwwwwwwwwwwwwwwwwwwwwWwwwwwwwwwwWWwwwwwwwWWWwwwwwwwWW
WWwWWWWWwwwwwwwwWWWWWWwwwwwwwwwwwwwwwwWWWWWWWwwwwwwwwwwwwwwwwwww
wWWWWWWWWwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwWWWWWWWWWwwwwWWWWWW
WWWWwwwwwwwwwwwWWWWWWWWWWWwwwwwwwWWWWWWWWWWWWwwwwwwwwwwwwwwwwwwW
WWWWWWWWWWWWwwwwwwwwwwwwwwwwwwwwwwwww

Haskell (runhugs)

main = mapM_ (putStrLn . fizzbuzz) [1..30]

fizzbuzz n
   = if or $ map (\s->length s /= 0) [fizz, buzz] 
        then fizz ++ buzz 
        else show n
     where fizz = if mod n 3 == 0 then "Fizz" else ""
           buzz = if mod n 5 == 0 then "Buzz" else ""

IO

for (n, 1, 30,
    fb := list (
        if (n % 3 == 0, "Fizz"),
        if (n % 5 == 0, "Buzz")) select (isTrue)
 
    if (fb isEmpty, n, fb join) println
)

JavaScript

node.js 0.11

for (var i = 1; i <= 30; i++) console.log (
    i % 15 
        ? i % 5
            ? i % 3
                ? i : 'Fizz'
            : 'Buzz'
        : 'FizzBuzz'
);

Lazy K

Unlambda, iota, and jot notations are all supported.

Due to the space constraints, the following example in iota just prints "Hi" instead of FizzBuzz in other examples.

**i*i*ii***i*i*i*ii***i*i*i*ii*ii**i*i*ii***i*i*i*ii**i*i*ii****
i*i*i*ii*ii*ii***i*i*i*ii***i*i*i*ii**i*i*ii*i*i*i*ii*i*i*ii*ii*
**i*i*i*ii***i*i*i*ii**i*i*ii*i*i*i*ii*i*i*ii***i*i*i*ii***i*i*i
*ii**i*i*ii*i*i*i*ii*i*i*ii****i*i*i*ii***i*i*i*ii*ii*ii*ii***i*
i*i*ii***i*i*i*ii**i*i*ii*i*i*i*ii*i*i*ii*ii**i*i*ii***i*i*i*ii*
**i*i*i*ii*ii**i*i*ii***i*i*i*ii***i*i*i*ii**i*i*ii*i*i*i*ii*i*i
*ii***i*i*i*ii**i*i*ii****i*i*i*ii*ii*ii***i*i*i*ii***i*i*i*ii**
i*i*ii*i*i*i*ii*i*i*ii*ii***i*i*i*ii***i*i*i*ii**i*i*ii*i*i*i*ii
*i*i*ii****i*i*i*ii***i*i*i*ii**i*i*ii*i*i*i*ii*i*i*ii*ii***i*i*
i*ii***i*i*i*ii**i*i*ii*i*i*i*ii*i*i*ii****i*i*i*ii*ii*ii***i*i*
i*ii***i*i*i*ii**i*i*ii*i*i*i*ii*i*i*ii*ii**i*i*ii***i*i*i*ii***
i*i*i*ii*ii**i*i*ii***i*i*i*ii**i*i*ii***i*i*i*ii***i*i*i*ii**i*
i*ii*i*i*i*ii*i*i*ii*ii***i*i*i*ii***i*i*i*ii**i*i*ii*i*i*i*ii*i
*i*ii****i*i*i*ii*ii*ii***i*i*i*ii***i*i*i*ii**i*i*ii*i*i*i*ii*i
*i*ii*ii**i*i*ii**i*i*ii****i*i*i*ii*ii*ii****i*i*i*ii*ii*ii***i
*i*i*ii***i*i*i*ii**i*i*ii*i*i*i*ii*i*i*ii*ii

Lua

function fizzbuzz(n)
  if n % 15 == 0 then
    return "FizzBuzz"
  elseif n % 3 == 0 then
    return "Fizz"
  elseif n % 5 == 0 then
    return "Buzz"
  else
    return n
  end
end

for i=1,30 do
  print(fizzbuzz(i))
end

m4

define(`for',
   `ifelse($#,0,``$0'',
   `ifelse(eval($2<=$3),1,
   `pushdef(`$1',$2)$5`'popdef(`$1')$0(`$1',eval($2+$4),$3,$4,`$5')')')')
 
for(`x',1,30,1,
   `ifelse(eval(x%15==0),1,FizzBuzz,
   `ifelse(eval(x%3==0),1,Fizz,
   `ifelse(eval(x%5==0),1,Buzz,x)')')
')

OCaml

/usr/bin/ocaml is built as:

ocamlmktop -custom unix.cma -o ocaml
let fizzbuzz n =
  let fizz = if n mod 3 == 0 then "Fizz" else "" in
  let buzz = if n mod 5 == 0 then "Buzz" else "" in
  if [] != List.filter (fun s -> String.length s != 0) [fizz;buzz]
     then fizz ^ buzz
     else string_of_int n

let puts s = print_string( s ^ "\n");;

for i = 1 to 30 do
  puts (fizzbuzz i)
done;;

PHP

<?php
  for ($i=1;  $i <= 30; $i++){
    echo $i % 15 ? 
      ( $i % 5 ? 
        ( $i % 3 ? $i : "Fizz" ) 
      : "Buzz")
    : "FizzBuzz";
    echo "\n";
  }
?>

Python 2.X

def fizzbuzz(n): 
    for i in range(1, n+1): 
        if   i % 15 == 0: yield 'FizzBuzz' 
        elif i %  5 == 0: yield 'Buzz' 
        elif i %  3 == 0: yield 'Fizz' 
        else:             yield i

for x in fizzbuzz(30): print x

Python 3.X

def fizzbuzz(n): 
    for i in range(1, n+1): 
        if   i % 15 == 0: yield 'FizzBuzz' 
        elif i %  5 == 0: yield 'Buzz' 
        elif i %  3 == 0: yield 'Fizz' 
        else:             yield i

for x in fizzbuzz(30): print(x) # print() is now a function

Ruby 1.9

# http://d.hatena.ne.jp/takatoh/20090207/fizzbuzz
fizzbuzz = Fiber.new do
  fizz = ["", "", "Fizz"].cycle
  buzz = ["", "", "", "", "Buzz"].cycle
  n = 1
  while
    s = fizz.next + buzz.next
    Fiber.yield(s == "" ? n.to_s : s)
    n += 1
  end
end

30.times{ puts fizzbuzz.resume }

Ruby 2.0

# http://jp.rubyist.net/magazine/?0041-200Special-lazy
fizzbuzz = Enumerator.new { |yielder|
  1.upto(Float::INFINITY) do |n|
    case
      when n % 15 == 0 then yielder << "FizzBuzz"
      when n % 5 == 0 then yielder << "Buzz"
      when n % 3 == 0 then yielder << "Fizz"
      else yielder << n.to_s
    end
  end
}

fizzbuzz.first(30).each do |str|
  puts str
end

Ruby 2.1

module FizzBuzz
  refine Fixnum do
    def fizzbuzz()
      fz = ''
      if (self % 3 == 0) then fz += 'Fizz' end
      if (self % 5 == 0) then fz += 'Buzz' end
      if (fz == '') then self.to_s else fz end
    end
  end
end

eval <<EOB
(1..30).each do |n|
  using FizzBuzz
  puts n.fizzbuzz
end
EOB

puts 42.fizzbuzz

PostScript (ghostscript)

gs does not support shebang but this service does (kinda).

1 1 30 
{ dup 15 mod 0 eq {(FizzBuzz)}
{ dup 3 mod 0 eq {(Fizz)} 
{ dup 5 mod 0 eq {(Buzz)}
if} ifelse } ifelse = clear }
for 

sh

Actually bash. Note you can only use internal commands since lleval forbids exec* and *fork.

for i in {1..30}
do
   fz=""
   if [ $[$i % 3] == 0 ]; then fz=${fz}fizz; fi
   if [ $[$i % 5] == 0 ]; then fz=${fz}buzz; fi
   if [ "$fz" == "" ]; then fz=$i; fi
   echo $fz
done

Tcl

for {set i 1} {$i <= 30} {incr i} {
    if {![expr $i % 3] && ![expr $i % 5]} {
        puts "FizzBuzz"
    } elseif {![expr $i % 3]} {
        puts "Fizz"
    } elseif {![expr $i % 5]} {
        puts "Buzz"
    } else {
        puts $i
    }
}