Skip to content

Instantly share code, notes, and snippets.

@xudifsd
xudifsd / qsort.clj
Created August 31, 2015 08:42
quick sort
(defn qsort [l]
(if (empty? l) '()
(let [f (first l)
smaller (filter #(<= % f) (rest l))
bigger (filter #(> % f) (rest l))]
(concat (qsort smaller) [f] (qsort bigger)))))
@xudifsd
xudifsd / safe-coding
Created July 10, 2015 02:44
avoid accidentally `git checkout .`, run as `safe-coding &`, exit with fg and ctrl-c
#!/bin/bash
while :
do
git add .
sleep 1
done
@xudifsd
xudifsd / memory.cpp
Last active August 29, 2015 14:24
$./a.out start forking 1.75534
#include <iostream>
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#define SIZE (40 * 1024 * 1024 * 1024L)
static inline double get_time_diff_double(struct timeval end, struct timeval start)
{
case class ListNode[+T](data: T, next: Option[ListNode[T]]) object List {
def newList[T](data: T) = newListFromNode(new ListNode(data, None))
def newListFromNode[T](node: ListNode[T]) = new List(node)
private def travse_iter[A](head:Option[ListNode[A]], fn: (ListNode[A]) => Unit): Unit = {
head match {
case Some(node) => {
fn(node)
List.travse_iter(node.next, fn)
@xudifsd
xudifsd / after-opt.clj
Created January 3, 2015 05:28
ACL checker after optimization
(defn self-is-active?
[{{:keys [uid]} :params :as req}]
(when-not (nil? uid)
(let [user-id (u/->int uid)
user (*get-user-by-id* user-id)]
(when-not (nil? user)
(not (:inactive user))))))
(defn self-belongs-to-team?
[{{:keys [uid subdomain]} :params :as req}]
@xudifsd
xudifsd / before-opt.clj
Created January 3, 2015 05:27
ACL checker before optimization
(defn self-is-active?
[{{:keys [uid]} :params :as req}]
(when-not (nil? uid)
(let [user-id (u/->int uid)
user (users/get-user-by-id user-id)]
(when-not (nil? user)
(not (:inactive user))))))
(defn self-belongs-to-team?
[{{:keys [uid subdomain]} :params :as req}]
@xudifsd
xudifsd / defasync.clj
Created December 20, 2014 06:27
defasync
(defmacro defasync [name args & body]
(if config/in-test?
`(defn ~name ~args ~@body)
(let [name-str (str name)]
`(defn ~name ~args
(future
(try
~@body
(catch Exception e#
(log/error (str ~name-str
@xudifsd
xudifsd / mileston.clj
Created June 6, 2014 02:22
we could use `:repeat` key and `<*` syntax in Typed Clojure to do useful annotation
; It's been a long time that Typed Clojure couldn't annotate `hash-map` using
; first class type. Because these functions require even number of arguments,
; and also have some special type constrain on its arguments.
; So we used to check these functions using special handler in checker.
; One of my GSOC goal is to make it possible to type check these kind of
; function without special handler.
; And now I made it. Thanks Sam Tobin-Hochstadt and Asumu Takikawa who inspired
; us.
; You can try it at my fork of Typed Clojure
; https://github.com/xudifsd/core.typed/tree/repeat-support
@xudifsd
xudifsd / T.java
Created December 27, 2013 06:09
annotation的使用
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.lang.NoSuchMethodException;
public class T {
public static void main(String[] args) throws Exception {
executeWithAnnotationSupport(T.class, "doSomething");
@xudifsd
xudifsd / ClassLoaderTest.java
Created December 14, 2013 08:17
class loader demo
import java.io.*;
public class ClassLoaderTest {
public static void main(String[] args) throws Exception {
ClassLoader myLoader = new ClassLoader() {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
try {
String fileName = "foo/" + name.replace('.', '/') + ".class";
System.out.println(fileName);