Skip to content

Instantly share code, notes, and snippets.

@tsaniel
Forked from 140bytes/LICENSE.txt
Created March 12, 2012 08:56
Show Gist options
  • Save tsaniel/2020791 to your computer and use it in GitHub Desktop.
Save tsaniel/2020791 to your computer and use it in GitHub Desktop.
Romanize

140byt.es

A tweet-sized, fork-to-play, community-curated collection of JavaScript.

How to play

  1. Click the Fork button above to fork this gist.
  2. Modify all the files to according to the rules below.
  3. Save your entry and tweet it up!

Keep in mind that thanks to the awesome sensibilities of the GitHub team, gists are just repos. So feel free to clone yours and work locally for a more comfortable environment, and to allow commit messages.

Rules

All entries must exist in an index.js file, whose contents are

  1. an assignable, valid Javascript expression that
  2. contains no more than 140 bytes, and
  3. does not leak to the global scope.

All entries must also be licensed under the WTFPL or equally permissive license.

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

function(
a, // input for a number within the range of 0~3999
b, // placeholder for
c, // placeholder for
d, // placeholder for
e // placeholder for
) {
for (
b = c = '' //
, d = 5; //
a; //
c++ //
, d ^= 7 // 5 ^ 7 = 2, 2 ^ 7 = 5
)
for (
e = a % d //
, a = a / d ^ 0; //
e--; //
)
b = 'IVXLCDM'.charAt(
e > 2 ? //
c + a - (a &= ~1) + (e = 1) //
:
c //
) + b //
return b
}
function(a,b,c,d,e){for(b=c='',d=5;a;c++,d^=7)for(e=a%d,a=a/d^0;e--;)b='IVXLCDM'.charAt(e>2?c+a-(a&=~1)+(e=1):c)+b;return b}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "theNameOfYourLibWhichMustBeAValidCamelCasedJavaScriptIdentifier",
"description": "This should be a short description of your entry.",
"keywords": [
"five",
"descriptive",
"keywords",
"or",
"fewer"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>undefined</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var myFunction = function(){ /* the code here should be identical to the entry. */ }
document.getElementById( "ret" ).innerHTML = myFunction()
</script>
@subzey
Copy link

subzey commented Mar 14, 2012

Wow, great!

@maettig
Copy link

maettig commented Mar 20, 2012

It's an decimal to roman converter for all numbers from 0 to 3999 in 124 bytes. Should be enough for the next two thousand years. I'm speechless.

@tsaniel
Copy link
Author

tsaniel commented Mar 21, 2012

Since I'm busy preparing for my examination these days, I'll annotate the code later.

@maettig
Copy link

maettig commented Mar 21, 2012

It even abuses charAt in a clever way. charAt returns an empty string instead of "undefined" for invalid positions. That's why it can't be replaced with [].

@tsaniel
Copy link
Author

tsaniel commented Mar 22, 2012

You can even replace "IVXLCDM" with "IVXLCDMↁↂ".

@williammalo
Copy link

@tsaniel
Hey! Tsaniel! I have no idea how to send pms on github, so I'll post this here. I'm a hardcore javascript minification nerd. And you seem to be one too, so I thought ide ask you if you know any code-golfer communities like 140bytes where I could share this hobby with others.
Thanks! :)

@williammalo
Copy link

This snippet is full of win!
"a&=~1" == "a=Math.floor(a/2)*2"
"d^=7" == "if(d==5)d=2;if(d==2)d=5"
"a=a/d^0" == "a=Math.floor(a/d)"
" c='' " == " c=0 "

if I were you, ide replace "a&=~1" with "a&=-2"
It does the same thing, it's the same length, and it's more descriptive.

@williammalo
Copy link

I found another short romanizer on the internet, maybe you can take tricks from it.
function(a,b,c,d,e){for(b='',d=0,e='IIII0V0XXXX0L0CCCC0D0MMMM'.split(0);7>d;d++)c=e[d].length+1,b=e[d].substr(0,a%c)+b,a=(a-a%c)/c;return b}

@maettig
Copy link

maettig commented Mar 24, 2012

Can you post the source URL please? And about d^=7, here is an other way to do this: d=7-d.

@williammalo
Copy link

@maettig d=7-d is one byte longer tho...
I forgot the source url...
I edited the source allot to make it small, it's barely recognizable.

@tsaniel
Copy link
Author

tsaniel commented Mar 24, 2012

@williammalo: Unfortunately, I don't know other code-golfer communities, and your romanizer has a bug (e.g. 4 -> IIII).
Shave some bytes off though:
function(a,b,c,d,e){for(e='IIII0V0XXXX0L0CCCC0D0MMMM'.split(d|=b='');7>d;a=(a-a%c)/c)c=d&1?2:5,b=e[d++].substr(0,a%c)+b;return b}

@williammalo
Copy link

made it shorter =D
function(a,b,c,d,e){for(b=c='',d=5;a;c++,d^=7)for(e=a%d,a=a/d^0;e--;)b='IVXLCDM'[e>2?c+a-(a&=~1)+(e=1):c|0]+b;return b}

@tsaniel
Copy link
Author

tsaniel commented Apr 4, 2012

@williammalo: Unfortunately your code is not compatible with older browsers.

@williammalo
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment