Utility library dedicated for functional & non-functional codebases to simplify modelling of success and failure responses for the JVM languages ๐Ÿ”€

Overview

Expressible CI codecov

Utility library, part of the panda-lang SDK, dedicated for functional codebases that require enhanced response handling. Express yourself with inspired by Rust, Kotlin and Vavr wrappers, to provide better API using this tiny library.


Supported wrappers (in panda.std.* package):

  • Result<Value, Error> - solve error handling gracfully
  • Option<Value> - enhanced alternative to standard `Optional
  • Lazy<Value> - lazy values & runners
  • Completable<Value> with Publisher & Subscriber pattern - synchronized alternative to CompletableFuture<Value>
  • Simple reactive containers - Reference<V>, MutableReference<V>, Computed
  • Mono<A>, Pair<A, B>, Triple<A, B, C>, Quad<A, B, C, D> - generic wrappers for set of values
  • Throwing functions, runnables, suppliers and consumers - set of functional interfaces with support for exception signatures
  • Tri and Quad consumers, functions and predicates - additional functional interfaces
  • PandaStream<Value> - Stream<Value> wrapper with support for features provided by expresible library

repositories {
    maven { url 'https://repo.panda-lang.org/releases' }
}

dependencies {
    implementation("org.panda-lang:expressible:1.1.9") // Core library
    implementation("org.panda-lang:expressible-kt:1.1.9") // Kotlin extensions
    implementation("org.panda-lang:expressible-kt-coroutines:1.1.9") // Kotlin coroutines extensions
    testImplementation("org.panda-lang:expressible-junit:1.1.9") // JUnit extensions
}

Examples

Suggested snippets show only a small use-cases for the available api. You're not forced to use this library this way, so you may need to find your style in expressing your thoughts. Adopting functional approach requires time and to simplify this process it's easier to slowly introduce new elements based on simple concepts.

Result

Rather than using Exception based error handling, return meaningful errors and interact with api responses gracefully. Following functional programming patterns make sure your methods don't contain side effects and unexpected exit points.

class UserFacade {
    // You can start adoption in a regular, non-functional codebases
    fun createUser(username: String): Result<User, String> {
        if (userRepository.findUserByName(username).isPresent()) {
            return error("User $username already exists")
        }
        return ok(userRepository.createUser(username))
    }
}

class UserEndpoint {
    // You can also use fully functional approach
    fun createUser(request: HttpRequest, response: HttpResponse) =
        userFacade.createUsername(request.param("username"))
            .peek { user -> response.respondWithJsonDto(user) }
            .onError { error -> ErrorReposne(BAD_REQUEST, error) }
}

internal class UserFacadeTest : UserSpec {
    // JUnit support
    @Test
    fun `should create user with a valid username` () {
        // given: a valid username
        val username = 'onlypanda'
        // when: user is created with the following name
        val user = userFacade.createUser(username)
        // then: user has been created
        assertOk(username, user.map(User::getUsername))
    }
} 

Option

Similar usage to Optional<Value> type provided by Java:

Option<String> withValue = Option.of("Value");
Option<String> empty = Option.empty();

Lazy

Lazy<String> completed = new Lazy<>("Value");
Lazy<String> lazy = new Lazy<>(() -> "Value");
Lazy<Void> initialize = Lazy.ofRunnable(() -> "Called just once);

String value = completed.get();

Completable

Completable<String> completable = Completable.create();

completable
    .thenApply(value -> parseBoolean(value))
    .then(value -> System.out.println(value));

completable.complete("true");

Reactive

Reference<Integer> a = reference(1);
MutableReference<Integer> b = mutableReference(2);
Computed<Integer> result = computed(dependencies(a, b), () -> a.get() + b.get());

result.subscribe(value -> System.out.println(value));
b.update(3); // prints "4"

Panda Stream

PandaStream<String> empty = PandaStream.empty();
PandaStream<String> standard = PandaStream.of(new ArrayList<>().stream());

Used by

You might also like...
A functional, fractional-byte programming language

Fig A functional, fractional-byte programming language. Tired of annoying Unicode codepages in your favorite golfing languages? Fig uses pure, printab

Sample Ktor app using a functional stack

samstack This is a template project you can clone and use as a basis for your own Kotlin based microservices. This application structure is my persona

Sample Ktor app using a functional stack

samstack This is a template project you can clone and use as a basis for your own Kotlin based microservices. This application structure is my persona

A creatively named utility for developing biome configurations for Terra
A creatively named utility for developing biome configurations for Terra

Biome Tool Biome Tool is a creatively named utility for developing biome configurations for Terra This is a simple dummy platform implementation that

JVM Open Asset Import Library (Assimp)

assimp JVM porting of Assimp This port is being written trying to stick as much as possible close to the C version in order to: minimize maintenance t

Collection of JVM library logic that the Sirloin software development team is currently using

Collection of JVM library logic that the Sirloin software development team is currently using

Depenject - a lightweight, minimalistic dependency injection library for Kotlin/JVM.

depenject depenject is a lightweight, minimalistic dependency injection library for Kotlin/JVM. Our goal is similar to flavor's to simplify the usage

Create libraries for all types of Kotlin projects: android, JVM, Multiplatform, Gradle plugins, and so on.

JavierSC Kotlin template Create libraries for all types of Kotlin projects: android, JVM, Multiplatform, Gradle plugins, and so on. Features Easy to p

Kotlin SDK for Jellyfin supporting Android and the JVM.

Jellyfin Kotlin SDK Part of the Jellyfin Project The Jellyfin Kotlin SDK is a library implementing the Jellyfin API to easily access servers. It is cu

Owner
Panda
Repositories related to the Panda programming language ๐Ÿ’•
Panda
StarkNet SDK for JVM languages (java, kotlin, scala)

โ˜• starknet jvm โ˜• StarkNet SDK for JVM languages: Java Kotlin Scala Clojure Groovy Table of contents Documentation Example usages Making synchronous re

Software Mansion 29 Dec 15, 2022
Spring Boot API for Modern Warfare 2 Dedicated Servers

Spring Boot API for Modern Warfare 2 Dedicated Servers

Kai o((>ฯ‰< ))o 1 Apr 24, 2022
Bukkit library written in Kotlin to make with compatibility and ease non-playable-character (NPC)

mc-npk Easy to use, fast and efficient library to make non-playable-characters (

Luiz Otรกvio 3 Aug 4, 2022
Explore-KiiT-App - An app to simplify the complicated website navigation and keep track of Attendance

KiiT Explore App "An app to simplify the complicated website navigation and keep

Ajay Khatri 17 Oct 12, 2022
This repository demonstrates how Kotlin can simplify Spring Boot configuration properties file mapping

Kotlin spring-boot nested config props This repository demonstrates how Kotlin can simplify Spring Boot configuration properties file mapping @Constru

Maksim Kostromin 1 Oct 11, 2021
Simplify the processing of sealed class/interface

shiirudo Generates DSL to simplify processing branching by when expressions in sealed class/interface. Setup Refer to the KSP quickstart guide to make

KeitaTakahashi 2 Nov 1, 2022
Automatically filled the declared non-null field is missing or null with default value.

[TOC] Moshi-kotlin-nullsafe ไธญๆ–‡็‰ˆ 1. moshi-kotlin moshi-kotlin support kotlin type safe check. When parsing json, fields declared as non-null types may

null 4 Oct 26, 2022
Fully customizable, built from scratch NumberPicker for android. Created as an alternative to non-customizable native android NumberPicker

GoodNumberPicker GoodPicker is an Android library that provides a picker with customizable UI. It was developed as alternative to the default NumberPi

null 3 Nov 30, 2022
A Gradle plugin providing various utility methods and common code required to set up multi-version Minecraft mods.

Essential Gradle Toolkit A Gradle plugin providing various utility methods and common code required to set up multi-version Minecraft mods via archite

Essential 29 Nov 1, 2022
Showcase project of Functional Reactive Programming on Android, using RxJava.

FunctionalAndroidReference FunctionalAndroidReference is a showcase project of Functional Reactive Programming on Android, using RxJava. It's a compan

Paco 278 Nov 18, 2022