A URL shortener service in 45 lines of Scala

  • 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))

  • 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.

    Here is one I wrote long enough ago for me to be divorced from its implementation flaws: https://github.com/TShadwell/go-shorten/blob/master/shorten/...

  • 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: https://gist.github.com/eliotfowler/9360895

  • Here's another one in 27 lines of Clojure: http://beauhinks.com/simple-url-shortener-with-clojure/

  • What would be the minimum path size in the shortened url to ensure a large enough capability of storage without letting users guess other urls easily?

  • Where does this handle getting the same random shortened url twice?

  • And another one I found, in 81 lines of Haskell (may be interesting for comparison): https://github.com/ryantrinkle/memoise/blob/master/src/Main....

    There's also a presentation to go with it: http://vimeo.com/59109358

  • Instead of working on a URL shortner, I suggest you work on not sending usernames and passwords in clear text via HTTP POSTs.