Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
7c9c0df
Update README.md
njlr Oct 24, 2018
3ca550e
Maybe: Treat hasValue with true/false instead of 1/0
rvarago Nov 5, 2018
caf2e65
Maybe: Add STL compatible size() (>= C++11)
rvarago Nov 4, 2018
286f7cd
Maybe: Add STL compatible empty() (>= C++11)
rvarago Nov 5, 2018
44a4e6f
updates buck and travis.yml
nikhedonia Nov 5, 2018
14fa8ca
* Fixes tests
njlr Nov 5, 2018
d79bb59
Merge pull request #19 from rvarago/maybe-size_empty
nikhedonia Nov 5, 2018
6d11902
Merge pull request #1 from rvarago/maybe-size_empty
rvarago Nov 6, 2018
3b6ca6e
Maybe: Add static const object none
rvarago Nov 11, 2018
833a7a3
Create buckaroo.toml
njlr Nov 19, 2018
0e00b01
* Moves gtest to submodule
njlr Nov 19, 2018
51608cc
Update .gitmodules
njlr Nov 19, 2018
05ebe5f
Merge pull request #21 from LoopPerfect/improvement/gtest-submodule
njlr Nov 20, 2018
b433263
chore: make equals simpler
nikhedonia Dec 2, 2018
5519deb
Merge pull request #20 from rvarago/maybe_none
nikhedonia Dec 2, 2018
2120eb5
Merge pull request #23 from LoopPerfect/pr-20
nikhedonia Dec 2, 2018
b4121ee
Replace uniform initialization with copy construction
WojciechMigda Dec 4, 2018
91d4878
Merge pull request #24 from WojciechMigda/uniform-initialization
njlr Dec 6, 2018
8256df4
Silence 'defined but not used' warning
WojciechMigda Dec 9, 2018
d525c3c
Merge pull request #25 from WojciechMigda/defined-but-not-used-warning
nikhedonia Dec 9, 2018
e1d480a
Remove copy construction from argument to std::move in rightMap &&
WojciechMigda Dec 10, 2018
cc6f617
Merge pull request #26 from WojciechMigda/rightMap-with-noncopyable
nikhedonia Dec 10, 2018
c334207
* Migrates to Buckaroo Redux
njlr Jan 9, 2019
4c3c4f2
* Disables superconsole for Travis
njlr Jan 9, 2019
713c3e3
* Migrates to SKYLARK
njlr Jan 9, 2019
f6e8b3b
* C++ 14 for macOS
njlr Jan 9, 2019
95c855c
* Upgrades to newer Buck
njlr Jan 9, 2019
b3fbfde
Merge pull request #27 from LoopPerfect/buckaroo-redux
nikhedonia Jan 9, 2019
90ce0f2
Fix typo in README
ingve Jan 18, 2019
a7af11e
Merge pull request #28 from ingve/master
njlr Jan 18, 2019
30cffc1
all except join are now moving from lvalue
Oct 26, 2019
66be8e2
working join with move
Oct 26, 2019
51dca4d
smaller cleanup of either.cpp test
Oct 26, 2019
9f7db87
Update before-install-osx.sh
njlr Oct 28, 2019
d2db83d
Merge pull request #30 from tmattha/feature/store_unique_ptrs
nikhedonia Oct 31, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .buckconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
[project]
ignore = .git, .buckd

[parser]
default_build_file_syntax = SKYLARK

[cxx]
should_remap_host_platform = true

