Tuesday, October 9, 2018

Think you know Javascript?

Javascript are now way more than little functions that let you change something on your page. What happened to its once simple syntax. It used to be simple and free you from variable types and semicolons.

Here are some never things I never heard of before... gee how are you supposed to keep up with endless changes.

Look at this dot-dot-dot thing that turns an array into individual argument: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

What the heck is this function * thing? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function*.

oh and you can have a void operator?

Wednesday, September 5, 2018

5th Android Phone

Expect you need a new phone every 2 years or so. My Moto G4 phone is basically dead. I had it for two years. It was supposed to be best budget phone 2 years ago. Things need to last longer.

Besides RIDICULOUSLY POOR photo quality this phone works great. Until it can't receive phone calls. If it rings you pick it up you freeze the phone. The power switch already came off for months and I had to use a toothpick to reset. Without the unique shake-it-to-use-camera and chop-it-twice-to-use-flashlight feature basically the phone can't turn on. And sound is totally gone. Can't hear anything through speaker or headphone. This is it. time for a new phone. It still can be used as a silent web server and silent games such as chess.

As a never-iphoner, I will need to get Android. and now a never Motorola-er. I need something else. I deserve a good phone. Latest Samsung Galaxy S9 ($720) and Pixel 2 ($650) were my finalists, both at whooping cost. But I've had it with budget phones... and I don't want a China brand. Now given the iphones go up to $1000 both are cheap. My usual formula is when you have both phones that are good you go to the cheaper one. Now you have to play with each at a store. All Android phones are basically similar. Eew why phones are so heavy now. That Moto G4 has the right weight... Both finalists are good. but oh, the Pixel does not have a phone jack! Call me old school but I prefer stick a headphone in than mess with bluetooth! And oh, Amazon slashes $100 off the Samsung so it is now the cheaper option with headphones! Boom! bought the Galaxy 9. I do NOT want the plus size. Phone need to e SMALL!

Screens need to be rectangle. don't dent the good rectangle with that camera. Another reason I am a never iphoner. I don't care for curved sides-- absolutely useless, but I can live with it.

Ok, the S9 came. Of course fragile things demand screen protector and bumper plastic. Ka-ching, more money needed. It's got its own assistant called the Bixby which has its own button to summon! I need no Siri, no Cortana, no Bixby, no Ok Google. I don't ever need voice activated things: no Amazon Echo. No Google Home ever needed.

So yes, the camera is better (but still a bit not so natural color).

Hope this will last for a few years.

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"};
        List 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);
        }
    }
}
Look, 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.