Skip to content

Instantly share code, notes, and snippets.

#ifndef OBJECT_H_
#define OBJECT_H_
#define OBJECT_INHERIT \
void *(**vtable)(); \
const char *name;
typedef struct object {
OBJECT_INHERIT
} OBJECT;
enum
@arianvp
arianvp / gist:3762231
Created September 21, 2012 15:37
Fast Fibonacci
fib :: (Num a, Integral a) => a -> Integer
fib = snd . fastfib
where fastfib :: (Num a, Integral a) => a -> (Integer, Integer)
fastfib 0 = (0, 0)
fastfib 1 = (0, 1)
fastfib n
| n `mod` 2 == 1 = ((a + c) * b, (c*c) + (b*b))
| otherwise = ((a*a) + (b*b), (a + c) * b)
where d = fastfib (n `div` 2)
a = fst d
7 main :: IO()
6 main = do
5
4 forM [1000000..5000000] $ \x -> do
3 a <- getCPUTime
2 return $! fib x
1 b <- getCPUTime
0 putStrLn $ show ((b-a) `div` 10^9)
===============================================================
20 fib :: (Num a, Integral a) => a -> Integer |@
19 fib = snd . fastfib |@
18 where fastfib :: (Num a, Integral a) => a -> (Integer, Integer) |@
17 fastfib 0 = (0, 0) |@
16 fastfib 1 = (0, 1) |@
15 fastfib n |@
14 | n `mod` 2 == 1 = ((a + c) * b, (c*c) + (b*b)) |@
13 | otherwise = ((a*a) + (b*b), (a + c) * b) |@
12 where d = fastfib (n `div` 2) |@
11 a = fst d |@
0 _ = require 'underscore'
1 TRUE = (a,b) -> a
2 FALSE = (a,b) -> b
3 NAND = (a,b) -> a(b,a)(FALSE, TRUE)
4 NOT = (a) -> NAND(a,a)
5 AND = (a,b) -> NOT(NAND(a,b))
6 OR = (a,b) -> NAND(NOT(a),NOT(b))
7 NOR = (a,b) -> NOT(OR(a,b))
8 XOR = (a,b) -> NOR(AND(a,b),NOR(a,b))
9 XNOR = (a,b) -> NOT(XOR(a,b))
<script>
$(function() {
$('.item').bind('touchstart', function (e) {
$(e.srcElement).addClass('touched');
});
$('.item').bind('touchmove', function (e) {
$(e.srcElement).removeClass('touched');
});
$('.item').bind('touchend', function (e) {
$(e.srcElement).removeClass('touched');
<script>
$(function() {
$('.item').bind('touchstart', function (e) {
$(e.srcElement).addClass('touched');
});
$('.item').bind('touchmove', function (e) {
$(e.srcElement).removeClass('touched');
});
$('.item').bind('touchend', function (e) {
$(e.srcElement).removeClass('touched');
#include "server.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <fcntl.h>
static void io_cb(struct ev_loop *loop, struct ev_io *watcher, int events)
{
if (events & EV_WRITE)
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.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
void accept_cb(EV_P_ ev_io *io, int revents)
{
int client_fd;
socklen_t len;
struct sockaddr_in client_addr;
len = sizeof(client_addr);
client_fd = accept(io->fd, (struct sockaddr *)&client_addr, &len);
fcntl(client_fd, F_SETFL, O_NONBLOCK);