Skip to content

Examples for BAEL-422#2293

Merged
zhendrikse merged 2 commits intoeugenp:masterfrom
NareshBabuPB:BAEL-422-VAVR-Collections
Jul 20, 2017
Merged

Examples for BAEL-422#2293
zhendrikse merged 2 commits intoeugenp:masterfrom
NareshBabuPB:BAEL-422-VAVR-Collections

Conversation

@NareshBabuPB
Copy link
Copy Markdown
Contributor

Collection API in Vavr

assertEquals(3, queue.size());
assertEquals(5, secondQueue.size());

secondQueue.dequeue()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this structure. Isn't there an easier way of checking if all queue elements are in there? Queue is essentially an iterable so it should be doable easily

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Queue is iterable. But, I wanted to show that dequeue() function returns a Tuple2 which is different from a mutable Queue. And that there is a map() function to work on the contents of the Tuple2.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely a must-have to show those things but not this way by nesting assertions in map calls and not by nesting dequeue calls inside map calls.

If you want to show that a Tuple gets returned containing head and tail, you can start easily with:
Tuple2<Integer, Queue<Integer>> result = secondQueue.dequeue();
and then perform necessary assertions.

Next thing, if you want to show a map call, definitely do not go with names like (k,v) which suggest that this key-value relationship. Keep in mind that map always needs to return another Tuple

result.map((head, tail) -> Tuple.of(head.toString(), tail))

There is also a method that you can use for transforming the whole tuple into something that is not wrapped in the Tuple:
result.apply((head, tail) -> head.toString());

and so on... you can store results of those operations as variables and perform assertions on them.

@zhendrikse zhendrikse merged commit 4e95722 into eugenp:master Jul 20, 2017
Number sum = vavrList.map(i -> i * i)
.sum();

assertEquals(new Long(14), sum);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The boxing of Long is unnecessary


@Test
public void givenSortedSet_whenReversed_thenCorrect() {
Comparator<String> reverseCompare = (a, b) -> b.compareTo(a);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the (a, b) -> b.compareTo(a) can be replaced with Comparator.reverseOrder();

java.util.List<Integer> javaList = java.util.Arrays.asList(1, 2, 3, 4);
List<Integer> vavrList = List.ofAll(javaList);

assertTrue(vavrList instanceof io.vavr.collection.List);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those instanceof assertions do not really bring anything new here. You can easily remove them and leave this test without assertions. After all, those are just supposed to be a short main methods and not full-blown tests

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it Ok to have this example in a private method in the same test class? Just wondering if a separate main class is required just for a few lines of code.

For rest of the comments, will work on it as suggested.

@Test
public void givenMap_whenIterated_thenCorrect() {
Map<Integer, List<Integer>> map = List.rangeClosed(0, 10)
.groupBy(i -> i % 2);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the provided formatter. Continuation indents should be 2 spaces

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants