Skip to content

Instantly share code, notes, and snippets.

View elbeno's full-sized avatar

Ben Deane elbeno

View GitHub Profile
@elbeno
elbeno / knights_knaves.cpp
Last active August 29, 2015 14:22
Knights & Knaves solved with the list monad in C++
#include <iostream>
#include <iterator>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
//------------------------------------------------------------------------------
// The "many-worlds" method of solving problems with the list monad
@elbeno
elbeno / fizzbuzz.cpp
Created January 6, 2016 05:06
FizzBuzz in C++14
#include <cstddef>
#include <iostream>
#include <string>
#include <utility>
using namespace std;
template <bool div3, bool div5>
struct printer
{
@elbeno
elbeno / auto_pitfall.cpp
Last active February 16, 2016 03:58
C++ auto may cause unwanted copies with getters
#include <iostream>
#include <string>
#include <type_traits>
using namespace std;
struct Foo
{
Foo() {}
Foo(const Foo&)
@elbeno
elbeno / forward_tuple.cpp
Last active March 5, 2016 18:56
Forwarding to a member function using a tuple
#include <cstddef>
#include <iostream>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
using namespace std;
template <typename T>
@elbeno
elbeno / enum_class_abuse.cpp
Last active November 28, 2016 10:48
Treating enum class as flags or iterable
#include <cstdint>
#include <iostream>
#include <iterator>
#include <type_traits>
using namespace std;
// -----------------------------------------------------------------------------
// Traits for treating enums like flags, and/or giving the ability to loop over
// them
@elbeno
elbeno / element_get.h
Last active November 4, 2016 01:58
Tersely copy keys from a map
#include <cstddef>
#include <tuple>
#include <utility>
template <size_t I, typename T>
struct element_get_helper;
template <size_t I, typename... Ts>
struct element_get_helper<I, std::tuple<Ts...>>
{
@elbeno
elbeno / inplace_merge.cpp
Last active November 25, 2021 03:04
A sketch of inplace_merge
#include <cassert>
#include <iterator>
#include <memory>
#include <utility>
template <typename ForwardIt>
void my_inplace_merge(ForwardIt first, ForwardIt middle, ForwardIt last)
{
if (first == middle || middle == last) return;
@elbeno
elbeno / curry_howard_silliness.cpp
Created April 2, 2016 08:00
Curry-Howard silliness
#include <functional>
#include <iostream>
#include <string>
#include <tuple>
#include <type_traits>
using namespace std;
// -----------------------------------------------------------------------------
// A silly (in C++) application of the Curry-Howard isomorphism to produce the
@elbeno
elbeno / delimited_algos.cpp
Last active April 21, 2016 21:50
Algorithms for dealing with delimited strings
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <utility>
using namespace std;
template <typename FwdIt1, typename FwdIt2, typename T>
std::pair<FwdIt1, FwdIt1> find_delimited(
@elbeno
elbeno / numbers.cpp
Last active May 3, 2016 05:58
Unfold formulations
#include <algorithm>
#include <cassert>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <sstream>
#include <string>
#include <utility>
#include <vector>