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.<p>'pseudo' Python url shorterner in 14 lines of code:<p><pre><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))</code></pre>
As far as I am aware, the standard implementation of a URL shortening algorithm is to convert the number to a higher base, not to generate a random string. This has the advantages of greater loading speed, and smaller storage size.<p>Here is one I wrote long enough ago for me to be divorced from its implementation flaws: <a href="https://github.com/TShadwell/go-shorten/blob/master/shorten/shorten.go#L84" rel="nofollow">https://github.com/TShadwell/go-shorten/blob/master/shorten/...</a>
Now that Twitter auto-shortens URLs (and expands them in the UI), is there really a use left for URL-shorteners? Are there any other major platforms where text length matters?
The site is blocked at my work so I can't see the implemtation but here is one I wrote back in march that is 79 lines: <a href="https://gist.github.com/eliotfowler/9360895" rel="nofollow">https://gist.github.com/eliotfowler/9360895</a>
Here's another one in 27 lines of Clojure: <a href="http://beauhinks.com/simple-url-shortener-with-clojure/" rel="nofollow">http://beauhinks.com/simple-url-shortener-with-clojure/</a>
And another one I found, in 81 lines of Haskell (may be interesting for comparison):
<a href="https://github.com/ryantrinkle/memoise/blob/master/src/Main.hs" rel="nofollow">https://github.com/ryantrinkle/memoise/blob/master/src/Main....</a><p>There's also a presentation to go with it: <a href="http://vimeo.com/59109358" rel="nofollow">http://vimeo.com/59109358</a>