forked from foonathan/type_safe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
13 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// Copyright (C) 2016 Jonathan Müller <[email protected]> | ||
// Copyright (C) 2016-2018 Jonathan Müller <[email protected]> | ||
// This file is subject to the license terms in the LICENSE file | ||
// found in the top-level directory of this distribution. | ||
|
||
|
@@ -20,7 +20,7 @@ ts::optional<char> back(const std::string& str) | |
} | ||
|
||
// some imaginary lookup function | ||
ts::optional<int> lookup(char c) | ||
ts::optional<int> lookup(int c) | ||
{ | ||
// simulate lookup | ||
return c == 'T' ? ts::nullopt : ts::make_optional(c + 1); | ||
|
@@ -50,11 +50,11 @@ int task_monadic(const std::string& str) | |
return back(str) | ||
// map takes a functor and applies it to the stored value, if there is any | ||
// the result is another optional with possibly different type | ||
.map([](char c) -> char { return std::toupper(c); }) | ||
// lookup is similar to map | ||
// but the result of map(lookup) would be a ts::optional<ts::optional<int>> | ||
// bind() calls unwrap() to flatten it to ts::optional<int> | ||
.bind(lookup) | ||
.map(static_cast<int (*)(int)>(&std::toupper)) | ||
// now we map lookup | ||
// as lookup returns already an optional itself, | ||
// it won't wrap the result in another optional | ||
.map(lookup) | ||
// value_or() as usual | ||
.value_or(0); | ||
} | ||
|
@@ -100,7 +100,7 @@ int main() | |
ts::optional_ref<int> ref; | ||
|
||
int a = 42; | ||
ref = a; // assignment rebinds | ||
ref = ts::ref(a); // assignment rebinds | ||
std::cout << ref.value() << '\n'; | ||
|
||
ref.value() = 0; | ||
|
@@ -116,15 +116,10 @@ int main() | |
ts::optional_ref<const int> ref_const(ref); | ||
|
||
// create optional_ref from pointer | ||
auto ptr = ts::ref(&a); | ||
auto ptr_const = ts::cref(&a); | ||
|
||
// map() takes a functor and wraps the result in an optional itself | ||
// transform() does not wrap it in an optional to allow arbitrary transformation | ||
// but it needs a fallback value if there is no value stored | ||
// here transform() is used to transform an optional_ref to a normal optional | ||
auto ptr_transformed = ptr.transform(ts::optional<int>(), [](int a) { return a; }); | ||
// note that the same for this particular situation can be achieved like this | ||
ptr_transformed = ts::copy(ptr); // there is also ts::move() to move the value | ||
auto ptr = ts::opt_ref(&a); | ||
auto ptr_const = ts::opt_cref(&a); | ||
|
||
/// transform an optional_ref to an optional by copying | ||
auto ptr_transformed = ts::copy(ptr); // there is also ts::move() to move the value | ||
std::cout << ptr_transformed.value() << '\n'; | ||
} |