Functional Java Plus

Java adaptations of concepts from other functional languages

License

License

Categories

Categories

Java Languages
GroupId

GroupId

com.github.charithe
ArtifactId

ArtifactId

funactional-java-plus
Last Version

Last Version

0.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Functional Java Plus
Java adaptations of concepts from other functional languages
Project URL

Project URL

https://github.com/charithe/functional-java-plus
Source Code Management

Source Code Management

https://github.com/charithe/functional-java-plus

Download funactional-java-plus

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.charithe/funactional-java-plus/ -->
<dependency>
    <groupId>com.github.charithe</groupId>
    <artifactId>funactional-java-plus</artifactId>
    <version>0.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.charithe/funactional-java-plus/
implementation 'com.github.charithe:funactional-java-plus:0.0.1'
// https://jarcasting.com/artifacts/com.github.charithe/funactional-java-plus/
implementation ("com.github.charithe:funactional-java-plus:0.0.1")
'com.github.charithe:funactional-java-plus:jar:0.0.1'
<dependency org="com.github.charithe" name="funactional-java-plus" rev="0.0.1">
  <artifact name="funactional-java-plus" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.charithe', module='funactional-java-plus', version='0.0.1')
)
libraryDependencies += "com.github.charithe" % "funactional-java-plus" % "0.0.1"
[com.github.charithe/funactional-java-plus "0.0.1"]

Dependencies

compile (1)

Group / Artifact Type Version
org.functionaljava : functionaljava-java8 jar 4.3

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
org.mockito : mockito-core jar 1.10.19

Project Modules

There are no modules declared in this project.

Functional Java Plus

With the release of project Lambda, writing Java code in functional style has become much easier (although at times it feels like trying to parallel park a lorry in a busy town centre). Libraries like Functional Java supplement the (very limited) set of functional programming utilities in the Java standard library. However, at times even they feel inadequate if you have been working with proper functional languages like Scala or Haskell for a while. Hence, this is a set of utilities to make life easier for those who are forced to use Java but are suffering from Scala/Haskell/Rust/ withdrawal.

Releases are available in Maven Central

<dependency>
    <groupId>com.github.charithe</groupId>
    <artifactId>functional-java-plus</artifactId>
    <version>0.0.2</version>
</dependency>

Try

Something I dearly miss from Scala and Rust. Being able to succinctly express that a particular block of code could either succeed or throw an exception is very powerful in my opinion. Java standard library contains the Optional type but using it forces you to discard the error completely. FunctionalJava library provides an Either type which is the kind of disjoint union type you want but it is too generic. What you need is something like the Try in Scala or the Result in Rust -- where the "left" side is always an error type.

// Wrap a successful result
Try<String> mySuccess = Try.fromSuccess("I am a success");

// Wrap an exception
Try<String> myFailure = Try.fromFailure(new Exception("Something went wrong");

// Wrap a code block that could throw an exception
Try<String> wrapped = Try.doTry(() -> {
    // code that could throw an exception
});


// Check if the result is a success
if(myTryResult.isSuccess()){ //yay! }


//flatMap (map, filter, foreach, getOrElse, toOptional and many more are supported. See Javadoc and/or unit tests)
// This is a contrived example to illustrate flatMap functionality.
public Try<String> readStringFromFile(String fileName) {
    return Try.doTry(() -> new BufferedReader(new FileReader(fileName)))
            .flatMap((BufferedReader br) -> Try.doTry(br::readLine));
}

Versions

Version
0.0.1