Hemanth's Scribes

web

Unicode Symbols as Function Names

Author Photo

Hemanth HM

Thumbnail

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 ;) ?

#javascript#linux
Author Photo

About Hemanth HM

Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.