Monadic interface for std::optional

optional

Simple monadic interface for std::optional

Installation

Just copy and include optional.h header in your project

Usage

All operations are in bsc::optional namespace so it is advised to add

using namespace bsc::optional;

At begining of a scope.

Supported operations:

map

Transforms the value in optional to something else (if present).

using namespace bsc::optional;
std::optional<int> optInt = 5;
auto timesTwo  = [](auto i) { return i * 2; };

auto result = optInt | map(timesTwo); //result is optional containing 10

flatMap

Transforms the value in optional to something else (if present). Same as map, but for functions that return optional

optGoodString = "10"; auto toInt = [](auto i){ try { return std::make_optional(std::stoi(i)); } catch (const std::invalid_argument&) { return std::optional{}; } }; auto resultBad = optBadString | flatMap(toInt); //empty optional auto resultGood = optGoodString | flatMap(toInt); //optional with value = 10 ">
using namespace bsc::optional;
std::optional optBadString = "not a number";
std::optional optGoodString = "10";

auto toInt = [](auto i){
    try {
        return std::make_optional<int>(std::stoi(i));
    } catch (const std::invalid_argument&) {
        return std::optional<int>{};
    }
};

auto resultBad = optBadString | flatMap(toInt); //empty optional
auto resultGood = optGoodString | flatMap(toInt); //optional with value = 10

filter

Returns the same value if it matches the predicate or empty optional if it doesn't.

    using namespace bsc::optional;
    auto isEven               = [](auto i) { return i % 2 == 0; };
    std::optional<int> optFour = 4;
    auto resultEven = optInt | filter(isEven); //result is optional with value =4
    
    std::optional<int> optFive = 5;
    auto resultOdd = optInt | filter(isEven); //result is empty optional

orElse, orElseGet, orElseThrow

If optional is not empty, then return its value or else return supplied object (orElse), result of a callable(orElseGet) or throw exception (orElseThrow)

    using namespace bsc::optional;
    auto isEven               = [](auto i) { return i % 2 == 0; };
    std::optional<int> optFour = 4;
    auto resultEven = optInt | filter(isEven) | orElse(10); //result is int = 4
    
    std::optional<int> optFive = 5;
    auto resultOdd = optInt | filter(isEven) | orElse(10); //result is int = 10

ifPresent

If optional hold value, call function passes as argument.

using namespace bsc::optional;
std::optional<int> optInt = 5;
auto print  = [](auto i) { std::cout << i << std::endl };

auto result = optInt | ifPresent(print); // prints "5" to standard output

License

optional.h is released under MIT license.

#Todo

  • create cmake configuration for easier installation
  • create package for package managers like conan
Similar Resources

A standard conforming C++20 implementation of std::optional.

A standard conforming C++20 implementation of std::optional.

Feb 11, 2022

C++11/14/17 std::optional with functional-style extensions and reference support

optional Single header implementation of std::optional with functional-style extensions and support for references. Clang + GCC: MSVC: std::optional i

Jun 21, 2022

Thread pool - Thread pool using std::* primitives from C++17, with optional priority queue/greenthreading for POSIX.

thread_pool Thread pool using std::* primitives from C++11. Also includes a class for a priority thread pool. Requires concepts and C++17, including c

Jun 15, 2022

Fast C++ container combining the best features of std::vector and std::deque

veque The double-ended vector A very fast C++17 container combining the best features of std::vector and std::deque "In Most Cases, Prefer Using deque

May 4, 2022

Invoke.hpp - std::invoke/std::apply analogs for C++11/14

invoke.hpp std::invoke/std::apply analogs for C++11/14 Requirements gcc = 4.9 clang = 3.8 msvc = 2015 Installation invoke.hpp is a header-only libr

Apr 27, 2022

Cpp-std-fwd - forward declarations for C++ std headers

cpp-std-fwd Forward declarations for most useful runtime classes of the C++ 17 standard library. DISCLAIMER: This project is meant as a proof-of-conce

Apr 22, 2022

C++ functions matching the interface and behavior of python string methods with std::string

Pystring is a collection of C++ functions which match the interface and behavior of python's string class methods using std::string. Implemented in C+

Jun 13, 2022

CRC32 slice-by-16 implementation in JS with an optional native binding to speed it up even futher

CRC32 slice-by-16 implementation in JS with an optional native binding to speed it up even futher. When used with Webpack/Browserify etc, it bundles the JS version.

Aug 4, 2021

Fully resizing juce peak meter module with optional fader overlay.

Fully resizing juce peak meter module with optional fader overlay.

Sound Meter Juce peak meter module with optional fader overlay. by Marcel Huibers | Sound Development 2021 | Published under the MIT License Features:

Jun 8, 2022

New version of the well known ESP32 Radio. Now optional I2S output!

ESP32Radio-V2 New version of the well known ESP32 Radio. Now optional I2S output! Compile time configuration in config.h. Do not forget to upload the

Jun 12, 2022

Qt5 image viewer with optional video support

Qt5 image viewer with optional video support

Qt5 image viewer with optional video support

Jun 20, 2022

Zep is a simple embeddable editor, with a rendering agnostic design and optional Vim mode

Zep is a simple embeddable editor, with a rendering agnostic design and optional Vim mode

Zep - A Mini Editor Zep is a simple embeddable editor, with a rendering agnostic design and optional Vim mode. It is built as a shared modern-cmake li

Feb 26, 2022

DLL Exports Extraction BOF with optional NTFS transactions.

DLL Exports Extraction BOF with optional NTFS transactions.

DLL Exports Extraction BOF What is this? This is a Cobalt Strike BOF file, meant to use two or three arguments (path to DLL, and/or a third argument [

May 30, 2022

Named Optional Arguments in C++17

Optional Argument in C++ News Mon 02 Dec 2019 [0.0.2 tag] Cancel any possible implicit conversion by making constructors explicit. Thu 21 Nov 2019 [0.

Dec 2, 2019

Zep - An embeddable editor, with optional support for using vim keystrokes.

Zep - An embeddable editor, with optional support for using vim keystrokes.

Zep - A Mini Editor Zep is a simple embeddable editor, with a rendering agnostic design and optional Vim mode. It is built as a shared modern-cmake li

Jun 18, 2022

STFT based multi pitch shifting with optional formant preservation in C++ and Python

stftPitchShift This is a reimplementation of the Stephan M. Bernsee smbPitchShift.cpp, a pitch shifting algorithm using the Short-Time Fourier Transfo

May 11, 2022

Legion Low Level Rendering Interface provides a graphics API agnostic rendering interface with minimal CPU overhead and low level access to verbose GPU operations.

Legion Low Level Rendering Interface provides a graphics API agnostic rendering interface with minimal CPU overhead and low level access to verbose GPU operations.

Legion-LLRI Legion-LLRI, or “Legion Low Level Rendering Interface” is a rendering API that aims to provide a graphics API agnostic approach to graphic

Mar 8, 2022

A drop-in replacement for std::list with 293% faster insertion, 57% faster erasure, 17% faster iteration and 77% faster sorting on average. 20-24% speed increase in use-case testing.

plf_list A drop-in replacement for std::list with (on average): 293% faster insertion 57% faster erasure 17% faster iteration 77% faster sorting 70% f

Apr 11, 2022
A standard conforming C++20 implementation of std::optional.

A standard conforming C++20 implementation of std::optional.

Feb 11, 2022
C++11/14/17 std::optional with functional-style extensions and reference support

optional Single header implementation of std::optional with functional-style extensions and support for references. Clang + GCC: MSVC: std::optional i

Jun 21, 2022
Named Optional Arguments in C++17

Optional Argument in C++ News Mon 02 Dec 2019 [0.0.2 tag] Cancel any possible implicit conversion by making constructors explicit. Thu 21 Nov 2019 [0.

Dec 2, 2019
C++11/14/17 std::expected with functional-style extensions

expected Single header implementation of std::expected with functional-style extensions. Clang + GCC: MSVC: Available on Vcpkg and Conan. std::expecte

Jun 15, 2022
A collection of std-like single-header C++ libraries

itlib: iboB's Template Libraries A collection of small single-header C++ libraries similar to or extending the C++ standard library. See below for a l

Jun 20, 2022
Range library for C++14/17/20, basis for C++20's std::ranges

range-v3 Range library for C++14/17/20. This code was the basis of a formal proposal to add range support to the C++ standard library. That proposal e

Jun 14, 2022
Allows a programmer to print table-like outputs over std::ostream.

tableprinter Allows a programmer to print table-like outputs over std::ostream. It is a header only library. No other dependency than STL. Provides re

Apr 15, 2022
Improved and configurable drop-in replacement to std::function that supports move only types, multiple overloads and more

fu2::function an improved drop-in replacement to std::function Provides improved implementations of std::function: copyable fu2::function move-only fu

Jun 11, 2022
A library for using real OPL2 synth chips via ALSA's hwdep interface.

oplhw: A library for using real OPL2 synth chips via ALSA's hwdep interface ==========================================================================

Nov 30, 2021
optional lite - A C++17-like optional, a nullable object for C++98, C++11 and later in a single-file header-only library

optional lite: A single-file header-only version of a C++17-like optional, a nullable object for C++98, C++11 and later Contents Example usage In a nu

Jun 7, 2022