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
5
votes
1 answer

How to catch a specific runtime exception using Vavr (formerly known as Javaslang) library?

I am trying to catch a specific runtime exception (so not throwable) and just log it (log.error has a void return type). What is the simplest way to do this in vavr? try { sayHello(); } catch (MyAppRuntimeException ex) { log.error("Error…
user1870400
  • 6,028
  • 13
  • 54
  • 115
4
votes
1 answer

Volatile variable read behavior

I have encountered the following code when reading Vavr's Lazy source code: private transient volatile Supplier supplier; private T value; // will behave as a volatile in reality, because a supplier volatile read will update all fields…
Martin
  • 129
  • 1
  • 6
4
votes
2 answers

Trying to convert a Try> to a Try> using Java Vavr

I'm able to "solve" this with the following (ugly and smelly) code: List messages = messagesRepository.findAll().get(); return Try.success( messages .stream() .map(m -> new…
Marcio Frayze
  • 43
  • 1
  • 5
4
votes
1 answer

What's the difference b/w foldLeft and Java collect

This question is targeted to Vavr (Java's FP library), but could probably apply to Scala or other FP languages that can compare to Java. What's the difference between Vavr foldLeft: U foldLeft(U zero, BiFunction
Gerard Bosch
  • 648
  • 1
  • 7
  • 18
4
votes
1 answer

How to pattern match a tuple of Option with defined type in Java with Vavr

new Vavr user here. I am trying to pattern match a tuple of options to execute a statements if both of them are Some, in Scala I would have done this with: val maybeThis: Option[String] = ??? val maybeThat: Option[String] = ??? (maybeThis,…
4
votes
1 answer

Type inference seems to fail vavr's Try works on jOOQ's fetchOne() function

I am using vavr and jOOQ, two fantastic libraries to have come out in the recent times, allowing us to use functional dialects in regular Java server applications. I am trying to get using jOOQ, what is equivalent of SQL's select count (*). The…
Nirmalya
  • 717
  • 9
  • 19
4
votes
4 answers

if-logic elimination from map call

I often see in vavr-based code: ... .map(x -> { if (someCondition(x)) { return doWith(x); } else { return x; } }) ... Is there any way to eliminate this logic from map call using some constructs? I find this if condition…
Opal
  • 81,889
  • 28
  • 189
  • 210
4
votes
2 answers

How to correctly use VAVR collections to be thread safe?

VAVR collections are "immutable". So, if I have static variable, for example, holding all the WebSocket sessions, how would I use VAVR so that the collection is thread safe? For example: @ServerEndpoint("/actions") public class DeviceWebSocketServer…
Rafael Paulino
  • 570
  • 2
  • 9
4
votes
2 answers

Vavr: Howto flatmap collection inside optional object

Is any easiest way to write this code below, without using toStream()? import io.vavr.collection.List; import io.vavr.control.Option; import lombok.Value; public class VavrDemo { public static void main(String[] args) { Foo bar = new…
MariuszS
  • 30,646
  • 12
  • 114
  • 155
4
votes
2 answers

Javaslang / Vavr LinkedHashMap from List

Is there a concise, idiomatic way to create an order-preserving map from a Javaslang / Vavr List? List.toMap() returns a plain HashMap, so that doesn't do it. What I have right now is something like this -- listOfKeys.foldLeft( …
David Moles
  • 48,006
  • 27
  • 136
  • 235
3
votes
2 answers

how to fix the second parameter of a function with vavr?

Suppose I have a function which takes two parameter. Function2 function; I want to fix the second parameter and makes it a Function1. With Function2.apply(T1 t), I can only fix the first parameter, is there a way to fix the second…
haoyu wang
  • 1,241
  • 4
  • 17
3
votes
3 answers

Exception handling in streams with Either

Background I have been fascinated with Scott WLaschin's Railway Oriented Programming model of handling exceptions: have a side channel, where all the bad stuff will be processed, and keep the good stuff on the main track. Picture below: Problem A…
Somjit
  • 2,503
  • 5
  • 33
  • 60
3
votes
1 answer

Consume both "left" and "right" of Vavr Either?

How can I consume both a "left" or a "right" of a vavr Either in a functional way? I have a method that returns an Either. Based on this result, I need to execute a callback to our reporting library, e.g. reportSuccess() or…
matsev
  • 32,104
  • 16
  • 121
  • 156
3
votes
1 answer

OpenCSV Header is missing required fields found []

opencsv 5.1 Caused by: com.opencsv.exceptions.CsvRequiredFieldEmptyException: Header is missing required fields [ALGVERIFICATION, DISTAL MV, LOCATION, PREDICTED STATE, PROXIMAL MV, RUN, SAMPLE TIME]. The list of headers encountered is []. at…
xenoterracide
  • 16,274
  • 24
  • 118
  • 243
3
votes
1 answer

How to flatMap vavr Either with left variance annotated

My code open class Fail(override val message: String, override val cause: Throwable?) : RuntimeException(message, cause) data class ValidationFail(override val message: String, override val cause: Throwable?) : Fail(message, cause) more fails will…
1
2
3
10 11