Skip to content

Instantly share code, notes, and snippets.

@ivcn
ivcn / print.cpp
Created April 6, 2017 13:43
How to send several values to stream until C++17
template<typename... Args>
void print(Args&&... args) {
using dummy = int[];
(void)dummy{0, (void(cout << std::forward<Args>(args)), 0)...};
cout << endl;
}
template<typename T, size_t n>
void copy_array(T (&src)[n], T (&dst)[n])
{
for (size_t i = 0; i < n; i++)
{
dst[i] = src[i];
}
}
int main()
@ivcn
ivcn / loop.js
Created March 5, 2017 21:36
Loop with timeout in JavaScript
var i = 0;
function loop () {
setTimeout(function () {
doSomething();
i++;
if (i < 20) {
loop();
}
}, 300)
}
@ivcn
ivcn / spawn.cpp
Created February 21, 2017 23:23
An example of asynchronous launching of some functions
template<typename F,typename A>
std::future<std::result_of<F(A&&)>::type>
spawn(F&& f,A&& a)
{
typedef std::result_of<F(A&&)>::type result_type;
std::packaged_task<result_type(A&&)>
task(std::move(f)));
std::future<result_type> res(task.get_future());
std::thread t(std::move(task),std::move(a));
t.detach();
'''
Consider next number sequence
F(1) = 1
F(2) = 2
F(3) = 3
F(n) = 1*F(n-1) + 2*F(n-2) + 3*F(n-3)
Here we are interested only in parity of numbers.
'''
@ivcn
ivcn / TestRandom.cpp
Last active February 17, 2017 12:14
Implementation of some statistical distributions using C++11 <chrono> and <random> libraries
#include <chrono>
#include <iostream>
#include <random>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
class TestRandom {