Skip to content

Instantly share code, notes, and snippets.

@snluu
snluu / Global.scala
Created May 28, 2012 19:42
Play Framework: Handle URL trailing slash with Application Global
import play.api._
import play.api.mvc._
object Global extends GlobalSettings {
override def onHandlerNotFound(request: RequestHeader): Result = {
// handle trailing slashes
if (request.path.endsWith("/")) {
// construct a new URI without the slash
val lnOf2 = scala.math.log(2) // natural log of 2
def log2(x: Double): Double = scala.math.log(x) / lnOf2
@snluu
snluu / redirect.scala
Created June 14, 2012 05:47
Play framework redirect
val newID = Inventory.save(data)
Redirect(
controllers.routes.Inventory.index.toString +
"#record_%d".format(newID)
).flashing("success" -> "Insertion succeeded!")
using System.Threading.Tasks;
static void SleepThenPrintSquare(int n) {
Thread.Sleep(2000);
Console.WriteLine("{0}", n * n);
}
static void Main() {
var tasks = new List<Task>(5);
using System.Threading.Tasks;
static void SleepThenPrintSquare(int n) {
Thread.Sleep(2000);
Console.WriteLine("{0}", n * n);
}
static void Main() {
var tasks = new List<Task>(5);
void MergeList(ref List<int> list1, List<int> list2)
{
int list1Size = list1.Count;
int list2Size = list2.Count;
int totalSize = list1Size + list2Size;
list1.Capacity = totalSize;
int index1 = 0;
int index2 = 0;
void MergeListImproved(ref List<int> list1, List<int> list2)
{
int list1Size = list1.Count;
int list2Size = list2.Count;
int totalSize = list1Size + list2Size;
var newList = new List<int>(totalSize);
int index1 = 0;
int index2 = 0;
@snluu
snluu / GoogleDoodleBasketball2012.java
Created August 8, 2012 17:24
Java code to play google doodle for you
Robot r = new Robot();
System.out.println("Start!");
Thread.sleep(2000); // give yourself some margin to switch window
int[] times = { 1, 5, 4, 4, 1, 3, 2 }; // number of shots for each distance
int[] delays = { 400, 400, 575, 820, 850, 1150, 1350 }; // time holding the ball
int[] delays2 = { 200, 650, 700, 600, 400, 450, 400 }; // time waiting for a new ball
for (int i = 0; i < times.length; i++)
for (int j = 0; j < times[i]; j++) {
r.keyPress(KeyEvent.VK_SPACE);
r.delay(delays[i]);
@snluu
snluu / shawty.js
Last active October 11, 2015 10:38
A shawty callback example
shawty({
'success': 1,
'message': '',
'short': 'http://luu.bz/bD',
'long': 'http://en.wikipedia.org/wiki/URL_shortening',
'hits': 0,
'timestamp': 1349564105
});
@snluu
snluu / index.html
Last active October 11, 2015 10:38
Example of a page embedding shawty
<script>
function shawty(data) {
if (data.success) {
document.write(
'<a href="' + data.short +
'">Short URL to the current page</a>');
}
}
</script>
<script src="//luu.bz/shawty.js"></script>