[cxx#linux-x86_64]
cxxflags = -std=c++14

[cxx#macosx-x86_64]
cxxflags = -std=c++14
gtest_dep = google.gtest//:gtest
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ buckaroo
buck-out
.buckd
.buckconfig.local
.buckconfig.d
BUCKAROO_DEPS
buckaroo_macros.bzl
32 changes: 20 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,27 @@ addons:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-5
- g++-6
- gcc-6
homebrew:
taps:
- facebook/fb
packages:
- buck

os:
- linux
- osx

env:
- BUCKAROO_VERSION=buckaroo-redux-alpha-8

osx_image: xcode9.3

before_install:
- cd /usr/bin/ && sudo rm g++ && sudo ln -s g++-5 g++ && cd -
- g++ --version
- sudo apt-get install default-jdk
- wget -O buck.deb https://github.com/facebook/buck/releases/download/v2017.09.04.02/buck-2017.09.04.02_all.deb
- sudo dpkg -i buck.deb
- wget -O buckaroo.deb https://github.com/LoopPerfect/buckaroo/releases/download/v1.3.1/buckaroo_1.3.1_amd64.deb
- sudo dpkg -i buckaroo.deb
- ./travis/before-install-$TRAVIS_OS_NAME.sh

script:
- buckaroo install
- buck build //:neither
- buck build //:test#linux-x86_64
- buck run //:test#linux-x86_64
- ./buckaroo-client install
- buck build -c ui.superconsole=DISABLED :neither
- buck test -c ui.superconsole=DISABLED //...
14 changes: 7 additions & 7 deletions BUCK
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
load('//:subdir_glob.bzl', 'subdir_glob')
load('//:buckaroo_macros.bzl', 'buckaroo_deps_from_package')

prebuilt_cxx_library(
name = 'neither',
header_only = True,
Expand All @@ -18,11 +21,8 @@ cxx_test(
srcs = glob([
'neither/tests/**/*.cpp',
]),
platform_linker_flags = [
('^linux.*', [ '-lpthread', ]),
],
link_style = 'shared',
deps = [
':neither',
],
deps = buckaroo_deps_from_package('github.com/buckaroo-pm/google-googletest') + \
[
':neither',
],
)
38 changes: 18 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<a target='_blank' rel='nofollow' href='https://app.codesponsor.io/link/hq7GXSxnYW3qEM98fqMVob9v/LoopPerfect/neither'> <img alt='Sponsor' width='888' height='68' src='https://app.codesponsor.io/embed/hq7GXSxnYW3qEM98fqMVob9v/LoopPerfect/neither.svg' /></a>

# neither

A functional implementation of Either in C++14.

[![Travis](https://img.shields.io/travis/LoopPerfect/neither.svg)](https://travis-ci.org/LoopPerfect/neither) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/LoopPerfect/neither/master/license)

```
buckaroo install loopperfect/neither
buckaroo add github.com/loopperfect/neither
```

## Examples
Expand All @@ -26,15 +24,15 @@ auto unsafe = [] { // a function that throws, sometimes we can't avoid it...
Either<std::exception, int> e = Try<std::exception>(unsafe); // let's lift the exception into the typesystem

e.left()
.map([](auto const& e) {
return std::cerr << e.what() << std::endl;
.map([](auto const& e) {
return std::cerr << e.what() << std::endl;
}); // print error if available

int result = e
.leftMap([](auto) { return 42; }) // do nothing with exception and map to 42
.rightMap([](auto x) { return x * 2; }) // do further computation if value available
.join() // join both sides of either

ASSERT_TRUE(result == 42);

```
Expand All @@ -53,7 +51,7 @@ std::string resultString = compute(5)
[](auto errorStr) { return "compute said: " + errorStr; }, // error-case
[](auto x) { return "compute said: " + std::to_string(x); } // success-case
);

std::cout << resultString << std::endl;

```
Expand Down Expand Up @@ -93,11 +91,11 @@ Maybe<float> compute(float x) {
Maybe<float> x = compute(-4)
.map([](auto x){ return x*x;})
.map([](auto x){ return x+1 });

if(!x.hasValue) {
std::cerr << "error occured" << std::endl;
}

```

### Monadic Lifting
Expand All @@ -112,12 +110,12 @@ auto monadicSum = lift(sum); // transforms sum to: Maybe<int> MonadicSum(Maybe<i

ASSERT_TRUE( monadicSum( maybe(5) , maybe(7) ).get(0) == 12 );
ASSERT_TRUE( monadicSum( maybe(), maybe(1) ).hasValue == false);

```

## Why Eithers? - Learned Lessons About Error handling

Some useful references:
Some useful references:

- Benchmarks: [Mongrel Monads, Dirty, Dirty, Dirty - Niall Douglas [ACCU 2017]](https://youtu.be/XVofgKH-uu4?t=1h)

Expand All @@ -129,31 +127,31 @@ Some useful references:
- Error codes break composition
- requires out-parameters; making functions impure and hard to reason about
- using out-parameters makes inlining harder
- => don't use output parameters
- => don't use output parameters
- Exceptions are 2-3 orders of magnitude slower if exceptions are thrown
- => avoid throwing exceptions - not always possible
- Overhead of exceptions grows linear with the callstack
- => catch exceptions early
- Exceptions are not part of the type-system
- annotating function signatures with `throw` and `noexcept` is not helpful;
- annotating function signatures with `throw` and `noexcept` is not helpful;
contract breaches are not detected in compile-time but call `std::terminate` in run-time
- handling exceptions is error prone and requires documentation
- => encode errors in the types to enforce propper handling by the API consumer
- => encode errors in the types to enforce proper handling by the API consumer

## Installation

This library requires a C++ 14 compiler.

Install with [Buckaroo](https://buckaroo.pm):
Install with [Buckaroo](https://buckaroo.pm):

```
buckaroo install loopperfect/neither
buckaroo add github.com/loopperfect/neither
```

The [Buck](https://www.buckbuild.com) target is `:neither`
The [Buck](https://www.buckbuild.com) target is `:neither`

Alternatively you can copy & paste the headers to your include path:

Alternatively you can copy & paste the headers to your include path:
```
cp neither/include/*.hpp $InstallPath/include/neither
```

<a target='_blank' rel='nofollow' href='https://app.codesponsor.io/link/hq7GXSxnYW3qEM98fqMVob9v/LoopPerfect/neither'> <img alt='Sponsor' width='888' height='68' src='https://app.codesponsor.io/embed/hq7GXSxnYW3qEM98fqMVob9v/LoopPerfect/neither.svg' /></a>
6 changes: 0 additions & 6 deletions buckaroo.json

This file was deleted.

13 changes: 0 additions & 13 deletions buckaroo.lock.json

This file was deleted.

9 changes: 9 additions & 0 deletions buckaroo.lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
manifest = "eaaa6562a921b2d40d31aed0f4b442994cb5a3135397b66bbff0d1fd868b4abc"

[[dependency]]
package = "github.com/buckaroo-pm/google-googletest"
target = "//:googletest"

[lock."github.com/buckaroo-pm/google-googletest"]
versions = [ "branch=master" ]
revision = "cb8d2c1a2fcd344953e6c129a1a699a2c230551d"
6 changes: 6 additions & 0 deletions buckaroo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
targets = [ "//:neither" ]

[[dependency]]
package = "github.com/buckaroo-pm/google-googletest"
version = "branch=master"
private = true
Loading