Skip to content

Instantly share code, notes, and snippets.

View martinmoene's full-sized avatar

Martin Moene martinmoene

View GitHub Profile
@martinmoene
martinmoene / gist:9091556
Created February 19, 2014 13:04
element_or(), container_element_or()
// element_or()
// container_element_or()
#include <map>
#include <vector>
/**
* return element of vector if present, def otherwise.
*/
template< typename E, typename I, typename D >
@martinmoene
martinmoene / gist:9485071
Last active August 29, 2015 13:57
with scoped
// [std-proposals] Using the underscore for unused identifiers
// https://groups.google.com/a/isocpp.org/d/msg/std-proposals/a4CRu2KONZ8/N0aPF76B990J
#include <iostream>
// _, or ANON
#define with( expr ) if ( auto&& _ = (expr) )
struct wither
{
// lest light-weight sections
// [1] Catch: Test cases and sections,
// https://github.com/philsquared/Catch/blob/develop/docs/tutorial.md#test-cases-and-sections
// [2] lest – lest errors escape testing,
// https://github.com/martinmoene/lest#lest--lest-errors-escape-testing---10
#include <cassert>
#include <iostream>
@martinmoene
martinmoene / lest-module-1.cpp
Created December 26, 2014 11:16
Spread lest tests over multiple files
// C++11 - use multiple source files.
#include "lest.hpp"
lest::tests & specifications()
{
static lest::tests tests;
return tests;
}
@martinmoene
martinmoene / loop_vs_algorithm.cpp
Last active August 29, 2015 14:21
Replacing a loop (compute_loop()) with an std::algorithm.
// replacing a loop (compute_loop()) with an std::algorithm.
#include <algorithm>
#include <functional>
#include <vector>
using Coll = std::vector<int>;
int compute_loop( Coll a, Coll b )
{
@martinmoene
martinmoene / main.cpp
Last active August 29, 2015 14:22
FizzBuzz GUI
#include <iostream>
#include <string>
#define sometimes if
bool is( std::string text )
{
return ! text.empty();
}
std::string number( int x )
@martinmoene
martinmoene / clamp-range-v3.cpp
Created August 23, 2015 19:57
C++ example of proposed clamp() used with range-v3 library.
// clamp-range-v3
#include <iostream>
#include <clamp.hpp>
#include <range/v3/core.hpp>
#include <range/v3/view/iota.hpp>
#include <range/v3/view/remove_if.hpp>
#include <range/v3/view/transform.hpp>
@martinmoene
martinmoene / main.cpp
Last active September 15, 2015 05:27
Simple running average (mean and median) of samples in C++.
//
// simple running average (mean and median) of samples.
//
#include <algorithm>
#include <cassert>
#include <numeric>
#include <vector>
#ifndef RUNLENGTH
@martinmoene
martinmoene / TestOpAssign.cpp
Created September 29, 2012 22:05
Performance test of X& operator=(X)
/*
* TestOpAssign.cpp
*
* Created by Martin on 28 September 2012.
* Copyright 2012 Universiteit Leiden. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
@martinmoene
martinmoene / TestOpAssign_Niels_Martin.cpp
Created October 2, 2012 12:38
Performance test of X& operator=(X?)
/*
* TestOpAssign_Niels_Martin.cpp
*
* Created by Martin on 28 September 2012.
* Adapted by Niels Dekker, 1 Oct 2012
* Copyright 2012 Universiteit Leiden. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/