Skip to content

Instantly share code, notes, and snippets.

View philsquared's full-sized avatar

Phil Nash philsquared

View GitHub Profile
@philsquared
philsquared / C++ Extension Methods
Created April 11, 2013 23:45
How to write the "left arrow operator" to enable extension methods in C++
#include <cassert>
#include <iostream>
template<typename T, typename R=void>
struct ExtMethod {
ExtMethod& operator - () {
return *this;
}
template<typename U>
struct MaxUsage { int memory, disk; };
TEST_CASE( "KeyValueBuffer disk memory usage" )
{
using namespace Catch::Generators;
MaxUsage params[] = {
{ 1, 2 },
{ 1, 1024 },
{ 8, 1024 },
// Define an optional chaining pipeline operator
operator infix |> { associativity left }
func |> <T, U>(t: T?, f: (T) -> U? ) -> U? {
if let t1 = t { return f( t1 ) }
else { return nil }
}
// some dummy classes and functions to chain together
class C { let x = 7 }
class B { }
@philsquared
philsquared / Result type usage
Created January 29, 2015 13:48
Usage example for Result<T> in Swift
var key : String?
let res = getPathComponents( file )
>>= { (let components ) -> Result<FileTypes> in
key = components.filenameWithoutExt
return FileTypes.fromExt( components.ext )
}
>>= { (let type ) -> Result<Void> in
switch type {
case .Video, .Image:
var caption : String?
@philsquared
philsquared / if let chain
Created February 11, 2015 17:39
if let chaining
var a : Int? = 11
var b : Int? = 20
if let a = a,
let b = b,
let c = { () -> Int? in
return a > 10 ? a+b : nil
} () {
println( "c: \(c)" )
}
extension Sequence {
func countIf( predicate: (Self.Iterator.Element) -> Bool ) -> Int {
return reduce( 0 ) { predicate($1) ? $0+1 : $0 }
}
}
#include <experimental/optional>
#include <string>
#include <iostream>
using std::experimental::optional;
template<typename T>
struct add_optionality { using type = optional<T>; };
template<typename T>
#include "clara.hpp"
#include <iostream>
int main( int argc, char** argv ) {
int count = 0;
using namespace clara;
auto cli = Parser() | Opt( [&]( bool ) { count++; } )
["-s"];
cli.parse( Args{ argc, argv } );
std::cout << "Repeated: " << count << ", effective " << std::boolalpha << (count%2==1) << " (Command line was:";
class SuccessFrom {
const int rc;
public:
SuccessFrom( int rc ) : rc(rc) {}
explicit operator bool() const {
return rc == SUCCESS;
}
friend std::ostream& operator<<( std::ostream& os, SuccessFrom const& result ) {
return os << mdb_strerror(result.rc);
@philsquared
philsquared / rewindtests.cpp
Created January 14, 2019 10:09
Kellabyte's rewind tests using flattened REQUIRESs (depends on https://gist.github.com/philsquared/567fdd233d574fe1957d17aff3e5d02c)
TEST_CASE("initialize rewind", "[rewind]") {
int rc;
MDB_env* env;
MDB_dbi dbi;
MDB_txn *txn;
REQUIRE( SuccessFrom( mdb_env_create(&env) ) );
REQUIRE( SuccessFrom( mdb_env_set_mapsize(env, size_t(1048576000) ) ) );
REQUIRE( SuccessFrom( mdb_env_set_maxdbs(env, 1024) ) );
REQUIRE( SuccessFrom( mdb_env_open(env, "/tmp", MDB_FIXEDMAP /*|MDB_NOSYNC*/, 0664) ) );