Skip to content

Instantly share code, notes, and snippets.

View virtualsafety's full-sized avatar

virtualsafety virtualsafety

View GitHub Profile
@virtualsafety
virtualsafety / libevent_test.c
Created January 29, 2017 08:49 — forked from mythosil/libevent_test.c
libevent sample (echo server)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <event.h>
#define PORT 1986
#define BACKLOG -1
#define BUFSIZE 256

Advanced Functional Programming with Scala - Notes

Copyright © 2017 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x
@virtualsafety
virtualsafety / Tree.scala
Created July 27, 2016 08:46 — forked from dholbrook/Tree.scala
Scala binary tree
/**
* D Holbrook
*
* Code Club: PO1
*
* (*) Define a binary tree data structure and related fundamental operations.
*
* Use whichever language features are the best fit (this will depend on the language you have selected). The following operations should be supported:
*
* Constructors
@virtualsafety
virtualsafety / c99.l
Created July 26, 2016 03:00 — forked from codebrainz/c99.l
C99 Lex/Flex & YACC/Bison Grammars
D [0-9]
L [a-zA-Z_]
H [a-fA-F0-9]
E ([Ee][+-]?{D}+)
P ([Pp][+-]?{D}+)
FS (f|F|l|L)
IS ((u|U)|(u|U)?(l|L|ll|LL)|(l|L|ll|LL)(u|U))
%{
#include <stdio.h>
@virtualsafety
virtualsafety / Queue_LinkedList.c
Created July 23, 2016 04:05 — forked from mycodeschool/Queue_LinkedList.c
Linked List implementation of Queue.
/*Queue - Linked List implementation*/
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Two glboal variables to store address of front and rear nodes.
struct Node* front = NULL;
struct Node* rear = NULL;
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE StandaloneDeriving #-}
module RedBlackTree where
data Zero
data Succ n
type One = Succ Zero
data Black
import Data.Maybe (isJust)
data Tree a = Leaf | Node (Tree a) a (Tree a)
replace :: a -> Tree a -> Tree a
replace a Leaf = Node Leaf a Leaf
replace a (Node l _ r) = Node l a r
rotateR :: Tree a -> Tree a
rotateR (Node (Node x a y) b z) = Node x a (Node y b z)
module AVLTree where
data BT = L | N Int BT BT deriving (Show, Eq)
-- Nice, small and useful functions
empty = L
-- You could say, depth L == Nothing depth (N v L L) == Just 0, but works for
-- me better this way:
depth L = 0
depth (N _ t u) = (max (depth t) (depth u)) + 1
module Main where
import Text.Parsec
import Text.Parsec.String
input_text :: String
input_text = "foo123:"
main :: IO ()
main = do
@virtualsafety
virtualsafety / Fibonacci_campare.txt
Last active December 23, 2015 11:59
Fibonacci数列两种实现方式效率比较
keliven@keliven:~/haskell_example$ cat fib_3.hs
import System.Environment
fib = 0:1:(zipWith (+) fib (tail fib))
main = do
args <- getArgs
print $ (fib !!) $ read $ args !! 0