Skip to content

Instantly share code, notes, and snippets.

View kylefeng's full-sized avatar

Kyle Feng kylefeng

  • Guiyang, Guizhou, China
View GitHub Profile
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"sync"
)
@kylefeng
kylefeng / prototype_chain.js
Last active December 31, 2015 00:09
prototype chain
function Animal(name) {
this.name = name;
}
Animal.prototype = {
weight: 0,
eat: function() {
console.log("Animal is eating!");
}
}
(defn gen-uniq-rand-seq
[n gen-elem-fn]
(loop [xs (java.util.ArrayList.) x (gen-elem-fn)]
(cond
(= n (count xs)) xs
(some #(= % x) xs) (recur xs (gen-elem-fn))
:else (do
(.add xs x)
(recur xs (gen-elem-fn))))))
@kylefeng
kylefeng / PlainNioEchoServer.java
Created November 26, 2013 03:46
NIO Echo Server Demo
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
#include <stdio.h>
#include <string.h>
int main(void) {
char pw[8] = "pass";
char in[8];
while (1) {
scanf("%s", in);
if (strcmp(in, pw) == 0) {
printf("yes\n");
@kylefeng
kylefeng / ClhSpinLock.java
Created October 10, 2013 08:02
A simple CLH spin lock implementation.
public class ClhSpinLock {
private final ThreadLocal<Node> pred;
private final ThreadLocal<Node> node;
private final AtomicReference<Node> tail = new AtomicReference<Node>(new Node());
public ClhSpinLock() {
this.node = new ThreadLocal<Node>() {
protected Node initialValue() {
return new Node();
}
(import '[java.io PushbackReader])
(defn make-name [f]
(str "memo/" (clojure.string/replace (str f) #" " "") "-memo-function.clj"))
(defn- open-memo-file [name]
(let [name (make-name name)]
(try
(with-open [r (clojure.java.io/reader name)]
(read (PushbackReader. r)))
import itertools
import functools
def min(x):
fn = lambda lst: int(''.join(map(str, lst)))
return fn(functools.reduce(lambda x,y: fn(x) < fn(y) and x or y, itertools.permutations(x)))
print(min([9,12,34]))
(for [col (partition-by #(> (second %) 0) (map-indexed vector lst))
:when (every? #(> (second %) 0) col)]
[(count col) [(first (first col)) (first (last col))]])
;user> ([3 [3 5]] [8 [11 18]] [7 [24 30]] [6 [36 41]])