Skip to content

Instantly share code, notes, and snippets.

@juxeii
Last active January 25, 2021 08:48
Show Gist options
  • Save juxeii/22941451dbe0cf3bf8a42ca6266b24c6 to your computer and use it in GitHub Desktop.
Save juxeii/22941451dbe0cf3bf8a42ca6266b24c6 to your computer and use it in GitHub Desktop.
Blog post for Optional
auto nameByShirtNo = std::map<int, std::string>{{8, "Andres Iniesta"}};
const auto shirtNumber = 8;
const auto nameExists = nameByShirtNo.contains(shirtNumber);
if (nameExists)
{
const auto playerName = nameByShirtNo.at(shirtNumber);
std::cout << playerName << " wears shirt number " << shirtNumber;
}
else
{
std::cout << "Shirt number " << shirtNumber << " is not mapped to any player";
}
try
{
auto playerName = nameByShirtNo.at(shirtNumber);
std::cout << playerName << " wears shirt number " << shirtNumber;
} catch (const std::out_of_range&)
{
std::cout << "Shirt number " << shirtNumber << " is not mapped to any player";
}
const auto search = nameByShirtNo.find(shirtNumber);
if (search != nameByShirtNo.end()) {
const auto playerName = search->second;
std::cout << playerName << " wears shirt number " << shirtNumber;
}
else
{
std::cout << "Shirt number " << shirtNumber << " is not mapped to any player ";
}
std::optional<std::string> maybePlayerName(const int shirtNumber, const std::map<int, std::string> &nameByShirtNo)
{
const auto search = nameByShirtNo.find(shirtNumber);
if (search != nameByShirtNo.end())
{
return std::optional{search->second};
}
return std::nullopt;
}
const auto playerName = maybePlayerName(shirtNumber, nameByShirtNo);
if (playerName.has_value())
{
std::cout << *playerName << " wears shirt number " << shirtNumber;
}
else
{
std::cout << "Shirt number " << shirtNumber << " is not mapped to any player ";
}
tl::optional<std::string> maybePlayerName(const int shirtNumber, const std::map<int, std::string> &nameByShirtNo)
{
const auto search = nameByShirtNo.find(shirtNumber);
if (search != nameByShirtNo.end())
{
return search->second;
}
return tl::nullopt;
}
maybePlayerName(shirtNumber, nameByShirtNo)
.map([](const auto playerName) { std::cout << playerName << " wears shirt number " << shirtNumber; })
.orElse([]{ std::cout << "Shirt number " << shirtNumber << " is not mapped to any player "; });
void printPlayerName(const std::string& playerName)
{
std::cout << playerName << " was found!";
}
void printNoPlayerFound()
{
std::cout << "No player found!";
}
maybePlayerName(shirtNumber, nameByShirtNo)
.map(printPlayerName)
.orElse(printNoPlayerFound);
std::optional<int> maybePlayerAge(const std::string& playerName, const std::map<std::string, int> &ageByName)
{
const auto search = ageByName.find(playerName);
if (search != ageByName.end())
{
return search->second;
}
return std::nullopt;
}
auto isOlderThanThirty(const int currentAge)
{
return currentAge>30;
}
const auto playerName = maybePlayerName(shirtNumber, nameByShirtNo);
if (playerName.has_value())
{
const auto playerAge = maybePlayerAge(*playerName, ageByPlayerName);
if (playerAge.has_value())
{
if (isOlderThanThirty(*playerAge))
{
std::cout << "Player with shirt number " << shirtNumber <<
" is older 30";
}
else
{
std::cout << "Player with shirt number " << shirtNumber <<
" is either not existing or younger than 31";
}
}
}
maybePlayerName(shirtNumber, nameByShirtNo)
.flatMap([&ageByPlayerName](auto playerName){ return maybePlayerAge(playerName, ageByPlayerName); })
.filter(isOlderThanThirty)
.map([](auto){std::cout << "Player with shirt number " << shirtNumber <<
" is older 30";})
.orElse([]{ std::cout << "Player with shirt number " << shirtNumber <<
" is either not existing or younger than 31"; });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment