Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Jan 31 21:44

    danieldietrich on v1.0.0

    Fixes malformed .travis.yml (#2… (compare)

  • Jan 31 21:44
    danieldietrich closed #2369
  • Jan 31 21:31
    codecov-io commented #2369
  • Jan 31 21:31
    codecov-io commented #2369
  • Jan 31 21:31
    codecov-io commented #2369
  • Jan 31 21:18

    danieldietrich on v1.0.0

    Simplification (#2367) (compare)

  • Jan 31 21:18
    danieldietrich closed #2367
  • Jan 31 21:18
    danieldietrich closed #2368
  • Jan 31 21:18
    danieldietrich labeled #2368
  • Jan 31 21:14
    danieldietrich labeled #2369
  • Jan 31 21:14
    danieldietrich labeled #2369
  • Jan 31 21:14
    danieldietrich milestoned #2369
  • Jan 31 21:14
    danieldietrich opened #2369
  • Jan 31 21:14

    danieldietrich on travis-yml-fix

    Fixes malformed .travis.yml (compare)

  • Jan 31 21:10
    codecov-io commented #2368
  • Jan 31 21:10
    codecov-io commented #2368
  • Jan 31 21:10
    codecov-io commented #2368
  • Jan 31 21:10
    codecov-io commented #2368
  • Jan 31 21:10
    codecov-io commented #2368
  • Jan 31 20:51
    danieldietrich opened #2368
Sébastien Varlet
@svarlet
I'm a bit confused as to how it affects a custom generator like this one:
Arbitrary<Long> validAmounts = Gen.frequency(newArrayList(
                Tuple.of(33, Gen.of(0L)),
                Tuple.of(67, Gen.choose(1, Long.MAX_VALUE))
        )).arbitrary();
what would be the result of using .check(100, 1000) ? it sounds like it should tell the underlying generator to generate Long values between -100 and 100 but my custom generator is supposed to generate number either equal to 0 or between 1 and Long.MAX_VALUE
:confused:
Daniel Dietrich
@danieldietrich

Hi,

Gen is random -> T, Arbitrary is size -> Gen, which is size -> random -> T.

Gen.arbitrary() creates a 'default' Arbitrary instance that ignores the size argument.

You could do the following:

Arbitrary<Long> validAmounts = size -> Gen.frequency(newArrayList(
                Tuple.of(33, Gen.of(0L)),
                Tuple.of(67, size)
        ));

(Note: This was from the top of my head, haven't tested it)

/cc @svarlet
Mhh, no, size needs to be 'wrapped' in a Gen instance...
Daniel Dietrich
@danieldietrich

This is the way to go:

Arbitrary<Long> validAmounts = size -> Gen.frequency(
        Tuple.of(33, Gen.of(0L)),
        Tuple.of(67, Gen.choose(0L, (long) size))
);

Property.def("test").forAll(validAmounts).suchThat(longVal -> true).check(/*size*/100, /*tries*/1000);

However, the Arbitrary size parameter is of type int, if you want to expand it to long space you have to multiply the size in the Gen.choose argument.

Sébastien Varlet
@svarlet
Ok thanks I finally understand
it was a long time confusion
Daniel Dietrich
@danieldietrich
👍🏼
Sébastien Varlet
@svarlet
Is there a way to do conditional pattern matching? For example, given 3 ints x y and z, I want to return "foo" when x belongs to a list of ints and if z > y.
Daniel Dietrich
@danieldietrich
Currently not. There exists an issue - we will implement it: #1220
The syntax isn't decided, yet.
Sébastien Varlet
@svarlet
I can't find where the then() function is defined
I see it in examples from the user guide, but couldn't find it in the javadoc or source
Daniel Dietrich
@danieldietrich
It might be that the doc is outdated. Could you point me to the example please? @svarlet
Sébastien Varlet
@svarlet
@danieldietrich yes, user guide section 3.2.3 and 3.2.4 about partial application and currying
then(add2.apply(4)).isEqualTo(6);
Israel Fernández
@iferca
Hi! Is there a Predicate to check if an Iterable contains or does not contains an element? Need to decide what is the next event in a flow based on the events that have taken place so far and I’m planning to use Match for that. Any hint? Thanks in advance!
Nándor Előd Fekete
@nfekete
@iferca to go from Java's Iterable to a Javaslang Traversable, use javaslang.collection.Stream.ofAll(Iterable). This way you can use Traversable.find(), Stream being Traversable. Going from Iterable to Stream should be cheap as Stream is lazy.
Israel Fernández
@iferca
@nfekete thanks! With that I can create predicates easily, still thinking​ on using Match
Nándor Előd Fekete
@nfekete
Stream.ofAll(iterable).find(predicate) seems quite straightforward to me, I'd surely use that :)
Daniel Dietrich
@danieldietrich
@iferca @nfekete Iterator.ofAll(iterable).find(predicate) is better in terms of memory overhead for one-shot computations. If your Iterator is a Java collections you may also use collection.stream().filter(predicate).findFirst().
Israel Fernández
@iferca
@danieldietrich thanks!
Nándor Előd Fekete
@nfekete
@danieldietrich regarding http://blog.javaslang.io/integrating-partial-functions-into-javaslang/
I believe the Function and PartialFunction are not compatible from the point of view of Liskov substitution principle, so I think the solution 5 would be the correct one to implement. They would, if PartialFunction would be at the top of the hierarchy, but you can't do that with j.u.l.Function being part of the hierarchy.
Daniel Dietrich
@danieldietrich
@nfekete yes, you are right. Variant 1-4 are unsafe
Mark Elston
@melston
Liskov substitution is an important aspect here since one of the appeals of FP is that if the types line up the code is likely to be correct. In other words, you should be able to rely on type safety to prevent whole classes of bugs. Violating Liskov makes that assumption false.
Daniel Dietrich
@danieldietrich

@melston yes, that is true. We should not violate the Liskov Substitution Principle (LSP) and we need strong behavioral subtyping. As @nfekete mentioned, variant 1 of the blog post would be an example of correct strong behavioral substyping - if there only were PartialFunction and Function1. All variants suffer from j.u.f.Function being on top of the type hierarchy. So PartialFunction has to be on its own. Especially we would violate LSP if PartialFunction would extend Function1 (variant 3).

I think I will update the post and mention the LSP. It is the key to understand why the problems arise. Thank you both!!

Rikard Andersson
@rikardev
Any idea on how to add a ninth variable to a Validation?
Daniel Dietrich
@danieldietrich
@rikardNL I'm afraid, I think this is only possible with wrapping variables in a separate Bean. We wanted to increase the limit of 8 to something greater (e.g. 22) but our javaslang-gwt blew up, so we currently stay with the limit of 8 (Tuple8, Function8, ...)
Nándor Előd Fekete
@nfekete
is code readable anyhow with anything having 8 arguments or wrapping them?
Daniel Dietrich
@danieldietrich
yes, the only other solution would be to use 'generic tuples' (aka HList) and curried functions instead of FunctionN.
But this would also not be readable :-/
Nándor Előd Fekete
@nfekete
didn't dive deep into HList, but is it actually doable in Java? or it's missing the language constructs for it
Rikard Andersson
@rikardev
Ok, thanks. Wrapping for now :)
Daniel Dietrich
@danieldietrich
@nfekete I think it is doable but not practical (there exists an issue, maybe closed...)
@rikardNL thank you
Israel Fernández
@iferca
Is there a way to add more than 8 validators to the combine method in the validation API?
On the other hand, is this API suitable to implement expensive validations? Are the validations executed synchronously?
Daniel Dietrich
@danieldietrich

Hi @iferca, the question arose two days ago (see above). I wrote:

I'm afraid, I think this is only possible with wrapping variables in a separate Bean. We wanted to increase the limit of 8 to something greater (e.g. 22) but our javaslang-gwt blew up, so we currently stay with the limit of 8 (Tuple8, Function8, ...)

The concept of deferred computation is built into two types: Lazy (for lazy evaluation) and Future (for asynchronous computation). I.e. Validation does work synchronously.

Todo: We should find a way to go beyond the limit of 8 variables by investigating how to build Validator hierarchies...

Israel Fernández
@iferca
@danieldietrich actually when a teammate mentioned the limit I was expecting to see the 9th implementation that receives an iterable. Why not do that instead of increase up to a given number?
Daniel Dietrich
@danieldietrich

@iferca An Iterable has element of one type T. But generally we have inputs of different types T1, T2, T3, ... The only solution is to use a heterogenous list (HList), see #237.

But I'm afraid - that approach does not seem to be practicable:

    // A lot of type annotation
    final HAppend<HNil, HCons<Double, HCons<String, HCons<Integer[], HNil>>>,
      HCons<Double, HCons<String, HCons<Integer[], HNil>>>> zero = append();
    final HAppend<HCons<Boolean, HNil>, HCons<Double, HCons<String, HCons<Integer[], HNil>>>,
      HCons<Boolean, HCons<Double, HCons<String, HCons<Integer[], HNil>>>>> one = append(zero);
    final HAppend<HCons<Integer, HCons<Boolean, HNil>>, HCons<Double, HCons<String, HCons<Integer[], HNil>>>,
      HCons<Integer, HCons<Boolean, HCons<Double, HCons<String, HCons<Integer[], HNil>>>>>> two = append(one);
    final HAppend<HCons<String, HCons<Integer, HCons<Boolean, HNil>>>,
      HCons<Double, HCons<String, HCons<Integer[], HNil>>>,
      HCons<String, HCons<Integer, HCons<Boolean, HCons<Double, HCons<String, HCons<Integer[], HNil>>>>>>>
      three = append(two);
Israel Fernández
@iferca
@danieldietrich you are right, I was just thinking in my example where I have only T. Thanks
Daniel Dietrich
@danieldietrich
@iferca I will work on a better solution but first I have to ship 2.0.6 and 2.1.0. So much to do (but also so much fun!)
Israel Fernández
@iferca
From development side I'm welcoming already all your improvements, javaslang provides with a development mode that greatly simply the code. So fun is shared!
Hopefully will find some time and try to contribute so I can learn more, just getting my neurons re-accommodating to think more functional 😋
Daniel Dietrich
@danieldietrich
@iferca Great to hear - contributions are welcome!
Chinmay Naik
@chinmay185
Hello guys, does javaslang Option support something like ifAbsent?
For ex.
Option.of(null).ifAbsent(()-> System.out.println(“got nothing”););
Chinmay Naik
@chinmay185

yes. so isEmpty() returns a boolean and then I’ll have to write something like this

if (Option.of(null).isEmpty()) {
    System.out.println(“got nothing”);
}

I am wondering if there is a way to pass the stuff that I am doing in the if as a block.. Java8 Optionalallows me to something like this when the optional value is present. For ex. Optional.of(42).ifPresent(() -> System.out.println(“got something”);

I am looking for a method that complements the ifPresent() method of Java8 Optional. @Ramblurr