Questions tagged [vavr]

Questions about the Vavr functional programming library (formerly known as Javaslang).

Vavr (formerly known as Javaslang) is an open-source, functional component library for Java 8+.

Vavr provides immutable collections and the necessary functions and control structures to operate on these values. Most Vavr data structures are purely functional, i.e. they are both immutable and persistent. Operations that appear to modify a purely functional data structure do not alter the underlying data, but instead create a new version (and, in Vavr, a new Java object); references to the old version remain valid.

(For efficiency, persistent data structures are generally implemented so as to share as much state between versions as possible.)

See Also:

154 questions
2
votes
1 answer

What is the difference between Vavr `Try.of` vs `Try.run`

Wanted more info on difference between javaslang Try.of() and Try.run() For example Try.of(() -> Integer.valueOf(str)).getOrElse(1) compiles fine but Try.run(() -> Integer.valueOf(str)).getOrElse(1) does not. found in the package javaslang.control…
ag157
  • 85
  • 1
  • 7
2
votes
1 answer

How to convert vavr collection to Java array?

I know about https://www.javadoc.io/doc/io.vavr/vavr/latest/io/vavr/Value.html#toJavaArray but it's deprecated. Is there a non-deprecated method that can be used?
Noel Yap
  • 18,822
  • 21
  • 92
  • 144
2
votes
1 answer

Is a method that returns a Try allowed to throw?

We're using Vavr in our project to ease exception handling. I always make sure that a method that returns a Try can never throw anything, like so: public Try someSafeMethod() { return Try.of(() -> someService.generateId()) …
Titulum
  • 9,928
  • 11
  • 41
  • 79
2
votes
3 answers

Why does vavr's Try container catches Throwable but not Exception?

I'm not an expert in java's type system and exception handling. But i found in SO that we should only catch exceptions but not throwable's. Here is the link: Difference between using Throwable and Exception in a try catch In Vavr's library i found…
Alexander Kondaurov
  • 3,677
  • 5
  • 42
  • 64
2
votes
1 answer

How to log only a certain exception with vavr

So I have a method which returns an Vavr Try: public Try request() {...} request comes from a source which I cannot modify. Currently, I flatmap over the result from request and depending if the Result has an error return a Try with an…
pepijno
  • 236
  • 1
  • 8
2
votes
1 answer

Javaslang - how do I join two successful Trys?

I'm still learning Javaslang/Vavr, so excuse the ignorance. The project I'm working on is stuck on Javaslang 2.1.0. My question: is there a more "Functional" way (as opposed to imperative style) to structure my code to join multiple Trys only once…
frIT
  • 3,213
  • 1
  • 18
  • 22
2
votes
1 answer

using vavr how to catch and re-throw the same exception

I am new to the functional style of programming using vavr. I have a method returns the data when its executed successfully and if it fails, it returns the MyCustomRunTimeException. In my Service class, I am calling this API method, when API method…
Madhu
  • 552
  • 2
  • 10
  • 23
2
votes
1 answer

Spring - inject vavr collections

In my Spring project I use extensively collections from vavr library. Sometimes I need to inject collection of beans. As far as I know Spring can only inject collections from JDK, e.g. List, Set, Map, etc. Is there any way to inject vavr collection?…
k13i
  • 4,011
  • 3
  • 35
  • 63
2
votes
4 answers

How to get Either left/right depending of Option value

I'm trying to return Either value depending on option value. My goal is to return Either.right() if the option is present otherwise, the code should return Either.left(). I use Java 8 and vavr 0.9.2 I want to avoid conditional imbrication public…
EFOE
  • 609
  • 1
  • 10
  • 20
2
votes
1 answer

Match on Options inside Tuple with Vavr

Using Vavr's types, I have created a pair of Somes: var input = Tuple(Some(1), Some(2)); I'd like to get at the integers 1 and 2 using Vavr's match expression; this is how I currently do it: import static io.vavr.API.*; import static…
Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
2
votes
1 answer

Vavr Set field should be volatile, atomic or declared other way?

In vavr you have the io.vavr.collection.Set that is immutable. What is a proper way and idiomatic way to use that considering that the addName() and names() could be called from various threads? import io.vavr.collection.Set; import…
raisercostin
  • 8,777
  • 5
  • 67
  • 76
2
votes
1 answer

VAVR compose Try and List

I'm trying to figure out what are the idiomatic ways of using VAVR's Try. The use case I'm looking at has to following steps: - fetch a list of shoes (the invocation can throw a checked exception) - clean each shoe (the invocation can throw a…
dgt
  • 1,002
  • 8
  • 10
2
votes
0 answers

Store io.vavr.collection.Map into MongoDB with Spring Data

I have problem with serialization and deserialization vavr Map into the MongoDB with the Spring Data. The code public class Location { public final Map region; public Location() { this.region =…
kojot
  • 1,634
  • 2
  • 16
  • 32
2
votes
1 answer

Grouping strings together in Vavr

I'm trying this code below: final Map> tuple2s = lowercase .groupBy(identity()); but this creates groups of single strings, basically like doing a #split: LinkedHashMap((lorem, List(lorem)), (ipsum, List(ipsum)),…
Cube.
  • 483
  • 6
  • 15
2
votes
3 answers

Iterate over list with indices in vavr

I'm using collections from vavr library. I have a list of elements defined like this: List integers = List.of(1, 2, 3); How to iterate over elements of the list and have an access to the indices at the same time? In Groovy there is a…
k13i
  • 4,011
  • 3
  • 35
  • 63