

A URL shortener service in 45 lines of Scala - lauriswtf
http://grasswire-engineering.tumblr.com/post/94043813041/a-url-shortener-service-in-45-lines-of-scala

======
TeeWEE
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
I 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
I agree. It was a short pseudo implementation.

------
tshadwell
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/...](https://github.com/TShadwell/go-
shorten/blob/master/shorten/shorten.go#L84)

~~~
xwowsersx
That makes sense. Thanks for the code.

------
skrebbel
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?

~~~
austenallred
The biggest advantage of URL shorteners, outside of the length, is the ability
to quickly generate URLs that are track-able, instead of constantly adding
campaigns with something like grasswire.com/1234?refid=twitter, which is how a
lot of marketers track where their clicks come from.

~~~
rplnt
This is only beneficial to one side of the userbase. For users clicking the
links it's only inconvenience.

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

------
markc
Here's another one in 27 lines of Clojure: [http://beauhinks.com/simple-url-
shortener-with-clojure/](http://beauhinks.com/simple-url-shortener-with-
clojure/)

------
u124556
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?

------
didip
83 lines in Go:
[https://github.com/didip/shawty](https://github.com/didip/shawty)

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

~~~
xwowsersx
Good catch - it doesn't. We'd need to add a line to check for collision and
retry. Better yet is to use hashing instead of random string as some folks
mentioned in the comments on the blog.

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

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

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

