Skip to content

Instantly share code, notes, and snippets.

🔆
daily grind

Martin Moene martinmoene

🔆
daily grind
Block or report user

Report or block martinmoene

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@martinmoene
martinmoene / gist:9091556
Created Feb 19, 2014
element_or(), container_element_or()
View gist:9091556
// 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:9410391
Created Mar 7, 2014
std::between() an idea by Niels Dekker
View gist:9410391
// std::between() an idea by Niels Dekker
#include <functional>
namespace std {
template<class T>
bool between(T const& val, T const& lo, T const& hi)
{
return between( val, lo, hi, std::less<T>() );
View gist:9485071
// [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
{
View lest_sections.cpp
// 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 / Understand-C++
Last active Aug 29, 2015
Myth 1: “To understand C++, you must first learn C” -- performance aspect
View Understand-C++
// https://isocpp.org/blog/2014/12/myths-1
//
// Bjarne Stroustrup.
//
// 2. Myth 1: “To understand C++, you must first learn C”
//
// No. Learning basic programming using C++ is far easier than with C.
//
// C is almost a subset of C++, but it is not the best subset to learn first
// because C lacks the notational support, the type safety, and the easier-to-use
@martinmoene
martinmoene / lest-module-1.cpp
Created Dec 26, 2014
Spread lest tests over multiple files
View lest-module-1.cpp
// 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 Aug 29, 2015
Replacing a loop (compute_loop()) with an std::algorithm.
View loop_vs_algorithm.cpp
// 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 Aug 29, 2015
FizzBuzz GUI
View main.cpp
#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 Aug 23, 2015
C++ example of proposed clamp() used with range-v3 library.
View clamp-range-v3.cpp
// 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 Sep 15, 2015
Simple running average (mean and median) of samples in C++.
View main.cpp
//
// simple running average (mean and median) of samples.
//
#include <algorithm>
#include <cassert>
#include <numeric>
#include <vector>
#ifndef RUNLENGTH
You can’t perform that action at this time.