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
3
votes
3 answers

Can I explicitly type an ambiguous method call and use it?

I'm sure this is answered somewhere, but I don't have good enough search terms to find it. I am using the io.vavr.control.Try package, and when I try to use getOrElseThrow method on an element in a stream of results, the method is ambiguous with the…
EMC
  • 1,560
  • 2
  • 17
  • 31
3
votes
3 answers

Java: Are Instance methods Prohibited for Domain Objects in Functional Programming?

Since Functional Programming treats Data and Behavior separately, and behavior is not supposed to mutate the the state of an Instance, does FP recommend not having instance methods at all for Domain Objects? Or should I always declare all the fields…
3
votes
2 answers

Java groupingBy: Get two (or more) groups with single Stream

If I have to generate two groups based on two different fields using Stream, this is one approach I can take: var prop1Group = beans.stream().collect(Collectors.groupingBy(Bean::getProp1)); var prop2Group =…
Gopal S Akshintala
  • 1,933
  • 1
  • 17
  • 24
3
votes
2 answers

Lazy view of java.util.Collection in Vavr

I have an existing api which uses java.util.Collection when returning values. I would like to use those values in later parts of my program with Vavr but I don't want to use the eager methods like List.ofAll (because I do not want to traverse those…
rattaman
  • 506
  • 3
  • 15
3
votes
2 answers

How to return the first exception that occurred?

There's a method called validate which as input accepts an instance of Option and a Predicate - two arguments (yeah, I know Option should be passed as an argument but this is simplified real-world scenario here. Now, if Option is empty I need to…
Opal
  • 81,889
  • 28
  • 189
  • 210
3
votes
2 answers

Extracting first defined value(if any) from sequence of suppliers of optional values

Given a sequence of Supplier> -- e.g., a list of method references -- what's the idiomatic way to get the first defined result, if any? Ideally without invoking any more suppliers after the first successful result. What I have now…
David Moles
  • 48,006
  • 27
  • 136
  • 235
3
votes
1 answer

Lock-free atomic update to immutable Map

Given a Javaslang / Vavr immutable map, and a function that updates that map: private Map myMap = HashMap.empty(); public void setBar(Foo foo, Bar bar) { myMap = myMap.put(foo, bar); } How can I ensure that two concurrent calls to…
David Moles
  • 48,006
  • 27
  • 136
  • 235
3
votes
2 answers

How to map a "Try with resources" exception type?

In a Cyclops React "Try with Resources" block, I would like to map an IOException to a custom exception type. I have tried this also with Javaslang and seems less fexible since it treats all exceptions the same. Code example: private static…
Xavier Arias
  • 162
  • 2
  • 10
3
votes
1 answer

Finding the "most" correct value using Java 8 Predicate

and thanks for checking out my question! I'm having a little trouble wrapping my head around using Streams with multiple predicates being applied in specific order. For the sake of example, please consider the following IntPredicates: …
Raudbjorn
  • 450
  • 2
  • 8
2
votes
1 answer

VAVR Match(Option).of() not working properly

I am trying to explore the the use of Vavr library within my Java application. I am trying to reduce the if-else ladder using the Vavr library's Match.of() construct. Here is a piece of code that I have written: Match(Option.of(clientId)).of( …
2
votes
1 answer

vavr add nested validations with validations combine

I have an input object as class Person { private String name; private String email; private String phone; private Address address; public static class Address { private String city; private String pincode; …
raaeusagvc
  • 71
  • 6
2
votes
2 answers

Convert List of Maps to a single Map with VAVR

I'm struggling to reduce a List of Maps to a single Map using Vavr >> to > I tried it with flatmap/reduce/merge but unfortunately with no happy end :-( Can someone provided an example how to do it with…
2
votes
1 answer

Return Either (Vavr) in rest api

I have method: @GetMapping public Either,ResponseEntity>> readAllOrderedByStatus() { var result = taskCrudService.readAllOrderedByStatus(); if (result.isLeft()) { return Either.left( …
Sampeteq
  • 123
  • 8
2
votes
1 answer

Clean way to chain multiple Try results using Vavr

In my project I often have a pattern, where I chain multiple methods that may or may not succeed. I struggle to find cleanest way to implement it using Vavr. Is there any other way to do it besides these two? Optimally something that does not…
Perkam
  • 23
  • 4
2
votes
1 answer

Vavr annotation procession tool doesn't kick-in

I am trying to replicate a simple Object Decomposition example from here. I've added the following dependencies to my project: io.vavr vavr
vasigorc
  • 882
  • 11
  • 22
1 2
3
10 11