Tuesday, August 26, 2014

A Snowball


It was a cold winter evening. I was sitting on work next to my laptop and working In our team we had many talented persons but in that evening I had 2 extremely talented: a product manager and a data scientist – a Phd guy.I was working on some task then PM just asked me when I predict the feature will be done. I started thinking. Then a Phd guy joined the discussion and started explaining how complicated the task is…Actually I realized that the PM had no idea what the Phd guy was talking about. But he tried to be proactive in the discussion. And asked one more question .Well,  he made the mistake of his life… a stream of math equations had been thrown  to explain him what he was asking…and guess what! It absolutely hasn't explained the second question… Well , guess what happened then…  The misunderstanding started rolling like a snowball. After 15 minutes of such a conversation both of them were totally tired with 0 understanding of each other…But what is terrible what both of them were absolutely sure that they understand and got conclusions.
Next day the PM had some meeting where he explained all other conclusions of his yesterday’s conversation. I can summarize his explanation like a "total wrong " (being very gentle here!) but the worst of all is that he mentioned that the math guy told him this. On that moment the math guy was on day off and they couldn't reach him to prove or disprove it. As a result – a snowball of crap keeps rolling down… and other people start making their decision based on totally wrong things.
And the main problem is that that’s the way it works .Not only in that  company.
The snowball grows and the worst thing: it’s unpredictable. You never know when it starts and what will be its impact.


So please…when you discuss – double check you understand before you make some conclusions.  And in addition to what – double check the opponent understood your question. I know it’s annoying. But that’s the only way ….

Friday, August 22, 2014

Java 8 Lambdas - examples

Group by and counting

List<String> strings = Arrays.asList("rami", "ida", "maya", "ida", "ida", "rami");
 Map<String, Long> collect = strings.stream().collect(Collectors.groupingBy((e) ->                                                                                                       e,Collectors.counting()));


Generate random numbers

 ThreadLocalRandom.current().longs().mapToObj(Long::toHexString).limit(10).sorted()


Filter and count
 long num = strList.stream().filter(x -> x.length()> 3).count();



PartitionBy

Map<Boolean, List<String>> collect1 = Arrays.asList("rami", "ida", "maya", "ida", "ida", "rami").stream().collect(Collectors.partitioningBy((x) -> x.startsWith("r")));

Result:
{false=[ida, maya, ida, ida], true=[rami, rami]}

Thursday, August 21, 2014

Java 8 Lambdas

First of all - have you ever asked yourself a question - why i need to create a Java Interface with 1 single method?
Hell! Why to create a file if the only thing i want is to create 1 single generic function!
If you did then you are absolutely right! There are many functional languages that spinning around that idiom .

So that's exactly that Java 8 did. They just defined thats kind of Generic interfaces and main of them are :

Interface name Arguments Returns Example
Predicate<T> T boolean Has this album been released yet?
Consumer<T> T void Printing out a value
Function<T,R> T R Get the name from an Artist object
Supplier<T> None T A factory method
UnaryOperator<T> T T Logical not (!)
BinaryOperator<T> (T, T) T Multiplying two numbers (*)

But that's no the end.
In order to provide us an option to write gentle code in already existing classes of the SDK
Stream API had been added

for example on a list you can now convert into stream and the proceed
Example :

long count = allArtists.stream().filter(artist -> artist.isFrom("London")).count();

And this gives us an option writing VERY short and gentle
Now we can create functions on the fly by only writing
()->do something
and pass it to some function

Examples:

Converting Stream to list:
Stream.of("a", "b", "c").collect(Collectors.toList());

Mapping ( transforming a collection)
List<String> collected = Stream.of("a", "b", "hello")
         .map(string -> string.toUpperCase())
         .collect(toList());

Filtering
Stream.of("a", "b", "hello")
.filter(value -> isDigit(value.charAt(0)))
.collect(toList());

flatMap
Is splitting every subelement to stream
Stream.of(asList(1, 2), asList(3, 4))
.flatMap(numbers -> numbers.stream())
.collect(toList());

Max and Min
Track shortestTrack = tracks.stream().min(Comparator.comparing(track -> track.getLength())).get();

Reducing
Stream.of(1, 2, 3)
.reduce(0, (acc, element) -> acc + element);

Combining actions together
album.getMusicians()
.filter(artist -> artist.getName().startsWith("The"))
.map(artist -> artist.getNationality())
.collect(toSet());

Great article to read
http://javarevisited.blogspot.co.il/2014/02/10-example-of-lambda-expressions-in-java8.html