nullable

The Nullable Monad

https://github.com/steve-downey/nullable

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.0%) to scientific vocabulary
Last synced: 6 months ago · JSON representation ·

Repository

The Nullable Monad

Basic Info
  • Host: GitHub
  • Owner: steve-downey
  • License: apache-2.0
  • Language: CMake
  • Default Branch: main
  • Size: 20.5 KB
Statistics
  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Created 10 months ago · Last pushed 9 months ago
Metadata Files
Readme License Citation

README.md

beman.nullable: C++29 A Nullable Monad

CI Tests Coverage <!-- markdownlint-enable -->

Rationale

Types such as smart pointers, raw pointers, optional, expected and others are a kind of monad. Using the Functor and Monad interfaces currently in optional and expected are safer than naive direct use of * or ->. These operations should be generalized to all types with similar interfaces. Additional safer operations, which are not strictly speaking monadic, such as value_or, should also be provided, as they have proved useful.

The Nullable Monad

Nullable types are, in C++, types with a contextual conversion to bool which indicates they contain a value, and * and -> operators that provide access to the contained value, such that (*n).v is equivalent to n->v. It may be that operator->() is not strictly necessary, and that only operator*() is required, but they generally come as a set and help confirm intent to conform to being 'pointer-like'.

The operations that must exist for a type to be a monad, which is also by necessity a functor and an applicative:

  • map
  • unit (pure, return)
  • ap (<*>)
  • bind (>>=)
  • join

This is not a minimal basis set as there are several possible choices of operations to define a monad, but seeing what they each do for a particular monad instance helps build intuition about the monad instance. In general, knowing that some structure is monadic does not tell you what the operations do, only that some combinations must be equivalent and respect certain equivalances with respect to the identity function and associativity.

map

The map operation takes a callable and, if the nullable contains a value, applies the callable to the value, returning a nullable that is empty if the nullable was empty, or a nullable that contains the value. This nullable does not have to be of the same type.

auto map(nullable auto n, A->B) -> nullable

unit

The unit (pure, return, just) operation takes a value and returns a nullable holding that value.

auto unit(A a) -> nullable

ap

Applicative functors are really about n-ary functions for functors. Functional languages often only make explicit the 2-ary version, and assume currying or application to extend to higher cardinality functions. C++ does not usually make applicative explicit.

auto ap(nullable B> auto n, nullable auto a) -> nullable

bind

auto bind(nullable auto a, callable nullable> auto f) -> nullable

Bind is also sometimes called flat_map, as it 'flattens' a map where the functorial map would return a nullable>, bind collapses the type.

join

auto join(nullable> auto a) -> nullable

The isolated flatten operation.

Additional APIs

nullable::value_or

auto value_or(nullable a, B b) -> common type of A and B

Returns either the value held by a or the value b, as the common type of A and B, as if by ternary operator.

nullable::reference_or

auto reference_or(nullable a, B b) -> common reference type of A and B Returns either a reference to the value held by a or a reference to the value b, as the common reference type of A and B, as if by ternary operator.

Owner

  • Name: Steve Downey
  • Login: steve-downey
  • Kind: user
  • Location: United States
  • Company: Software engineer @Bloomberg

This is my personal collection of stuff, unconnected with my work @bloomberg.

Citation (CITATION.cff)

cff-version: 1.0.0
message: "If you use this software, please cite it as below."
title: "beman.nullable"
url: "https://github.com/bemanproject/nullable"

GitHub Events

Total
  • Push event: 12
  • Create event: 2
Last Year
  • Push event: 12
  • Create event: 2

Dependencies

requirements-dev.in pypi
  • clang-format ==18.1.8 development
  • gcovr * development
  • pre-commit * development
requirements-dev.txt pypi
  • cfgv ==3.4.0 development
  • clang-format ==18.1.8 development
  • colorlog ==6.8.2 development
  • distlib ==0.3.8 development
  • filelock ==3.15.4 development
  • gcovr ==7.2 development
  • identify ==2.6.0 development
  • jinja2 ==3.1.4 development
  • lxml ==5.3.0 development
  • markupsafe ==2.1.5 development
  • nodeenv ==1.9.1 development
  • platformdirs ==4.2.2 development
  • pre-commit ==3.7.1 development
  • pygments ==2.18.0 development
  • pyyaml ==6.0.1 development
  • virtualenv ==20.26.6 development
requirements.in pypi
requirements.txt pypi