View fiber.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iterator> | |
#include <iostream> | |
#include <future> | |
#include <experimental/coroutine> | |
std::function<void()> resume; | |
template <typename _Ty, typename _Alloc = std::allocator<char>> | |
class future { | |
public: |
View lock.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val s = Array("A", "B", "C") | |
val lock = new java.util.concurrent.locks.ReentrantLock() | |
val total = s.length | |
var token: Int = 0 | |
val cvs = s.map(i => lock.newCondition()) | |
val threads = for (t <- 0 until 3) yield { | |
val thread = new Thread { | |
override def run { | |
lock.lock() |
View unfold.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function unfold(p, f, g, seed, reduce, tailgen) { | |
function recursive(iter) { | |
if (p(iter)) return tailgen(iter); | |
return reduce(f(iter), recursive(g(iter))); | |
} | |
return recursive(seed); | |
} | |
function sum(a) { |
View unfold.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function unfold(p, f, g, seed, reduce, tailgen) { | |
function recursive(iter) { | |
if (p(iter)) return tailgen(iter); | |
return reduce(f(iter), recursive(g(iter))); | |
} | |
return recursive(seed); | |
} |
View echo server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package info.kghost.test | |
import java.net.InetSocketAddress | |
import java.nio.channels.SelectionKey | |
import java.nio.channels.Selector | |
import java.nio.channels.ServerSocketChannel | |
import java.nio.channels.SocketChannel | |
import java.nio.ByteBuffer | |
import scala.collection.JavaConversions.collectionAsScalaIterable |
View gist:5706808
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# This file echoes a bunch of color codes to the | |
# terminal to demonstrate what's available. Each | |
# line is the color code of one forground color, | |
# out of 17 (default + 16 escapes), followed by a | |
# test use of that color on all nine background | |
# colors (default + 8 escapes). | |
# |
View cpu-time-profile.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#if defined(__i386__) | |
static __inline__ unsigned long long rdtsc(void) | |
{ | |
unsigned long long int x; | |
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x)); | |
return x; | |
} |
View gist:7065653
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// use dp to speed up | |
def Count(n: Int, m: Int) = { | |
def Loop(n: Int, m: Int, max: Int): Int = { | |
if (m == 0) { | |
if (n == 0) 1 else 0 | |
} else { | |
(0 to Math.min(max, n)).map({ x => Loop(n - x, m - 1, x) }).sum | |
} | |
} |
View gist:7064870
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def Conbination(n: Int, m: Int) { | |
def Loop(n: Int, m: Int, prefix: List[Int]) { | |
if (m == 0) | |
println(prefix) | |
else | |
for (x <- m to n) | |
Loop(x - 1, m - 1, x :: prefix) | |
} | |
Loop(n, m, Nil) | |
} |
View gist:7064763
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class BinaryTree(left: BinaryTree, right: BinaryTree) | |
def isFull(tree: BinaryTree): Boolean = { | |
def FullWithHeight(tree: BinaryTree): Int = { | |
if (tree == null) 0 else { | |
val l = FullWithHeight(tree.left) | |
val r = FullWithHeight(tree.right) | |
if (l >= 0 && l == r) l+1 else -1 | |
} | |
} |
NewerOlder