Wednesday, July 25, 2018
More Java 8 stuff: Streams
No this isn't reading a file like file stream. It is more twist on Collections, like pipe them around
This tutorial from 2014. Whoa I live under rock.
https://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/.
People are aggressive, like to combine a bunch of things in 1 line. If an exception get thrown in middle of some of the many processes it would be harder to find out where.
Friday, July 20, 2018
Javascript 6: a first look
OMG, modern javascript has added so many things (ok, may not work on some oldie browser). Check this out http://es6-features.org
Now it even has object oriented features, modules and stuff. Even has some interesting string manipulation, and gosh much more.
Tweaked a bit of samples to illustrate some, put in a <script> tag, and watch console.
// String manipulation, and extend your lines with backquote var customer = { name: "John" } var item = { howmany: 7, product: "Candy Bar", unitprice: 5 } var message = `Hello ${customer.name}, want to buy ${item.howmany} ${item.product} for a total of ${item.howmany * item.unitprice} dollars?` console.log("message: "+message) // ------------------ // OOP for javascript class Shape { constructor (id, x, y) { this.id = id this.move(x, y) } move (x, y) { this.x = x this.y = y console.log("Shape: move to ("+x+","+y+")"); } } class Rectangle extends Shape { constructor (id, x, y, width, height) { console.log("Rectangle ctor"); super(id, x, y) this.width = width this.height = height } } class Circle extends Shape { constructor (id, x, y, radius) { console.log("Circle ctor"); super(id, x, y) this.radius = radius } } var c = new Circle(1,100,100,5); var r = new Rectangle(2,10,30,50,50);
Friday, July 13, 2018
Java 8 Lambda
Java 8 and its lambda thing is out for a long time. Functional Programmingused to be a separate discipline and just have to be incorporated in Java.
Thanks to helpful people all over the web I get a good taste of how things work now.
Here is little runnable example.
import java.util.Arrays; import java.util.Comparator; import java.util.List; public class TestingLambda { public static void main(String arg[]) { String[] nameArray = {"Johnny","Ken","Alan"}; ListLook, besides defining lambda function itself, there is now this forEach thing (no, not the same for loop thing as in Microsoft's languages). That arrow notation is gosh straight from mathematics. Look! that double colon! You may have seen that in C++. Nope not same use. So lambda let you define function on the fly, and stuff it onto sorting, for example. There is more to lambda than this little example. People just love define lots of things on the fly and stuff everything into 1 line.names = Arrays.asList(nameArray); // Using lambda expression and functional operations names.forEach((x) -> System.out.println("lambda thing: "+x)); System.out.println("-----"); System.out.println("The double colon"); // Using double colon operator in Java 8 names.forEach(System.out::println); Comparator byNameLength = (String o1, String o2) -> ( new Integer(o1.length()).compareTo( new Integer(o2.length()))); names.sort(byNameLength); System.out.println("-----"); System.out.println("sorted by name length"); // a good old way to list for (String name: names) { System.out.println(name); } } }
Subscribe to:
Posts (Atom)