Skip to content

Instantly share code, notes, and snippets.

View elbeno's full-sized avatar

Ben Deane elbeno

View GitHub Profile
@elbeno
elbeno / main.hs
Last active August 29, 2015 14:20
Writer monad idea, with foldable things instead of monoids
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
module Main where
-- Here's the familiar Writer monad. For the purposes here, we're not worried
-- about the instances for functor, monad etc.
newtype Writer a x = Writer { runWriter :: (x, a) }
@elbeno
elbeno / keyed.cpp
Last active June 10, 2018 23:33
Keyed functions in C++
#include <memory>
#include <type_traits>
// Foo is a class that has a private constructor, and it's created by a factory
// function that returns a smart pointer.
class Foo
{
public:
static std::unique_ptr<Foo> Create();
@elbeno
elbeno / fix.cpp
Created May 16, 2015 21:58
Fixed-point (Y) combinator in C++
#include <functional>
#include <iostream>
using namespace std;
template <typename F>
struct Y
{
Y(F f) : m_f(f) {}
template <typename T>
@elbeno
elbeno / fibonacci.cpp
Last active May 19, 2020 03:55
O(log n) fibonacci numbers
#include <utility>
// The 2x2 fibonacci matrix
//
// { F(n+1) F(n) }
// { F(n) F(n-1) }
//
// can be represented as just the bottom row, since the top row can be computed:
// so just use a pair.
template <typename N>
@elbeno
elbeno / visit_at.cpp
Created May 16, 2015 23:22
Runtime visitation of a tuple in C++
#include <algorithm>
#include <cassert>
#include <tuple>
#include <utility>
namespace detail
{
template <size_t N>
struct visit_impl
{
@elbeno
elbeno / for_each_args.cpp
Created May 17, 2015 00:21
Functions that work over their arguments
//------------------------------------------------------------------------------
// A function that will apply a function to each argument
#include <initializer_list>
#include <utility>
template <typename F, typename... Ts>
void for_each_arg(F&& f, Ts&&... ts)
{
using I = std::initializer_list<int>;
(void) I { (std::forward<F>(f)(std::forward<Ts>(ts)), 0)... };
@elbeno
elbeno / with_macro.cpp
Last active August 29, 2015 14:21
WITH macro for C++
#include <algorithm>
#include <cassert>
#include <utility>
#include <iostream>
using namespace std;
class Light
{
public:
void turn_on() { m_on = true; };
@elbeno
elbeno / constexpr_lambda.cpp
Created May 19, 2015 01:05
A macro for constexpr lambdas in C++
#include <iostream>
#include <type_traits>
#include <utility>
template<class F>
struct wrapper
{
static_assert(std::is_empty<F>(), "Lambdas must be empty");
template<class... Ts>
decltype(auto) operator()(Ts&&... xs) const
@elbeno
elbeno / large_tuples.cpp
Last active August 29, 2015 14:21
Quick compilation of large tuples
#include <iostream>
#include <tuple>
using namespace std;
// When tuples get large, compile times suffer because the compiler is hashing
// the name of a really long type. Lambdas offer a way to truncate that type
// name, leading to faster compile times and the ability to instantiate larger
// tuples (the template depth quickly exceeds the max).
// Compare compile times:
@elbeno
elbeno / constexpr_hash.cpp
Last active August 29, 2015 14:21
constexpr __FILE__ hashing
#include <iostream>
using namespace std;
constexpr unsigned long long fnv_hash64_r(unsigned long long result, const char* string)
{
return (*string == 0) ? result : fnv_hash64_r(1099511628211ULL * result ^ *string, string + 1);
}
constexpr unsigned long long fnv_hash64(const char* string)
{