Skip to content

Instantly share code, notes, and snippets.

@jankjn
jankjn / Y-combinator.clj
Created October 21, 2018 19:10
y combinator try out
(defn Y [f]
(#(% %)
(fn [x] #(apply (f (x x)) %&))))
(def fib-gen
(fn [f]
(fn [n]
(if (= 0 n) 1 (* n (f (dec n)))))))
((Y fib-gen) 5) ;; => 120
@jankjn
jankjn / NIOServer.java
Last active March 15, 2017 03:31
Java simple nio socket server
import java.io.IOException;
import java.net.InetSocketAddress;
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.Objects;
import java.util.Set;
import java.util.HashMap;
public class LRUCache {
private class Node {
Node prev, next;
int key, value;
Node (int key, int value) {
this.key = key;
this.value = value;
}
@jankjn
jankjn / ThreadPool.java
Last active June 17, 2016 11:05
Java simple thread pool
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
/**
* java线程池
* v1: 使用同步list 保存任务,synchronized wait notify实现任务分发
* v2: 使用BlockingQueue 保存任务及任务分发
* v3*: 前两种方式使用定时轮询检查flag来退出线程,v3 使用向任务队列尾部添加修改flag的任务的方式
* Created by Ezio on 6/17/2016.
*/