Skip to content

Instantly share code, notes, and snippets.

package gc.demo
import com.typesafe.scalalogging.LazyLogging
import zio._
import zio.stream._
object Zio2App extends App with LazyLogging {
override def run(args: List[String]): ZIO[ZEnv, Nothing, Int] = {
function createMultimethod(dispatch, noMatch) {
if(typeof dispatch !== 'function') {
throw new TypeError('dispatch must be a function');
}
const dict = {};
if(typeof noMatch == 'function') {
dict.noMatch = noMatch;
}
@gchudnov
gchudnov / inline_visitor_builder.cpp
Created December 18, 2015 15:45
inline_visitor_builder
#include <iostream>
#include <vector>
// inline visitor
struct triangle;
struct square;
struct figure_visitor {
virtual ~figure_visitor() = default;
virtual void visit(const triangle&) {}
@gchudnov
gchudnov / variant_visitor_builder.cpp
Created December 14, 2015 15:28
variant visitor
#include <iostream>
#include <string>
#include <type_traits>
#include <typeinfo>
#include <tuple>
#include <functional>
template <typename T>
struct function_traits
@gchudnov
gchudnov / cpp_format_str.cpp
Last active February 12, 2018 03:42
C++ variadic formatter using boost::format
#include <iostream>
#include <boost/format.hpp>
inline std::string format_str_recursive(boost::format& message) {
return message.str();
}
template<typename TValue, typename... TArgs>
std::string format_str_recursive(boost::format& message, TValue&& arg, TArgs&&... args) {
message % std::forward<TValue>(arg);
@gchudnov
gchudnov / cpp_call_once.cpp
Created November 6, 2014 19:39
Lazy (call once)
#include <iostream>
#include <memory>
#include <functional>
#include <mutex>
template <typename T>
struct lazy {
public:
template <typename Fun>
explicit lazy(Fun&& fun) : fun(std::forward<Fun>(fun)) {}
@gchudnov
gchudnov / cpp_read_file.cpp
Created November 6, 2014 19:36
Read a text file
#include <iostream>
#include <string>
#include <fstream>
int main() {
std::string filepath = "data.txt";
std::ifstream ifs(filepath.c_str(), std::ios_base::in | std::ios_base::binary);
if (ifs) {
@gchudnov
gchudnov / cpp_utf8_utf16.cpp
Created November 6, 2014 19:33
C++ string conversion UTF8 <-> UTF16
#include <string>
#include <locale>
#include <codecvt>
//UTF-8 to UTF-16
std::string source;
//...
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
std::u16string dest = convert.from_bytes(source);
@gchudnov
gchudnov / cpp_win32_com_exceptions.cpp
Created November 6, 2014 19:30
Win32 COM exception handling
#include <iostream>
int main() {
try {
// ...
} catch(_com_error &e) {
_bstr_t source(e.Source());
@gchudnov
gchudnov / cpp_elapsed_time.cpp
Created November 6, 2014 19:26
Measure elapsed time
#include <iostream>
#include <chrono>
int main() {
auto t0(std::chrono::steady_clock::now());
// some code to measure time for
auto t1(std::chrono::steady_clock::now());