Wednesday, October 10, 2018

"1984" Quote applicable in today's programming world

You may have heard of this famous 1984 novel with these quotes:
war is peace
freedom is slavery
ignorance is strength

This are remarkable quotes in that world because these are totally opposite in the logical world but not in the brainwashed world... and are we in such world? See https://www.enotes.com/homework-help/novel-1984-what-do-3-slogans-mean-201861 for more explanation.

Now this world is totally applicable with this javascript thing called "redux".

Don't be fooled by all the fancy talk about reducers, middleware, store enhancers—Redux is incredibly simple.
See https://redux.js.org/basics. This is incredibly verbose ugly syntax and wacky terminology and you call it simple. This idiot reducer isn't reducing anything. Revolt against tyranny is what people should do!

Transforming XML

Suppose you have an XML file with your favorite data, such as your CD collection
<catalog>
  <cd>
    <title>Greatest Hits</title>
    <artist>John Doe</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Favorite Songs</title>
    <artist>Mary Doe</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>

</catalog>
How do you change it to the following in HTML?

My CD Collection

Title Artist
Greatest Hits John Doe
Favorite Songs Mary Doe

Sure you can write a little program,in java, for example, to use one of the many fairly-difficult-to-use XML library to turn it into an object then rewrite it in html. But there is an perhaps easier way. You can just transform the XML into something else, like HTML. This is actually a very old problem, and it is called XSLT... That actually is tricky to get right, and hard to come up with entirely by hand. Gotta give credit for those who put good tutorials. https://www.w3schools.com/xml/xsl_intro.asp.

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.

Thursday, May 24, 2018

School Shooting Weapons

In the Parkland school shooting people out-cried about the powerful automatic weapons used. and in the most recent Santa Fe shooting... only REGULAR weapons were used. So even gun control people are quiet this time. Everyone seems to be ok with shotguns and handguns. Here we go another cycle of thoughts-and-prayers-nothing-done and next guy will do this somewhere. Now this guy does not even have any criminal records or other serious warnings.

But look. Even the shotgun are not so ok. Imagine if this kid has more powerful weapons. The number of deaths would be higher. Learn something from other countries. Imagine how many he can kill with ninja stars or even a sword before stopped by a police with a legitimate gun.

And look at what suggestions people have: arm the teachers! or less doors!

All idiot suggestions. If people have guns they will use it. The logic is this: only thing to stop a bad guy with a gun is good guy with a gun. Let's stop the guy with a gun part. But nope, not in America. Impossible to repeal the 2nd amendment.

Who will be next victim?

Friday, April 27, 2018

Korean war finally ending

North and South Korea talk and the war is finally coming to an end. If you were born during the start of that war, you are probably a granddaddy or grandmom now. It was 65 years ago! Even Trump was a kid back then.

Read about it: https://www.cnn.com/2018/04/27/asia/korean-summit-intl/index.html

This. is. huge.

After nuke testing here and there why does this peace come so easily? Oh it needs to involve China and US too... Most people have forgotten about this terrible conflict. I really wonder why the north suddenly change. Peace! Posterity! Perhaps it will go so far that both Koreas merge back?

Thursday, April 26, 2018

Incredible geeky pi quiz

Wow I came across a recite pi digit quiz: https://www.piday.org/pi-quiz/ ..and a site dedicated to pi day.

Did I do well? No I didn't. I just remembered 8 digits, what your typical calculator will give you. It is incredible that there are so many people bothered to remember more than that. To remember a few is enough: 3.1416. That's it. Don't need to go any further.

Delighted to see a joke page dedicated to pi. Wish the site say a bit more about Learn About π.

Wednesday, March 14, 2018

Goodbye Stephen Hawking on Pi Day

Stephen Hawking the great scientist passed away at 76. Read about it.

Good bye sir. You are THE most well known modern day scientist since Einstein. You have beats the odd: the doctors say you can't survive 2 years after diagnosed with that neuron disease that binded you to the wheel chairs. You probably outlived that doctor. Though you can't speak or move but fortunately there are machines that help you write out all the amazing works.

The "Brief History of Time" is such classic. I am glad to have read it in college. Will definitely buy another copy, the annotated version to read it at least once more. Being brilliant is one thing, being brilliant and still able to communicate to layman is another.

That "On the Shoulder of Giants" and "God Created the Integers" perhaps I will get one day.. when I have a big bookshelf.

Now to the world: Do not forget what he warned: AI can destroy the world!.

Now about π day. It is unfortunate I didn't learn another thing about π lately. If I say Happy Pi Day on facebook or something arrows shoot at me saying eeew you geek and nerdy! To those people I'll throw pie at their face. π These bozos don't even have a clue what π is.

Wednesday, February 14, 2018

The most useless sign

Boom! A Chicago Police Commander got killed in the Thompson Center, just a few blocks from where I work.

See news https://chicago.suntimes.com/news/person-shot-near-thompson-center.

But that place is full of this useless sign in every door:

Why do we even post such useless signs? It is not like a no-smoking sign where you can just butt our your cigarette before entering.

Now the criminal in custody is just not named. Look at this guy, in jailed for many years for various felonies... still unable to change him. Bad guy remain bad guy. Should there be capital punishment on repeated offenders?

"In Thompson Center" is as specific as you get in any media coverage. Where? In food court? Near CTA Station? A particular floor?

Prayers, mourning, what else can be done in Chicago gun violence? Nothing.

Friday, February 2, 2018

Dow drops 666

After MONTHS of rallying, the Dow has dropped 666 points today. https://www.npr.org/sections/thetwo-way/2018/02/02/582809604/dow-plummets-more-550-points.

