cyclops-integration icon indicating copy to clipboard operation
cyclops-integration copied to clipboard

How to chain operations with Try<Option>?

Open noorulhaq opened this issue 8 years ago • 6 comments

Hi,

I tried chaining below operations

OptionalTValue<String>  valueT1 = OptionalT.fromValue(Try.of(Optional.of("Noor1")));
OptionalTValue<String> valueT2 = OptionalT.fromValue(Try.failure(new RuntimeException("ERROR")));

System.out.println(valueT1.flatMapT(i-> valueT2.map(j -> i.concat(j))));

And it prints below string instead of OptionalTValue[AnyMValue[Failure]] OptionalTValue[AnyMValue[]]

Also, is there any possibility to keep the value type as below:

Try<Option<String>>  valueT1 = OptionalT.fromValue(Try.of(Optional.of("Noor1")));

noorulhaq avatar Jan 15 '17 09:01 noorulhaq

I'll take a look at this later today. V2 of cyclops-react will provide away to keep the types available (using Witness types and fluent type safe conversion methods). In v1 AnyM drops the monad type and keeps only the type of the contained value. There is an implementation of FutureT in v2 already, when the API is solid it can be expanded for other types.

I'll add these examples as tests and investigate.

johnmcclean avatar Jan 15 '17 11:01 johnmcclean

I haven't run this yet, but from the code this should return a Try.Failure wrapped in an AnyM. Looks like a bug.

johnmcclean avatar Jan 15 '17 12:01 johnmcclean

The bug seems to in in the toString() generation as the result of the flatMapT operation does seem to have the correct type. E.g. the intermediate variable v is displayed as OptionalTValue[AnyMValue[]], but when we drill down into it in the debugger we can see it has the expected type. I'll work on fixing the toString() on the 1.x branch.

Improving the type safety + simplicity of this is a big driver behind the motivation of the next big cyclops release (2.x for cyclops-react and will be 9.x for the integration modules).

screen shot 2017-01-15 at 21 13 15

In 2.x the code should look more like this

OptionalT<tryType,String>  valueT1 = Optionals.liftM(Optional.of("Noor1"),Witness.tryType.INSTANCE);
OptionalT<tryType,String> valueT2 = OptionalT.fromValue(Try.failure(new RuntimeException("ERROR")));


OptionalT<tryType,String> valueT3 = valueT1.flatMapT(i-> valueT2.map(j -> i.concat(j)));

Try<Optional<String>,Throwable> tryOpt = valueT3.unwrapTo(Witness::tryType);

But with v1 the best we can do is

OptionalTValue<String> valueT1 = OptionalT.fromValue(Try.of(Optional.of("Noor1")));
OptionalTValue<String> valueT2 = OptionalT.fromValue(Try.failure(new RuntimeException("ERROR")));

    
 Try<Optional<String>,Throwable> v = valueT1.flatMapT(i-> valueT2.map(j -> i.concat(j)))
                                                   .unwrap()
                                                   .unwrap(); 

 System.out.println(v);

Which isn't type safe (as via unwrap().unwrap() we know we have Optional<String> but not Try), but it does at least print the correct value

Try.Failure(error=java.lang.RuntimeException: ERROR)

johnmcclean avatar Jan 15 '17 21:01 johnmcclean

Thanks a million for reporting this by the way! If you find any other issues or have any questions please do log an issue or get in touch.

johnmcclean avatar Jan 15 '17 21:01 johnmcclean

No problem, it is my pleasure. You are doing a tremendous job. One more problem that I faced to get start is which version to start with. The documention probably is refering to version 1 where as version 2 seems more attractive. Considering the requirement I have in hand which involves multiple Monads (Option, Try, Reader etc) and Applicative functors like Validation, please recommend me most suitable verision. Also, I may use Project Reactor in near future.

noorulhaq avatar Jan 16 '17 04:01 noorulhaq

I think v2 is a big improvement from v1 - but most of the monad transformers have yet to be reimplemented in the new way. Also cyclops-reactor has to be reworked for v2. On balance I'd recommend going with v1 for now, but keep an eye on v2. Very good progress on v2 during the week (just not enough to recommend that you go with it over v1 at this stage).

johnmcclean avatar Jan 20 '17 22:01 johnmcclean