Skip to content

Instantly share code, notes, and snippets.

View 2bbb's full-sized avatar

2bit 2bbb

View GitHub Profile
@2bbb
2bbb / inline_variable.cpp
Created February 21, 2018 13:51
inline_variable
#include <iostream>
template <typename type, typename tag = type>
struct inline_variable {
static type &get(const type &initial_value) {
static type _{initial_value};
return _;
}
};
@2bbb
2bbb / build.sh
Created February 21, 2018 05:06
duplicate
#!/bin/bash
clang++ -Wall impl.cpp sub.cpp -o impl.a -std=c++11
./impl.a
@2bbb
2bbb / autodiff.hpp
Last active January 16, 2018 09:01
autodiff
#include <cmath>
namespace bbb {
namespace autodiff {
template <typename type>
struct dual {
inline static dual make(type x) { return dual(x); };
inline static dual scalar(type x) { return dual(x, 0.0); };
constexpr dual(type x, type dx = type(1.0)) noexcept
@2bbb
2bbb / sheep.scpt
Last active October 23, 2017 21:43
sleep sheep (羊の数を数えてくれるApple Script)
set i to 1
repeat while true
say {"羊が", i, "匹"} as string using "Kyoko"
set i to i + 1
delay 1
end repeat
@2bbb
2bbb / brainfuck.exs
Last active October 26, 2017 17:26
brainfuck.exs
defmodule Brainfuck do
@seek_next ?>
@seek_prev ?<
@inc_head ?+
@dec_head ?-
@print_head ?.
@read_input ?,
@loop_begin ?[
@loop_end ?]
@2bbb
2bbb / function_check.cpp
Created September 28, 2017 08:22
check function exist
#include <iostream>
int f(int x) {
std::cout << "true f" << std::endl;
return 0;
}
namespace bbb {
struct function_is_not_exist {};
function_is_not_exist f(...) { return {}; };
@2bbb
2bbb / build.sh
Created July 26, 2017 17:03
variable_inliner
#!/bin/bash
clang++ -c -std=c++11 -stdlib=libc++ main.cpp
clang++ -c -std=c++11 -stdlib=libc++ sub.cpp
clang++ main.o sub.o -o main
@2bbb
2bbb / polyfill.hpp
Created June 16, 2017 13:53
polyfill
namespace bbb {
template <typename type>
struct is_null_pointer : std::false_type {};
template <typename type>
using is_null_pointer_t : get_type<is_null_pointer<type>>;
template <>
struct is_null_pointer<std::nullptr_t> : std::true_type {};
template <typename ...>
@2bbb
2bbb / pipe_ex.cpp
Created June 16, 2017 04:20
pipe example
#include <bbb/pipe.hpp>
#include <iostream>
int main(int argc, char *argv[]) {
std::string str = "a/b/cdcd/ezzzf/ghi/azzz/czzzd/ef";
auto res = str
| bbb::replace("zzz", "Z") // "a/b/cdcd/eZf/ghi/aZ/cZd/ef"
| bbb::split("/") // {"a", "b", "cdcd", "eZf", "ghi", "aZ", "cZd", "ef"}
| bbb::filter([](std::string str) { return str.length() < 3; }) // {"a", "b", "aZ", "ef"}
@2bbb
2bbb / overloadable_function.js
Last active June 10, 2017 17:40
overloadable_function
function fun(org = function(...args) { throw new Error("overload failed", args.map(a => typeof a)); }, ... others) {
const overload_functions = {};
if(others.length) {
overload_functions[others.map(a => a.constructor.name)] = org;
org = function(...args) { throw new Error("overload failed", args.map(a => typeof a)); };
}
const f = function(... args) {
return ((overload_functions[args.map(a => a.constructor.name)] || org).bind(this))(... args);
}
f.overload = function(overloading_function, ...args) {