TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

A URL shortener service in 45 lines of Scala

27 pointsby lauriswtfover 10 years ago

10 comments

TeeWEEover 10 years ago
I know scala is very powerful, but I think this code proofs to me that scala code is difficult to read. I&#x27;m a pro functional programmer, but scala mixes too many concepts.<p>&#x27;pseudo&#x27; 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>
评论 #8208142 未加载
tshadwellover 10 years ago
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:&#x2F;&#x2F;github.com&#x2F;TShadwell&#x2F;go-shorten&#x2F;blob&#x2F;master&#x2F;shorten&#x2F;...</a>
评论 #8207485 未加载
skrebbelover 10 years ago
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?
评论 #8207663 未加载
评论 #8207345 未加载
评论 #8207427 未加载
eliotfowlerover 10 years ago
The site is blocked at my work so I can&#x27;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:&#x2F;&#x2F;gist.github.com&#x2F;eliotfowler&#x2F;9360895</a>
markcover 10 years ago
Here&#x27;s another one in 27 lines of Clojure: <a href="http://beauhinks.com/simple-url-shortener-with-clojure/" rel="nofollow">http:&#x2F;&#x2F;beauhinks.com&#x2F;simple-url-shortener-with-clojure&#x2F;</a>
u124556over 10 years ago
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?
didipover 10 years ago
83 lines in Go: <a href="https://github.com/didip/shawty" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;didip&#x2F;shawty</a>
agscalaover 10 years ago
Where does this handle getting the same random shortened url twice?
评论 #8207482 未加载
评论 #8207417 未加载
评论 #8207471 未加载
innguestover 10 years ago
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:&#x2F;&#x2F;github.com&#x2F;ryantrinkle&#x2F;memoise&#x2F;blob&#x2F;master&#x2F;src&#x2F;Main....</a><p>There&#x27;s also a presentation to go with it: <a href="http://vimeo.com/59109358" rel="nofollow">http:&#x2F;&#x2F;vimeo.com&#x2F;59109358</a>
stevewilhelmover 10 years ago
Instead of working on a URL shortner, I suggest you work on not sending usernames and passwords in clear text via HTTP POSTs.