Skip to content

Instantly share code, notes, and snippets.

@adishavit
Last active September 21, 2016 14:41
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 adishavit/7f7771fe4cbed4b18799f64a7d59cade to your computer and use it in GitHub Desktop.
Save adishavit/7f7771fe4cbed4b18799f64a7d59cade to your computer and use it in GitHub Desktop.
Run Time C++ Dependent Type Experiment - zip
#include <vector>
#include <string>
#include <iostream>
#include "optional.hpp"
using nonstd::optional;
class prove_same_size
{
public:
static optional<prove_same_size> prove(std::vector<int> const& v1, std::vector<int> const& v2)
{
if (v1.size() == v2.size())
return prove_same_size(v1,v2);
return {};
}
std::vector<int> const& v1_;
std::vector<int> const& v2_;
private:
explicit prove_same_size(std::vector<int> const& v1, std::vector<int> const& v2) : v1_(v1), v2_(v2) {}
};
auto zip(std::vector<int> const& v1, std::vector<int> const& v2)
{
std::vector<std::pair<int, int>> zipped;
for (int i = 0; i < v1.size(); ++i)
zipped.push_back({ v1[i], v2[i] });
return zipped;
}
auto zip(prove_same_size const& vecs)
{
std::vector<std::pair<int, int>> zipped;
zipped.reserve(vecs.v1_.size());
for (int i = 0; i < vecs.v1_.size(); ++i)
zipped.push_back({ vecs.v1_[i], vecs.v2_[i] });
return zipped;
}
int main()
{
std::vector<int> const v1 = { 1,2,3,4 };
std::vector<int> const v2 = { 4,3,2,1 };
auto zipped = zip(v1, v2);
auto y = prove_same_size::prove(v1, v2); // this is the proof
if (y)
{
auto zipped = zip(y.value());
assert(v1.size() == zipped.size());
}
else
std::cout << "Input different sizes!" << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment