Skip to content

Instantly share code, notes, and snippets.

View philsquared's full-sized avatar

Phil Nash philsquared

View GitHub Profile
@philsquared
philsquared / SquarePeg.rs
Last active May 5, 2021 10:54
Fitting a SquarePeg into a RoundHole, in Rust
trait Square {
fn size(&self) -> f64;
fn perimeter(&self) -> f64;
}
trait Circle {
fn radius(&self) -> f64;
fn perimeter(&self) -> f64;
}
@philsquared
philsquared / rewind-sections.cpp
Created January 14, 2019 10:09
Kellabyte's rewind tests using plain SECTIONs (depends on https://gist.github.com/philsquared/567fdd233d574fe1957d17aff3e5d02c)
TEST_CASE("initialize rewind", "[rewind]") {
int rc;
MDB_env *env;
MDB_dbi dbi;
MDB_txn *txn;
SECTION("create and open an environment") {
REQUIRE(SuccessFrom(mdb_env_create(&env)));
REQUIRE(SuccessFrom(mdb_env_set_mapsize(env, size_t(1048576000))));
SCENARIO("initialize rewind", "[rewind]") {
GIVEN( "an uninitialised environment") {
int rc;
MDB_env *env;
MDB_dbi dbi;
MDB_txn *txn;
WHEN("an environment is created and opened") {
REQUIRE(SuccessFrom(mdb_env_create(&env)));
REQUIRE(SuccessFrom(mdb_env_set_mapsize(env, size_t(1048576000))));
@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) ) );
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);
#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:";
#include <experimental/optional>
#include <string>
#include <iostream>
using std::experimental::optional;
template<typename T>
struct add_optionality { using type = optional<T>; };
template<typename T>
extension Sequence {
func countIf( predicate: (Self.Iterator.Element) -> Bool ) -> Int {
return reduce( 0 ) { predicate($1) ? $0+1 : $0 }
}
}
@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)" )
}
@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?