4 ms·
I know scala is very powerful, but I think this code proofs to me that scala code is difficult to read. I'm a pro functional programmer, but scala mixes too man
by TeeWEE 12y ago
I know scala is very powerful, but I think this code proofs to me that scala code is difficult to read. I'm a pro functional programmer, but scala mixes too many concepts.
'pseudo' Python url shorterner in 14 lines of code:
from webapp2 import RequestHandler
import redis
def shorten_url(url):
import base64
return base64.encode(url)[:7]
class Handler(RequestHandler):
def get(self, path):
self.redirect(redis.get(path))
def post(self, path):
redis.put(path, shorten_url(path))
- GregorStocks 12y agoI believe you'll have a lot of collisions that way, as two URLs with the same 7-byte suffix in base64 will have the same key. The Scala version uses 7 random characters, which I think will lead to a collision after about sqrt(36^7) entries due to the birthday paradox (62^7 if alphanumeric includes both uppercase and lowercase). That's better, but I'd recommend just using something autoincrementing instead, which in theory makes collisions impossible.
- TeeWEE 12y agoI agree. It was a short pseudo implementation.