feat: imrpovement and add more methods to Option and Result

This commit is contained in:
DL 2024-02-28 15:16:01 +01:00
parent 8a96704909
commit 47a2d0a64d
2 changed files with 513 additions and 54 deletions

269
src/ro.h
View File

@ -5,12 +5,13 @@
#include <string>
#include <memory>
#include <functional>
#include <tuple>
#define None(T) (ro::Option<T>())
#define Some(v) (ro::Option(v))
#define ERR(fmt, ...) Error(ro::sfmt("%s:%d: " fmt, __FILE__, __LINE__, ##__VA_ARGS__))
#define Err(e) (ro::Result(e))
#define Ok(v) (ro::Result(v))
// #define Err(e) (ro::Result(e))
// #define Ok(v) (ro::Result(v))
namespace ro
{
@ -46,7 +47,8 @@ namespace ro
}
};
template <typename T, typename E> class Result;
template <typename T, typename E>
class Result;
template <typename T>
class Option
@ -56,8 +58,13 @@ namespace ro
Option(T obj) : m_some(obj), m_notnull(true){};
Option() : m_notnull(false){};
Option<T>& operator=(const T& value) {insert(value); return *this;};
inline friend bool operator==(const Option<T>& l, const Option<T>& r) {
Option<T> &operator=(const T &value)
{
insert(value);
return *this;
};
inline friend bool operator==(const Option<T> &l, const Option<T> &r)
{
if (!l.m_notnull && !r.m_notnull)
{
return true;
@ -69,9 +76,41 @@ namespace ro
return false;
};
inline bool is_none(){return !m_notnull;};
inline bool is_some(){return m_notnull;};
inline T& unwrap()
inline friend Option<T> operator|(const Option<T> &l, const Option<T> &r)
{
if (l.is_some())
{
return l;
}
return r;
}
template <typename U>
inline friend Option<U> operator&(const Option<T> &l, const Option<U> &r)
{
if (l.is_some())
{
return r;
}
return Option<U>();
}
inline friend Option<T> operator^(const Option<T> &l, const Option<T> &r)
{
if (l.is_some() != r.is_some())
{
if (l.is_some())
{
return l;
}
return r;
}
return Option<T>();
}
inline bool is_none() const { return !m_notnull; };
inline bool is_some() const { return m_notnull; };
inline const T &unwrap() const
{
if (m_notnull)
{
@ -79,7 +118,26 @@ namespace ro
}
throw Error("Object is None");
}
inline T& get_or_insert(T value)
inline const T &unwrap_or(const T &alt) const
{
if (m_notnull)
{
return m_some;
}
return alt;
}
inline T unwrap_or_else(const std::function<T()> &fn) const
{
if (m_notnull)
{
return m_some;
}
return fn();
}
inline const T &get_or_insert(const T &value)
{
if (!m_notnull)
{
@ -87,7 +145,7 @@ namespace ro
}
return m_some;
}
inline T& insert(T value)
inline const T &insert(const T &value)
{
m_some = value;
m_notnull = true;
@ -95,7 +153,7 @@ namespace ro
}
template <typename E>
inline Result<T, E> ok_or(E err)
inline Result<T, E> ok_or(const E &err)
{
if (m_notnull)
{
@ -105,7 +163,26 @@ namespace ro
}
template <typename U>
inline Option<U> map(const std::function<U(T)>& fn)
inline Option<U> and_then(const std::function<Option<U>(const T &)> &fn)
{
if (m_notnull)
{
return fn(m_some);
}
return Option<U>();
}
inline Option<T> or_else(const std::function<Option<T>()> &fn)
{
if (m_notnull)
{
return *this;
}
return fn();
}
template <typename U>
inline Option<U> map(const std::function<U(const T &)> &fn)
{
if (is_some())
{
@ -115,7 +192,7 @@ namespace ro
}
template <typename U>
inline U map_or(U defv, const std::function<U(T)>& fn)
inline U map_or(const U &defv, const std::function<U(const T &)> &fn)
{
if (is_some())
{
@ -124,6 +201,44 @@ namespace ro
return defv;
}
inline Option<T> filter(const std::function<bool(const T &)> &fn)
{
if (is_some() && fn(m_some))
{
return Option<T>(m_some);
}
return Option<T>();
}
inline Option<T> take()
{
if (m_notnull)
{
m_notnull = false;
return Option<T>(m_some);
}
return Option<T>();
}
inline Option<T> take_if(const std::function<bool(const T &)> &fn)
{
if (m_notnull && fn(m_some))
{
return take();
}
return Option<T>();
}
template <typename U>
inline Option<std::tuple<T, U>> zip(const Option<U> &other)
{
if (m_notnull && other.is_some())
{
return Option<std::tuple<T, U>>(std::tuple<T, U>(m_some, other.unwrap()));
}
return Option<std::tuple<T, U>>();
}
private:
T m_some;
bool m_notnull;
@ -138,13 +253,82 @@ namespace ro
Result(E err) { m_error.insert(err); };
Result(){};
inline Result<T,E>& operator=(const T& value) {m_result = value; m_error = None(E); return *this;};
inline Result<T,E>& operator=(const E& err) {m_error.insert(err); return *this;};
inline Result<T, E> &operator=(const T &value)
{
m_result = value;
m_error = None(E);
return *this;
};
inline Result<T, E> &operator=(const E &err)
{
m_error.insert(err);
return *this;
};
inline bool is_ok(){return m_error.is_none();};
inline bool is_err(){return m_error.is_some();};
inline friend bool operator==(const Result<T, E> &l, const Result<T, E> &r)
{
if (l.is_ok() != r.is_ok())
{
return false;
}
if (l.is_ok())
{
return l.m_result == r.m_result;
}
return l.m_error == r.m_error;
};
inline T& unwrap()
inline friend bool operator==(const Result<T, E> &l, const E &r)
{
if (l.is_ok())
{
return false;
}
return l.m_error == r;
};
inline friend bool operator==(const Result<T, E> &l, const T &r)
{
if (l.is_err())
{
return false;
}
return l.m_result == r;
};
inline friend Result<T, E> operator|(const Result<T, E> &l, const Result<T, E> &r)
{
if (l.is_ok())
{
return l;
}
return r;
}
template <typename U>
inline friend Result<U, E> operator&(const Result<T, E> &l, const Result<U, E> &r)
{
if (l.is_ok())
{
return r;
}
return Result<U, E>(l.m_error.unwrap());
}
inline bool is_ok() const { return m_error.is_none(); };
inline bool is_err() const { return m_error.is_some(); };
template <typename F>
inline Result<T, F> or_else(const std::function<Result<T, F>(const E &)> &fn)
{
if (is_ok())
{
return Result<T, F>(m_result);
}
return fn(m_error.unwrap());
}
inline const T &unwrap() const
{
if (is_ok())
{
@ -153,7 +337,16 @@ namespace ro
throw m_error.unwrap();
}
inline T& expect(E e)
inline const E &unwrap_err() const
{
if (is_err())
{
return m_error.unwrap();
}
throw m_result;
}
inline const T &expect(E e) const
{
if (is_ok())
{
@ -163,7 +356,7 @@ namespace ro
}
template <typename U>
inline Result<U,E> map(const std::function<U(T)>& fn)
inline Result<U, E> map(const std::function<U(const T &)> &fn)
{
if (is_ok())
{
@ -173,7 +366,7 @@ namespace ro
}
template <typename E1>
inline Result<T,E1> map_err(const std::function<E1(E)>& fn)
inline Result<T, E1> map_err(const std::function<E1(const E &)> &fn)
{
if (!is_ok())
{
@ -183,7 +376,7 @@ namespace ro
}
template <typename U>
inline U map_or(U defv, const std::function<U(T)>& fn)
inline U map_or(const U &defv, const std::function<U(const T &)> &fn)
{
if (is_ok())
{
@ -192,8 +385,36 @@ namespace ro
return defv;
}
Option<E> err() { return m_error; }
Option<T> ok() { return is_ok()?Option<T>(m_result): Option<T>(); }
template <typename U>
inline Result<U, E> and_then(const std::function<Result<U, E>(const T &)> &fn)
{
if (is_ok())
{
return fn(m_result);
}
return Result<U, E>(m_error.unwrap());
}
inline const T &unwrap_or(const T &alt) const
{
if (is_ok())
{
return m_result;
}
return alt;
}
inline T unwrap_or_else(const std::function<T(const E &)> &fn)
{
if (is_ok())
{
return m_result;
}
return fn(m_error.unwrap());
}
inline const Option<E> &err() const { return m_error; }
inline Option<T> ok() const { return is_ok() ? Option<T>(m_result) : Option<T>(); }
private:
T m_result;

View File

@ -2,12 +2,19 @@
#include "../src/ro.h"
#include <functional>
#include <iostream>
#include <cstring>
using namespace std;
using namespace ro;
struct Stat{
int passed;
int total;
};
static Stat g_stat = {0,0};
#define assert(v,fmt,...) if(!(v)) { \
throw Error(ro::sfmt("ASSERT ERROR %s:%d: " fmt, __FILE__, __LINE__, ##__VA_ARGS__)); \
}
@ -19,12 +26,14 @@ void test(const char* desc, const std::function<void()>& lambda)
try {
lambda();
std::cout << "OK" << std::endl;
g_stat.passed++;
}
catch(ro::Error e)
{
std::cout << "FAILED" << std::endl;
std::cout << e.what() << std::endl;
}
g_stat.total++;
}
int main(int argc, char const *argv[])
@ -52,6 +61,14 @@ int main(int argc, char const *argv[])
assert(ret.unwrap() == "abc", "Data mistmatch");
});
test("OPtion get_or_insert", [](){
auto x = None(int);
int y = x.get_or_insert(5);
assert(y == 5, "Unexpected value");
assert(x == 5, "Unexpected value");
});
test("Option map", [](){
auto fn = [](int x){
return "Hello";
@ -67,7 +84,7 @@ int main(int argc, char const *argv[])
});
test("Option map or", [](){
test("Option map_or", [](){
auto fn = [](int x){
return ++x;
};
@ -81,6 +98,140 @@ int main(int argc, char const *argv[])
assert(v == 3, "Value mismatch %d",v);
});
test("Option filter", [](){
auto fn = [](int n) {
return n % 2 == 0;
};
//Option<int> opt = None(int).filter(fn);
assert(None(int).filter(fn).is_none(), "Filter value shall be None");
assert(Some(3).filter(fn).is_none(), "Filter value shall be None");
assert(Some(4).filter(fn).is_some(), "Filter value shall not be None");
});
test("Option OR", [](){
auto x = Some(2);
auto y = None(int);
assert((x | y) == Some(2), "Unexpected value");
x = None(int);
y = Some(100);
assert((x | y) == Some(100), "Unexpected value");
x = Some(2);
y = Some(100);
assert((x|y) == Some(2), "Unexpected value");
x = None(int);
y = None(int);
assert((x | y) == None(int), "Unexpected value");
});
test("Option AND", [](){
auto x = Some(2);
auto y = None(int);
assert((x & y) == None(int), "Unexpected value");
x = None(int);
y = Some(100);
assert((x & y) == None(int), "Unexpected value");
x = Some(2);
y = Some(100);
assert((x&y) == Some(100), "Unexpected value");
x = None(int);
y = None(int);
assert((x & y) == None(int), "Unexpected value");
});
test("Option XOR", [](){
auto x = Some(2);
auto y = None(int);
assert((x ^ y) == Some(2), "Unexpected value");
x = None(int);
y = Some(100);
assert((x ^ y) == Some(100), "Unexpected value");
x = Some(2);
y = Some(100);
assert((x^y) == None(int), "Unexpected value");
x = None(int);
y = None(int);
assert((x ^ y) == None(int), "Unexpected value");
});
test("Option or_else", [](){
auto nobody = [](){return None(int);};
auto vikings = [](){return Some(100);};
assert(None(int).or_else(vikings) == Some(100), "Unexpected value");
assert(None(int).or_else(nobody) == None(int), "Unexpected value");
assert(Some(2).or_else(vikings) == Some(2), "Unexpected value");
});
test("Option and_then", [](){
auto fn = [](int x){return string("Hello");};
assert(Some(1).and_then<string>(fn) == Some(string("Hello")), "Unexpected value");
assert(None(int).and_then<string>(fn) == None(string), "Unexpected value");
});
test("Option take", [](){
auto x = Some(2);
auto y = x.take();
assert(x == None(int), "Unexpected value");
assert(y == Some(2), "Unexpected value");
auto z = None(int);
y = z.take();
assert(x == None(int), "Unexpected value");
assert(y == None(int), "Unexpected value");
});
test("Option take_if", [](){
auto fn = [](int n) {
return n % 2 == 0;
};
auto x = Some(2);
auto y = x.take_if(fn);
assert(x == None(int), "Unexpected value");
assert(y == Some(2), "Unexpected value");
auto z = Some(3);
y = z.take_if(fn);
assert(z == Some(3), "Unexpected value");
assert(y == None(int), "Unexpected value");
});
test("Option unwrap_or", [](){
assert(Some(2).unwrap_or(4) == 2, "Unexpected value");
assert(None(int).unwrap_or(4) == 4, "Unexpected value");
assert(None(Error).unwrap_or(Error("Hello")) == Error("Hello"), "Unexpected value");
});
test("Option unwrap_or_else", [](){
assert(Some(2).unwrap_or_else([](){ return 4; }) == 2, "Unexpected value");
assert(None(int).unwrap_or_else([](){ return 4; }) == 4, "Unexpected value");
});
test("Option zip", [](){
auto x = Some(2);
auto y = Some(string("Hello"));
auto z = None(int);
assert(x.zip(y).unwrap() == std::tuple(2, string("Hello")), "Unexpected value");
assert(x.zip(z).is_none(), "Unexpected value");
});
test("Test Result", []{
Result<string, Error> ret = ERR("Error");
assert(ret.is_err(), "Object should containe error object");
@ -132,7 +283,7 @@ int main(int argc, char const *argv[])
});
test("Result map err", [](){
test("Result map_err", [](){
auto fn = [](int x){
return "Hello";
};
@ -145,7 +296,7 @@ int main(int argc, char const *argv[])
assert(ret1.err().unwrap() == "Hello", "Value mistmatch");
});
test("Result map or", [](){
test("Result map_or", [](){
auto fn = [](int x){
return ++x;
};
@ -159,4 +310,91 @@ int main(int argc, char const *argv[])
assert(v == 3, "Value mismatch %d",v);
});
test("Result AND", [](){
Result<int, Error> x = 2;
Result<int, Error> y = Error("Hello");
assert((x&y) == Error("Hello"), "Value mismatch");
x = Error("Error1");
y = 10;
assert((x&y) == Error("Error1"), "Value mismatch");
x = Error("Error1");
y = Error("Error2");
assert((x&y) == Error("Error1"), "Value mismatch");
x = 1;
y = 10;
assert((x&y) == 10, "Value mismatch");
});
test("Result OR", [](){
Result<int, Error> x = 2;
Result<int, Error> y = Error("Hello");
assert((x|y) == 2, "Value mismatch");
x = Error("Error1");
y = 10;
assert((x|y) == 10, "Value mismatch");
x = Error("Error1");
y = Error("Error2");
assert((x|y) == Error("Error2"), "Value mismatch");
x = 1;
y = 10;
assert((x|y) == 1, "Value mismatch");
});
test("Result and_then", [](){
auto fn = [](int x){return string("Hello");};
Result<int, Error> x = 2;
auto test = Result<string,Error>(string("Hello"));
assert(x.and_then<string>(fn) == string("Hello"), "Unexpected value");
x = Error("Error");
assert(x.and_then<string>(fn) == Error("Error"), "Unexpected value");
});
test("Result or_else", [](){
auto sq = [](int x){return x*x;};
auto err = [](int x){return x;};
Result<double, int> x(2.0);
auto ret = x.or_else<int>(sq).or_else<int>(sq);
assert(ret == 2.0, "Unexpected value");
ret = x.or_else<int>(err).or_else<int>(sq);
assert(ret == 2.0, "Unexpected value");
x = 3;
ret = x.or_else<int>(sq).or_else<int>(err);
assert(ret == 9, "Unexpected value");
ret = x.or_else<int>(err).or_else<int>(err);
assert(ret == 3, "Unexpected value");
});
test("Result unwrap_err", [](){
Result<int, Error> x = Error("Hello");
assert(x.unwrap_err() == Error("Hello"), "Unexpected value");
});
test("Result unwrap_or", [](){
Result<int, Error> x = 2;
assert(x.unwrap_or(10) == 2, "Unexpected value");
x = ERR("Error");
assert(x.unwrap_or(10) == 10, "Unexpected value");
});
test("Result unwrap_or_else", [](){
auto fn = [](const Error& x){return strlen(x.what());};
Result<int, Error> x = 2;
assert(x.unwrap_or_else(fn) == 2, "Unexpected value");
x = Error("HELLO");
assert(x.unwrap_or_else(fn) == 5, "Unexpected value");
});
cout << "REPORT: " << g_stat.passed << " tests passed / " << g_stat.total << " tests." << endl;
}