Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/d-plaindoux/hpas

Functional ADT And Asynchronous stuff in Java
https://github.com/d-plaindoux/hpas

asynchronous functional-programming monadic-interface

Last synced: 24 days ago
JSON representation

Functional ADT And Asynchronous stuff in Java

Awesome Lists containing this project

README

        

# HiPeAS

[![Codacy Badge](https://api.codacy.com/project/badge/Grade/052bbdb13acf4b35bd5bbb485f6cad25)](https://app.codacy.com/manual/d-plaindoux/hpas?utm_source=github.com&utm_medium=referral&utm_content=d-plaindoux/hpas&utm_campaign=Badge_Grade_Dashboard)
[![Build Status](https://travis-ci.org/d-plaindoux/hpas.svg?branch=master)](https://travis-ci.org/d-plaindoux/hpas)
[![Coverage Status](https://coveralls.io/repos/github/d-plaindoux/hpas/badge.svg?branch=master)](https://coveralls.io/github/d-plaindoux/hpas?branch=master)
[![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges)
[![Maven Central](https://img.shields.io/maven-central/v/org.smallibs/hpas.svg)](http://search.maven.org/#artifactdetails%7Corg.smallibs%7Chpas%7C0.5%7Cjar)

Functional paradigm deeply drives the design with a taste of OO for encapsulation and chaining methods which mimics infix
operators like Haskell monad function `>>=`.

Since such ADT provides traditional map, flapmap etc. functions for a DSL perspective are also given in order to increase the code readability.

## A taste of HiPeAS

```java
Executor executor = ExecutorHelper.create(Executors.newSingleThreadExecutor());
Promise helloWorldPromise = executor.async(() -> "Hello").and(s -> s + " world!")
```

## HiPeAS overview

### Synchronous data types

Basically well known `MayBe` and `Try` are available for this purpose. Theses ADT are also the basis for the asynchronous part
of this library.

### Asynchronous computational model

### Executors

```java
Executor executor = ExecutorHelper.create(Executors.newSingleThreadExecutor());
```

#### `async`

In Executor **<T> async :: (() -> T) → Promise<T>**

```java
Promise integerPromise = executor.async(() -> 1);
```

#### `await`

In ExecutorHelper **<T> await :: (Promise<T>, Duration) → Try<T>**

```java
Try result = ExecutorHelper.await(integerPromise, Duration.TWO_SECONDS);
```

### Promise

#### `and` or `map`

In Promise<T> **<R> map :: (T → R) → Promise<R>**

In Promise<T> **<R> and :: (T → R) → Promise<R>**

```java
integerPromise.map(i -> i + 1);
integerPromise.and(i -> i + 1);
```

#### `then` or `flatmap`

In Promise<T> **<R> flatmap :: (T → Promise<R>) → Promise<R>**

In Promise<T> **<R> then :: (T → Promise<R>) → Promise<R>**

```java
integerPromise.flatmap(i -> executor.async(() -> i + 1));
integerPromise.then(i -> executor.async(() -> i + 1));
```

#### Back to the Future

In Promise<T> **getFuture :: () → Future<T>**

```java
integerPromise.getFuture();
```

### Conclude on success

In Promise<T> **onSuccess :: (T → void) → Promise<T>**

```java
integerPromise.onSuccess(i -> System.println(i))
```

### Conclude on failure

In Promise<T> **onFailure :: (Throwable → void) → Promise<T>**

```java
integerPromise.onFailure(t -> t.printStackTrace(System.err))
```

### Conclude on complete

In Promise<T> **onComplete :: (Try<T> → void) → Promise<T>**

```java
integerPromise.onComplete(t -> t.fold(integerPromise::onSuccess, integerPromise::onFailure));
```

## Functor, Applicative and Monad

In addition monadic approach is available for each ADT. As usual `Monad` ihnerits `Applicative` which inherits `Functor`.

### Functor

In PromiseHelper **functor<T> :: Promise<T> → Functor<T>**

```java
Functor> p1 = functor(executor.async(() -> 1));
HK> p2 = p1.map(i -> i + 1);
```
### Applicative

In PromiseHelper **applicative<T> :: Promise<T> → Applicative<T>**

```java
Applicative> p1 = applicative(executor.async(() -> 1));
HK> p2 = p1.apply(functor(executor.async(() -> i -> i + 1)));
```
### Monad

In PromiseHelper **monad<T> :: Promise<T> → Monad<T>**

```java
Monad> p1 = monad(executor.async(() -> 1));
HK> p2 = p1.flatmap(i -> executor.async(() -> i + 1));
```

## CompletableFuture and Promise

In CompletableFutureHelper **completableFuture<T> :: Promise<T> → CompletableFuture<T>**

```java
Executor executor = ExecutorHelper.create(Executors.newSingleThreadExecutor());
Promise helloWorldPromise = executor.async(() -> "Hello").and(s -> s + " world!");
CompletableFuture completable = CompletableFutureHelper.completableFuture(helloWorldPromise);
```

In CompletableFutureHelper **promise<T> :: CompletableFuture<T> → Promise<T>**

```java
CompletableFuture completable = CompletableFutureHelper.supplyAsync(() -> "Hello World");
Promise helloWorldPromise = CompletableFutureHelper.promise(completable);
```

## Releases

This library is available at Sonatype OSS Repository Hosting service and can be simply used adding the following
dependency - for instance - to your pom project.

```

org.smallibs
hpas
0.11.0

```

## About the library design

The library has been designed simulating High Order Type in Java and self type thanks to F-Bounded quantification polymorphism.

For more information follow [this link](https://gist.github.com/jdegoes/6842d471e7b8849f90d5bb5644ecb3b2).

## License

Copyright (C)2016-2020 D. Plaindoux.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program; see the file COPYING. If not, write
to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
USA.