Skip to content

Instantly share code, notes, and snippets.

@madmongo1
Created September 25, 2021 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madmongo1/17d2e2ba784ca4d275ace416a426cb32 to your computer and use it in GitHub Desktop.
Save madmongo1/17d2e2ba784ca4d275ace416a426cb32 to your computer and use it in GitHub Desktop.
struct invariants;
struct invariants_builder
{
void set(std::string const& index, std::any item);
invariants commit();
};
/// A map of type -> any.
///
/// Maps are immutable, however, it is possible to create a shallow copy and
/// mutate/add elemeents in the copy. The added/mutated elements shall exist
/// only in the copy. All other elements are available in the copy, via the
/// parent
struct invariants
{
invariants set(std::string const& index, std::any item) const;
invariants_builder bulk_update() const;
template<class T>
T const*
query(std::string const& index) const;
template<class T>
T const&
require(std::string const& index) const;
};
void
test()
{
invariants i1; //empty map
auto i1 = i1.set("base_currency", "BTC");
auto i2 = i1.set("term_currency", "USD");
// i2 contains "base_currency" and "term_currency"
// i1 contains only "base_currency", which is shared with i2
// now perform a bulk update
auto updated = i2.bulk_update();
updated.set("markup", 0.001);
updated.set("source_exchange", "FTX");
updated.set("target_exchange", "Binance");
auto i3 = updated.commit();
// i3 contains:
// "base_currency"
// "term_currency"
// "markup"
// "source_exchange"
// "target_exchange"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment