Skip to content

Instantly share code, notes, and snippets.

public static class ObjectHelper {
public static T? Min<T>(T? x, T? y) where T : struct, IComparable {
if (x.HasValue && y.HasValue) {
return x.Value.CompareTo(y.Value) <= 0 ? x : y;
} else if (x.HasValue) {
return x;
} else {
return y;
}
}
function randomInt(from, to) {
var interval = to - from + 1;
return function() {
return Math.floor(Math.random() * interval) + from;
}
}
function every(times, callback) {
var count = times;
return function() {
count--;
if(count === 0) {
count = times;
callback();
}
}
}
function after(times, callback) {
return function() {
times--;
if(times === 0) {
callback();
}
}
}
function asyncmap(list, op, cb) {
function after(times, callback) {
return function() {
times--;
if(times === 0) {
callback();
}
}
}
@pietrom
pietrom / DateTimeExtension.cs
Created September 22, 2015 15:28
DateTimeExtension
public static class DateTimeExtension {
public static Int64 Epoch(this DateTime date)
{
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
return Convert.ToInt64((date - epoch).TotalMilliseconds) ;
}
public static DateTime ToDateTime(this long unixTime)
{
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
db.messages.aggregate([
{ $unwind: '$headers.To' },
{ $group: { _id: { id: '$_id', from: '$headers.From'}, to: { $addToSet: '$headers.To' }}},
{ $unwind: '$to' },
{ $group: { _id: { from: '$_id.from', to: '$to'}, hits: {$sum: 1}}},
{ $match: { hits: {$gt: 1} } },
{ $sort: { hits: -1 }}
]);
@pietrom
pietrom / gist:099a7701565c44881126
Created May 5, 2015 09:21
Starting ssh-agent and registering a private key
# Starts ssh-agent
eval $(ssh-agent)
# Adds private key to ssh-agent
ssh-add /path/to/private/key
@pietrom
pietrom / pom.xml
Created April 8, 2015 08:41
Maven pom.xml with compiler-pluging and charset configuration and dependency/dependency management templates
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
<name></name>
<packaging></packaging>
<dependencyManagement>
@pietrom
pietrom / empty.jsp
Created April 8, 2015 07:34
Empty JSP with UTF-8 charset declaration
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
</head>
<body>