You don't generally hear that big a drop.

Poof! a lot of that recent 401K gains are now lost.

Look at this:

The 2.6-percent drop in the Dow came as the Labor Department reported 200,000 jobs were added to the economy last month, which was stronger than expected, and the unemployment rate stayed at 4.1 percent — the lowest since 2000.

But worries about inflation grew when the report showed that average hourly wages grew 2.9 percent from a year ago — the largest increase since June 2009. Yields for 10-year Treasurys hit four-year highs Friday.

Even with good news, just "worry" will cost things to drop so much? Gee I will never ever understand this volatile market.

Whoa, my gut feeling is... after dropping that much it is going to rise again! TIME TO BUY when price is low!

Well I am not going to play with money like that.

Waita minute, look at that 1 year and 5 year chart, Dow has been growing quite a bit. 666 points is actually not a whole lot.

I am not in big panic yet.

HTML alt text

Ok back in mid 1990s I read the original HTML primer, and I learned MANY tags in just one day.

Some things are changed and most are... still the same as in first html.

Quiz: how do you show a little hint text on an image?

ALT text did you say?! You are RIGHT.

My favorite site w3schools says that too: ttps://www.w3schools.com/tags/att_img_alt.asp. But look.. IT DOESN'T WORK in today's browser like Chrome and Firefox??

Hover on that smiley face and I did not see the alt text!

Thanks for googling I am able to find the way to do this with the "title" tag. Don't remember seeing that in 1990s in that HTML Primer I read.

<img src="smiley.gif" title="Smiley face" width="42" height="42">

By the way, perfectionists say nobody should hardcode width and height nowadays.

Thursday, January 18, 2018

Amazon 2nd headquarter suspense

So now Amazon has a finalist list of 2nd headquarter and just have to keep you in suspense.

http://www.chicagotribune.com/business/ct-biz-chicago-amazon-finalist-h2q-20180118-story.html.

This is huge. If your city become 2nd headquarter the real estate market will go to the roof. Thousands of talented people will flock to that city and buy up all the apartments. This means: if you buy a crappy one now and someone will want it and you make instant money right there. Real Estate buy-sell can earn you money that takes years and years to make. But that's only when Amazon choose your city or you still have a crappy house but eventually you will probably get a buyer.

Is Chicago going to win? There are better cheaper places with less violence and also warmer. Ooh Amazon coming meaning jobs jobs jobs, well ONLY if you know a thing or two about AWS I guess. Why does Amazon give you a finalist list and not tell you result right away? It is a long while.

Tuesday, January 9, 2018

HTML input type=number

HTML5 gives you a lot more input types. Well you may be required to support browsers that does NOT support it, well that's too bad.

Suppose you want a field that you want to only limit number, like a quantity field, phone number field or a credit card number CVV field, you can you input type="number".

https://www.w3schools.com/html/html_form_input_types.asp

Well this thing will give you annoying up/down arrow. Make sense for a quantity field, but not for a phone number field ok? Good news: CSS can hide it.

Since the dawn of HTML (I think) there is a maxlength field on the input type="text" but it won't work with "number"! so you can't put a maxlength=10!

Here you can play: https://www.w3schools.com/html/tryit.asp?filename=tryhtml_input_number Ah, there is "min" and "max" property for input type=number. BUT IT DOESN'T WORK. You can type more than 5 in Chrome, for instance. Firefox give you an error when you hover but you still can type. So I can't make it min=1 max=9999999999 if I want a 10 digit max.

Fortunately some CLEVER DUDES figured out how to do this.


<style>
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
input[type="number"] {
    -moz-appearance: textfield;
}
</style>

<input name="phonenumber"
oninput="maxLengthCheck(this)"
type = "number"
maxlength = "10"
min = "1"
max = "9999999999" />

<script>

  function maxLengthCheck(object)
  {
    if (object.value.length > object.maxLength)
      object.value = object.value.slice(0, object.maxLength)
  }
</script>
Well a number here is not whole number, but it can take decimal point too, it even takes more than 1 decimal point. and that decimal point is still annoying... there are people who use keypress event to limit 0..9 only but may not work on some phones.. oh there is the pattern attribute, but does not work! https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_pattern (it took more than 3 letters for me in Chrome)

Book Fury

Oh wow, a book about the Trump White House has been center of attention.. and now that's Fire and Fury.

https://www.washingtonpost.com/news/post-politics/wp/2018/01/07/white-house-adviser-stephen-miller-calls-bannon-an-angry-vindictive-person-over-comments-in-wolff-book

Of course Trump's fans will say Fake News Fake Book... and Trump has to defend his own sanity: tweeting he is "stable genius"

Now, this guy is a billionaire and have towers named after him. Not many people else can. Ran for president with 0 experience and still win beating all odds. Is it really his genius or voters' not-so-genius? No genius would brag about his nuclear button when the Koreas agree to talk.

Well I haven't read the book and of course Trump supporters will of course deny everything in there if anything negative about Trump. Americans now cannot tell what is true and what is false.

But denial can only get you so far... until real actual truth come in. But can truth prevail?

Ok, Democrats are now seriously looking at the 25th amendment to remove Trump and find a way to remove him through impeachment. Forget it! Not until the mid term election give you more democrats.

Now, if D > R, you get Obamacare, if R > D, you get endless attempts to try repealing of it. if D > R, tax the rich guys! if R > D, tax relief the rich guys!

Is cycling back and forth a good way to run a country? I guess still better than dictatorship.

This certainly is an interesting chapter in American history. An even more interesting chapter if Oprah runs and wins in 2020.