Unicode symbols as function names
Unicode stings for function/method names is a very rare scenario, but who knows in the near future people might use them daily! [ Too much of an expectation aye? ;) ]
Say : one needs to convert dollars to pounds and writes a method to do so, might name it "dollar2pound", but a code monkey might name it $2£
Below is the result of such a play in few of my favorite programming languages :
To makes things silly/simple, lets assume our methods takes an integer argument and prints ❤'s those many time.
First up is BASH [ Simple, undocumented so might stop working soon! ]:
function ❤ () { yes '❤' | head -n$1 | xargs echo; } # Invoke the function ❤ 12 # Bazinga! we got 12 ❤s :) ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤
Python's turn :
l={} l['❤'] = lambda x: "❤ "*x print l['❤'](12).decode('utf8') ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤
Perl! here it comes! [ Yes you are right, no strict ] :
use utf8; *{'❤'} = sub { print '❤ ' x shift; }; ${\'❤'}->(12); ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤
Ruby [ Don't forget -Ku ] :
#!/usr/bin/ruby -Ku def ❤(x) puts '❤ '*x; end ❤(12) ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤
JavaScript FTW!?
String.prototype.repeat = function(n) { return (new Array(n+1)).join(this.valueOf()); }; this["❤"] = function (x) { return '❤ '.repeat(x); }; this["❤"](12) ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤
Update :
function Σ() {return [].reduce.call(arguments, function(a,b){return a+b})}; Σ(1,2,3,4) 10
So how do you do it in your fav programming languages ;) ?
Recent blog posts
- watir-webdriver web inspector
- gem list to gemfile
- Packing ruby2.0 on debian.
- Made it into The Guinness Book!
- to_h in ruby 2.0
- Filter elements by pattern jQuery.
- Better HTML password fields for mobile ?
- Grayscale image when user offline
- nth-child CSS pseudo-class Christmas colors
- EventEmitter in nodejs