This file contains hidden or 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
import java.util.concurrent.atomic.AtomicReference; | |
public class LazyReference{ | |
abstract static class LazyRef<T> { | |
// BUG1: should be final | |
// member start with _ | |
private final AtomicReference<T> _ref = new AtomicReference<T>(); | |
public LazyRef() |
This file contains hidden or 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 <iostream> | |
using namespace std; | |
// permutate recusively - use a bitmap, index as the permutate value. | |
// If the target to permutation value doesn't fit in a bitmap, just use a hashmap. | |
void permutate() | |
{ | |
static int a[] = {0, 0, 0, 0}; | |
static int o[4]; |
This file contains hidden or 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 <iostream> | |
#include <vector> | |
using namespace std; | |
vector<int> concat(const vector<int>& v1, const vector<int>& v2, const vector<int>& v3) | |
{ | |
vector<int> res = v1; | |
res.insert(res.end(), v2.begin(), v2.end()); |
This file contains hidden or 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 "stdafx.h" | |
#include <iostream> | |
using namespace std; | |
template <class T> | |
class SmartPtr | |
{ | |
T* p_; | |
int* count_; |
This file contains hidden or 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 <iostream> | |
using namespace std; | |
// Use 2 language features: | |
// 1. Derived classes through private inheritance can access base class's protected member, but make it private to its sub classes | |
// 2. Virtual inheritance will cause the final derived class to call the virtual bases's constructor directly | |
// so the sealed class has no problem to create instance while its sub class can't create instance. | |
// This is also another use of CRTP (curious recurring template pattern) | |
template <class T> | |
class SealedBase |
This file contains hidden or 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
# | |
# This script is to create startup code for different languages | |
# so you can quickly start your programming. | |
# | |
use strict; | |
use warnings; | |
use Getopt::Long; | |
sub help() |
This file contains hidden or 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 <iostream> | |
#include <functional> | |
int main() | |
{ | |
std::function<void(int)> a[2]; | |
// lambda: [&capture](parameters)->return_type {body} | |
a[0] = [&](int i) {std::cout << i << std::endl; a[i/1000](i+1);}; | |
a[1] = [](int i) {return;}; | |
a[0](1); |
This file contains hidden or 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
sub make_adder | |
{ | |
my $addpiece = shift; | |
return sub { shift + $addpiece }; | |
} | |
$f1 = make_adder(20); | |
&f1(1); # = 21; | |
&f1(2); # = 22; |
This file contains hidden or 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
using System; | |
using System.ComponentModel; | |
using System.Diagnostics; | |
namespace timeit | |
{ | |
class Program | |
{ | |
static int Main(string[] args) | |
{ |
This file contains hidden or 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
template <typename T> | |
T max(const T& a, const T& b) | |
{ | |
return a > b ? a : b; | |
} |