Lazy streams and their terminal operations.

Okay, I have been bending over backwords trying to use streams inappropriately. So I found this issue about using DoubleStream#max and DoubleStream#min, they are both terminal operations.
For example lets say we have a bunch of Rectangle2D’s and we want to make a histogram of the widths. To do this we have to find the min, the max, create some bins, and iterate over the rectangles to place each one in its respective bin.
import javafx.geometry.Rectangle2D;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
/**
 * Created by odinsbane on 3/25/15.
 */
public class StreamCheck {
    /**
     * Broken version of making a histogram of rectangle widths.
     * @param rectangles collection of rectangles that will be histogrammed.
     * @return a list of bin, count pairs.
     */
    static List<double[]> broken(List<Rectangle2D> rectangles){
        double[] extrema = new double[]{-Double.MAX_VALUE, Double.MAX_VALUE};
        //since I cannot use both widths.max() and widths.min(), I tried to get
        //these values on the mapToDouble loop.
        DoubleStream widths = rectangles.stream().mapToDouble((rect)->{
            double w = rect.getWidth();
            extrema[0] = w>extrema[0]?w:extrema[0];
            extrema[1] = w<extrema[1]?w:extrema[1];
            return w;
        });
        /*
         * This is broken, because min and max have not been set yet.
         */
        int bins = 20;
        double min = extrema[1];
        double max = extrema[0];
        double delta = (max - min)/bins;
        System.out.printf("before %2.2f\t%2.2f\n",extrema[0], extrema[1]);
        List<double[]> op = IntStream.range(0, bins).mapToObj(
                (i)->new double[]{(i+0.5)*delta + min, 0}
        ).collect(Collectors.toCollection(ArrayList::new));
        //put the data in the bins.
        widths.forEach((d)->{
            int dex = (int)((d - min)/delta);
            dex = dex==op.size()?dex-1:dex;
            op.get(dex)[1] = op.get(dex)[1]+1;
        });
        System.out.printf("after %2.2f\t%2.2f\n", extrema[0], extrema[1]);
        return op;
    }
    /**
     * Fixed by using a for each to make the histogram of rectangle widths.
     * @param rectangles
     * @return a list of bin, count pairs.
     */
    static List<double[]> fixed(List<Rectangle2D> rectangles){
        //input is List<Rectangle2D> rectangles.
        double[] extrema = new double[]{-Double.MAX_VALUE, Double.MAX_VALUE};
        rectangles.stream().forEach((rect)->{
            double w = rect.getWidth();
            extrema[0] = w>extrema[0]?w:extrema[0];
            extrema[1] = w<extrema[1]?w:extrema[1];
        });
        DoubleStream widths = rectangles.stream().mapToDouble(Rectangle2D::getWidth);
        int bins = 20;
        double min = extrema[1];
        double max = extrema[0];
        double delta = (max - min)/bins;
        System.out.printf("before %2.2f\t%2.2f\n",extrema[0], extrema[1]);
        List<double[]> op = IntStream.range(0, bins).mapToObj(
                (i)->new double[]{(i+0.5)*delta + min, 0}
        ).collect(Collectors.toCollection(ArrayList::new));
        //put the data in the bins.
        widths.forEach((d)->{
            int dex = (int)((d - min)/delta);
            dex = dex==op.size()?dex-1:dex;
            op.get(dex)[1] = op.get(dex)[1]+1;
        });
        System.out.printf("after %2.2f\t%2.2f\n", extrema[0], extrema[1]);
        return op;
    }
    static Rectangle2D random(){
        return new Rectangle2D(Math.random(), Math.random(), Math.random(), Math.random());
    }
    public static void main(String[] args){
        List<Rectangle2D> rectangles = new ArrayList<>();
        IntStream.range(0,100).forEach((i)->rectangles.add(random()));
        broken(rectangles);
        fixed(rectangles);
    }
}

Since mapToDouble is an intermediate operation it happens lazily and does not get executed until the .forEach is called later on. So I had to use a terminal operation, in the fixed version a forEach is used.

Uncle Bob: "Make the Magic go away"

Bob Martin, also known in the industry as “Uncle Bob,” wrote a blog post entitled “Make the Magic go away,” which focuses mainly on whether you need a framework, and some minor thoughts about how you might judge.
It’s an interesting article. It uses rxJava as a launching point for thought, using Bob’s own history in the industry as observation (of the Observation Pattern, of all things).
There are some lightning-rod statements:

The authors of rxJava, and of Spring, and JSF, and JPA, and Struts, and [put your favorite framework here] are all searching for the same thing. These frameworks are born out of frustration with the language; and are an attempt to improve upon that language.
Every framework you’ve ever seen is really just an echo of this statement:
My language sucks!
And so we write frameworks to compensate for the lack of features that we wish were in our language.

It’s fair to say that this is hyperbole (and Bob knows it) – a point exaggerated

Using packr to bundle an OS X JRE, from linux

(by Eron Gjoni, rufsketch1#gmail.com)
packr is a useful little program that allows you to include a copy of the JRE along with your runnable JAR file. Making it much easier for your users to just download and run your program, without them having to worry about java versions or installation procedures. You can learn to use packr from the documentations available on its github page
https://github.com/libgdx/packr
Notably, however, packr requires platform specific copies of the JDK in order to bundle a JRE with your program. This is fine if you have easy access to each of the platforms you wish to support, or if you intend to use openJDK. But what do you do if you only have linux, but want to create a self contained runnable for OS X users? Oracle only provides the OSX JDK as a dmg. Luckily, we can still access the files we need!
Download a copy of the Java SE JDK for OS X from Oracle.
use 7zip to extract the contents of the .dmg.

7z x jdx-[version number]-x64.dmg -oosxJDK

cd into the osxJDK subdirectory created by the command above.
You’ll see a number of files that were extracted from the dmg.
We’re interested in the contents of the file ending in .hfs.
To access its contents, first make sure you have the hfsplus module active.

sudo modprobe hfsplus

Then, create a subdirectory into which to mount the .hfs file.

mkdir mountedHFS

Then, mount the file

mount -oloop 4.hfs mountedHFS

If you cd into the mountedHFS directory, you will find a .pkg file. Move it to the parent directory and then unmount the .hfs file.

sudo mv JDK\ 8\ Update\ 131.pkg ../ && cd ../ && sudo umount mountedHFS

Next, extract the contents of the .pkg file into a new subdirectory.

7z x JDK\ 8\ Update\ 131.pkg -opkg

cd into that subdirectory

cd pkg

You will find a number of files and subdirectories. cd into the jdk180131.pkg subdirectory (replacing the version number as appropriate)

cd jdk180131.pkg

There you will find a file named Payload. We will need to extract the contents of this file using

cat Payload | gunzip -dc |cpio -i

A new subdirectory called Contents will be created. if you cd into Contents/Home you will find the files needed by packr to create a self-contained bundle for OSX.