Monday, December 29, 2014

Git Tip of the Year: Find the Most Recent Common Commit

Okay, you're working on a branch called 'awesome' and you want to know what's the most recent common commit of branch 'awesome' and 'master'.

Just run this:
$ git merge-base awesome master
d931216adebf441dc7431f1a073baed8b4a1b20a
And you'll get the SHA1 of the most recent common commit.

Friday, December 26, 2014

IntelliJ Tip of The Year: Rectangular Select

Okay, you want to select a rectangular area in your favorite IDE?

If your favorite IDE is made by IntelliJ here's what you can do:
  1. Hold an  ⌥ (Option) key and do your usual select of the area.
  2. Enjoy yourself.
Here's a video:

Wednesday, December 24, 2014

Github Tip of the Year: Shortcuts for Searching

Github has two great shortcuts that can change your life.

1. Let's say you're browsing the bootstrap repository and you want to find a file named tooltip.js
Press T (yeah, that's the character T on your keyboard).
Boom!
You've activated the File Finder:



Just type tooltip and enjoy yourself:


You can exit the File Finder by pressing Escape.

2. Let's say you're browsing the bootstrap repository and you want to search for a string "awesome".
So, you need to click on a search box and start typing, right?
Nope, just type S (yeah,  that's the character S on your keyboard).
Boom!
The search box is focused now!
Just type awesome into it and press Enter.


Isn't Github awesome?

Sunday, December 21, 2014

PyCharm Tip of the Year: Switch to the Previous File

This tip applies to any IntelliJ product.

Press ⌘-E (slow motion instant replay: Command-E). The popup window containing the list of recent files will appear:


Just press Enter to switch to the previous file. In this example that'll be a file pydrill.css.

To sum it up: to switch to the previous file in IntelliJ press ⌘-E followed by Enter.

Thursday, December 18, 2014

Git Tip of the Year: Switch to the Previous Branch

Okay, you're working on a branch called 'awesome'. You switch to a branch 'master'. You merge 'awesome' into 'master'

How do you switch back to 'awesome'?
Use git checkout - to switch to the previous branch.
# suppose we're at the branch 'master' now
$ git checkout -
Switched to branch 'awesome'
# now let's go back to master
$ git checkout -
Switched to branch 'master'
Yeah, that's pretty similar to a good old cd -.

Git Tip of the Year: Aliases

If you want to use the "git st" instead of the "git status" then you've come to the right place.

If you want to use the "git co" instead of the "git checkout" then you've come to the right place.

If you want to use the "git ci" instead of the "git commit" then you've come to the right place.

Just run this commands:
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.ci commit
And you're done:
$ git ci --allow-empty -m "nice, we can commit with 'git ci' now"
Yay, we've just made a subversion out of git!

Monday, December 15, 2014

Alfred Tip of the Year: Run Command in the Terminal

First things first. This tip is for the powerpack. Come on, it's only £17!

Okay, fire up Alfred, type ">" followed by the terminal command:
This will open the Terminal and run the command "say Hallo".

If you want to use iTerm2 instead of the Terminal (who wouldn't?), no problem: Alfred Preferences -> Features -> Terminal -> Application:


Just choose an iTerm2 in the dropdown menu and you're set.


only £17 only only £17£17

Wednesday, December 10, 2014

PyCharm Tip of the Year: Multiple Cursors

Multiple cursors is the best thing to happen to text editing since ed.

Here's how to use them in PyCharm. Two simple rules:

  1. To add a cursor, hold an  ⌥ (Option) key and click on a place where you want to add a cursor.
  2. When done with multiple cursor editing, press Escape and you'll be left with a single cursor.

Here's a short video illustrating this feature:

Note: in this particular use case Rename Refactoring is definitely superior to Multiple Cursors. Still a great feature.

Saturday, December 6, 2014

IPython Tip of the Year: %timeit

Let's say you have written an amazing python function called amazing_function and you want to measure its speed.

Forget about manually importing time, and looping n times. Use:
  1. IPython
  2. IPython magic function %timeit:
$ ipython
In [1]: %timeit amazing_function('some', 'arguments')
100000 loops, best of 3: 13.4 µs per loop

Wednesday, December 3, 2014

Mac Tip of the Year: Find a Menu Item by its Name

Let's pretend that you forgot how to reopen closed tab in Chrome. All you remember is: the menu item is called "reopen something".

1. Hit ⌘? (slow motion instant replay: Command-Question mark). This shortcut activates help search field.
2. Type "reopen" in search box. It's quite logical: the command we're looking for have something to do with reopening, thus searching for "reopen" is pretty reasonable.

3. Select "Reopen Closed Tab" in search results:

4. Tada!

⌘? is a great way to find menu items by name. By the way, it works with every application.

Sunday, November 30, 2014

Mac Tip of the Year: Read your SSH Key Out Loud

If you feel lonely, you can make your Mac to speak to you.

Fire up the terminal and type:
say hello
say command, ahem, says things. It can even read your private SSH key out loud (warning: this is not very secure, someone can eavesdrop you private key):
cat ~/.ssh/id_rsa | say
Or your home directory:
ls | say
Even the cliboard content:
pbpaste | say

Wednesday, November 26, 2014

Mac Tip of the Year: Draw Graphs

Imagine a situation. You're surfing web on your Mac and suddenly you feel an urgent need to draw a graph of the function y = x * x.

These things happen.

Ok, start the built-in Grapher application. It'll ask you for the graph type:


Select "Default" and click "Choose". The graph window will appear.

Type in: y = x ^ 2 and press "Enter". Done:


Now you can continue surfing web.

Sunday, November 23, 2014

iTerm2 Tip of the Year: Open an URL

Let's say there is some URL in your iTerm session that you want to visit:


Hold the ⌘ key and click on the URL. iTerm2 will open the new browser tab with your URL.

Thursday, November 20, 2014

Mac Tip of the Year: Dictionary

Let's say you're reading Ulysses on your Mac and you've stumbled on an unknown word. These things happen, you know. Just do a three-finger tap on an unknown word and enjoy its definition:



It also shows you a snippet of the relevant Wikipedia article.

Heresiarch, huh?

Monday, November 17, 2014

Golang Tip of the Year: fmt.Errorf

So, you want to create an error from the format string in golang.
That's easy, right?
import (
  "errors"
  "fmt"
)
err := errors.New(fmt.Sprintf("my %s error", "awesome"))
About 4000 golang projects  do errors.New(fmt.Sprintf(...

Don't do it, there is a better way:
import (
  "fmt"
)
err := fmt.Errorf("my %s error", "awesome")
You're welcome.

Friday, November 14, 2014

iPhone Tip of the Year: Typing Period

Let's say you're answering a text message on your iPhone and you want to type a period.

So, you need to switch to the symbol keyboard and tap on a period, right?

No. There is a better way.

To type a period on your iPhone just press space twice.
Slow motion instant replay: p r e s s  s p a c e  t w i c e.

You're welcome. Period.

Tuesday, November 11, 2014

Localhost Tip of the Year: Wildcard Subdomains

There comes a time in life when you just need to have the wildcard localhost subdomains.

/etc/hosts won't help you.

Yes, you can roll out bind, named, and whatever else. But there is a simpler and good-enough solution.

Use vcap.me instead of localhost.
vcap.me is a public name (owned by VMWare, so it's pretty solid) that resolves into 127.0.0.1.

But wait, there is more. *.vcap.me also resolves into 127.0.0.1.

> nslookup whatever.vcap.me
Non-authoritative answer:
Name:    whatever.vcap.me
Address:  127.0.0.1

Now you have it: a wildcard domain that resolves into 127.0.0.1.

*.vcap.me

Saturday, November 8, 2014

Mac Tip of the Year: Insert Unicode Characters

Want to insert ⌘, ⇧, or some other weird character on Mac?
Press ⌃⌘Space (slow motion instant replay: Control-Command-Space) and the pop-up window will appear:


It has several character categories. Click on the icons in the bottom row to choose a category.

Emojis:


Panda emojis:

Strange-looking symbols:

And so on.

To insert a character/emoji in the text - just click on it.

Also, when pop-up window is active, you can start typing to search for a character by its name:



⇧⌫↑ℬ
I am very sorry.

Wednesday, November 5, 2014

Alfred Tip of the Year: Calculator

Alfred is an amazing application. It's a great app launcher. Everybody knows that.

More importantly, Alfred is the best calculator in the world. Not many people know that.

Here's how to use Alfred as a calculator. Let's say we want to calculate 120 * 0.72:

  1. Press ⌘Space to run Alfred (your Alfred launch shortcut may be a little different)
  2. Type 120 * 0.72 into Alfred search box
  3. It's 86.4. Done: 





 
 Optional: to copy the result of the calculation to the clipboard, press Enter or ⌘C

Monday, November 3, 2014

Python Tip of the Year: pip install --editable .

Let's say you're developing an application in Python. Being a good citizen you've created a file setup.py that describes your application.

Kind of like this:
from setuptools import setup

setup(
    name='yourapp',
    author='Your name',
    version='0.0.1',
    description='Your app',
    packages=['yourapp'],
)
When developing an application you often want to install it into system/virtualenv site-packages directory. At the same time you don't want to run python setup.py install after every time you make a change. There is a simple solution.

Just cd into directory containing setup.py and run pip install --editable .

Slow motion instant replay: pip install --editable <period>. Trailing period is important.

Now your application is installed into site-packages via symlink (kind of) and you don't need to reinstall it after making a change. Just edit your code and site-packages will magically have a new version of your application.

Tuesday, March 18, 2014

Java 8 is the biggest thing to happen to Java since Java 7

This is a transcript of the keynote given by Larry Ellison at the EclipseCon 2014.

Good morning.
[ Applause ]
[ Cheering ]
Thank you for coming this morning.

We've got some really cool stuff to show you. It's an amazing time at Oracle, an extraordinary time and I'd like to show you some of the things going on, I'll start with a few updates beginning with the new date and time API.

It's in the package java.time. We spent 18 years working every detail of the date and time API, getting everything exactly right. We used immutable classes so your programs that don't use threads are absolutely thread safe now. We provided a fluent interface so you can chain your method calls to make your programs look stunning.

No one would have done this but Oracle ... and the guy who did JodaTime.

Here's how you get the first Sunday of the next month:
import java.time.DayOfWeek;
import java.time.LocalDate;
import static 
     java.time.temporal.TemporalAdjusters.firstDayOfNextMonth;
import static java.time.temporal.TemporalAdjusters.nextOrSame;

LocalDate firstSundayOfNextMonth = LocalDate
  .now()
  .with(firstDayOfNextMonth())
  .with(nextOrSame(DayOfWeek.SUNDAY));
LocalDate.with method is remarkable.

Let's format a date:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

LocalDate tomorrow = LocalDate.now().plusDays(1);
tomorrow.format(DateTimeFormatter.ISO_DATE); // "2014-03-06"
It's absolutely gorgeous.

We've put your favorite formats into the DateTimeFormatter class:
import java.time.format.DateTimeFormatter;

tomorrow.format(DateTimeFormatter.ISO_DATE); // "2014-03-06"
tomorrow.format(DateTimeFormatter.ISO_WEEK_DATE); // "2014-W10-5"
tomorrow.format(DateTimeFormatter.ISO_ORDINAL_DATE); // "2014-066"
LocalDate class has a signature of factory method and this method has a signature (int, int, int) or (int, java.time.Month, int):
import java.time.LocalDate;
import java.time.Month;

LocalDate date = LocalDate.of(2014, 3, 18);
LocalDate sameDate = LocalDate.of(2014, Month.MARCH, 18);
All right.
[Cheering & Applause]
Now these snippets don't quite capture the energy of the brand new date and time API. This snippet does:
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

LocalDateTime now = LocalDateTime.now();
LocalDateTime afterNow = now.plusHours(5).plusMinutes(61);

long minutes = ChronoUnit.MINUTES.between(now, afterNow); // 361
long hours = ChronoUnit.HOURS.between(now, afterNow); // 6
long centuries = ChronoUnit.CENTURIES.between(now, afterNow); // 0
We're really excited about the ChronoUnit class. It's the most advanced way to calculate the amount of time between two temporal objects.

Interop with java.util.Date is incredibly simple. Here I just call toInstant, then atZone, and finally toLocalDate.
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

Date oldDate = new Date(114, 2, 18); // deprecated since 1.1
LocalDate brandNewDate = oldDate.toInstant()
  .atZone(ZoneId.systemDefault())
  .toLocalDate(); // 2014-03-18

Date oldDateFromBrandNewDate = Date.from(brandNewDate
  .atStartOfDay()
  .atZone(ZoneId.systemDefault())
  .toInstant()); // 2014-03-18
So that's a brief update on date and time API.

Now on to the stats.

Last year was an incredible year for Java. 89% of computers in the US run Java. 3 billion mobile phones run Java. Maven repository has over 600,000 artifacts. This is amazing.

No one could have predicted this. Now, today, we're taking it to the next level making a huge leap.
We have some very exciting news to tell you about Java.

We launched the first version of Java in 1996. And what an incredible breakthrough it was for the entire industry. Java simply went on to become the number one programming language in the world. That's why we called it Java one.

And each and every year, we introduced new versions with new features, new innovations. Each time, setting a new bar for what is the gold standard in the programming languages.

And we're going to do that again today. Today, we're going to introduce Java 8.
[ Applause ]
And I'd like to show it to you right now. So let's take a look at the brand new Java 8.

So, Java 8, it is made entirely of lambdas and streams. It's designed to an exacting level of standard unlike anything we or anyone in our industry has made before.

And I don't think it is an exaggeration to say that the software engineering that has gone into this product is the most challenging our team has even taken on. And what they've accomplished is simply amazing.

So let's walk through it.

Java 8 is the first programming language in history that has lambdas. But before we get into it, this is the monumental challenge the team had. How do you add lambdas to a language that doesn't even have the function type? You reinvent the interfaces. Interfaces in Java 8 are absolutely beautiful. Java 8 interface can provide a default method implementation:
public interface Language {
  default boolean hasLambdas() {
    return false;
  }
}
With typical legendary Java ease of use you just add the default modifier, provide an implementation and it works.

But we didn't stop there. We've added static methods to the interfaces.
public interface Language {
  static int getNumberOfLanguagesThatRunOnThreeBillionDevices {
    return 1;
  }
}
To give the finishing touch to the interfaces we've added stunning @FunctionalInterface annotation.

Functional interface is an interface that has exactly one abstract method.
@FunctionalInterface
public interface Language {
  default boolean hasLambdas() {
    return false;
  }

  static int getNumberOfLanguagesThatRunOnThreeBillionDevices {
    return 1;
  }

  // abstract method
  boolean runsOnThreeBillionDevices();
}
We don't want to make Java 8 too restrictive and @FunctionalInterface annotation is not required to make the interface functional. Having exactly one abstract method in the interface is enough.

But if you provide the @FunctionalInterface annotation, then our brand new compiler won't compile a functional interface with two or more abstract methods. It works like magic.
// Compilation error: Language is not a functional interface.
// Multiple non-overriding abstract methods found 
// in the interface Language
@FunctionalInterface
interface Language {
  boolean runsOnThreeBillionDevices();
  boolean isAmazing();
}
It's amazing. Let's use the brand new default methods to fix this error:
@FunctionalInterface
public interface Language {
  boolean runsOnThreeBillionDevices();

  default boolean isAmazing() {
    return runsOnThreeBillionDevices();
  }
}
But why would we redesign interfaces that way? And what does it have to do with lambdas?
Lambdas should feel great in the language and more importantly, should be easy to use. So when you have a functional interface you can instantiate it with lambda.

This is simply amazing.
Language java = () -> true;
System.out.println(java.runsOnThreeBillionDevices()); // true
When you instantiate a functional interface with lambda then this lambda "substitutes" that single abstract method. Type signatures of lambda and abstract method should match.

What's really amazing is that many of the JDK interfaces are in fact functional interfaces and can be instantiated with lambdas.

Working with threads and Runnable was never so much fun:
(new Thread(() -> System.out.println("Awesome"))).start();
Writing custom comparators is incredibly simple:
import java.util.Comparator;

Comparator<String> lengthComparator = 
  (x, y) -> Integer.compare(x.length(), y.length());
It's even simpler with the Comparator's brand new helper methods and method references:
import java.util.Comparator;

Comparator<String> lengthComparator = 
  Comparator.comparing(String::length);
Java 8 lambdas come in several different shapes. Our favorite at Oracle is type-inferenced short version. You just write an argument list, an arrow (->), and provide and implementation. It's pretty awesome:
Comparator<String> lengthComparator = 
  (x, y) -> Integer.compare(x.length(), y.length());
If you want to you can add a type signature:
Comparator<String> lengthComparator = 
  (String x, String y) -> Integer.compare(x.length(), y.length());
Or explicit return statement:
Comparator<String> lengthComparator = (String x, String y) -> { 
    return Integer.compare(x.length(), y.length()); 
  };
Method references can be used instead of lambdas with stunning effect.
// static method reference
// same as (int x, int y) -> Integer.compare(x, y)
Comparator<Integer> integerComparator = Integer::compare;

// non-static method reference
// same as (Integer integer) -> integer.byteValue()
Comparator<Integer> intAsByteComparator = 
  Comparator.comparing(Integer::byteValue);

// instance method reference 
// same as (String suffix) -> javaString.endsWith(suffix)
String javaString = "Java 8";
Comparator<String> mysteriousComparator = 
  Comparator.comparing(javaString::endsWith);

// constructor reference, same as (String s) -> new Integer(s)
Comparator<String> stringAsIntegerComparator = 
  Comparator.comparing(Integer::new);


Next, ultrauseful java.util.stream package. It plays so nice with lambdas.

It's nothing like seeing it. So I'd like to show it to you now.
import java.util.stream.Stream;
Creating a stream is remarkably simple:
Stream<String> languages = Stream.of("C", "Java 8", "C++", 
                                     "Haskell", "C");
Let's filter out high-level languages:
languages.filter(lang -> lang.startsWith("C")); 
// stream of C, C++, C
Method references feel incredible with the streams:
languages.map(String::length); // stream of 1, 6, 3, 7, 1
Stream.map applies specified function to the elements of the stream and returns a new stream.
Here's one gotcha though. You can chain stream methods:
languages.filter(lang -> lang.startsWith("C")).map(String::length);
But you can't call two methods on the same stream. This won't work:
languages.filter(lang -> lang.startsWith("C"));
languages.map(String::length); // throws IllegalStateException
We didn't stop with the map method. We've added mapToInt, mapToLong, and mapToDouble methods so you can enjoy working with your favorite primitive types:
languages.mapToInt(String::length) 
// primitive stream of 1, 6, 3, 7, 1. We call it IntStream
Stream.distinct method is fantastic when you don't want duplicate elements:
languages.filter(lang -> lang.startsWith("C")).distinct(); 
// stream of C and C++
We've reinvented the sorting. It's so simple:
languages.sorted(); // C, C, C++, Haskell, Java 8
Brand new Comparator methods take sorting to a different level:
languages.sorted(Comparator.reverseOrder()); 
// Java, Haskell, C++, C, C
Limiting and skipping your streams is incredibly fun:
languages.limit(3); // C, Java 8, C++
languages.skip(2); // C++, Haskell, C
Next, gorgeous forEach method:
languages.forEach(System.out::println); // println every element
By the way we've added the forEach method to our classic Iterable interface. It's fantastic.

How do you convert a stream to array? It's easy. You just call the toArray method:
String[] langs = languages.toArray(String[]::new); 
// convert to a string array
Counting elements is so easy:
languages.count(); // 5
These methods are so intuitive and natural I won't even explain what they're doing:
languages.anyMatch(s -> s.equals("Java 8")); // true
languages.allMatch(s -> s.equals("Java 8")); // false
languages.noneMatch(s -> s.equals("Java 8")); // false
Parallel processing of streams works like magic. You just call the parallel method and continue working with the stream like nothing happened:
languages.parallel().forEach(System.out::println); 
// makes processing parallel
Concat method is great for concatenating streams:
Stream.concat(Stream.of("Java", "Java 8"), Stream.of("C", "C++")); 
// Stream of Java, Java 8, C, C++
Finding the maximum element is gorgeous:
languages.max(Comparator.naturalOrder()); // Optional[Java 8]
Getting the first element is at least five years ahead of what's in any other language:
languages.findFirst(); // Optional[C]
Now, these last two methods return an Optional<String>. What is Optional? Optional is an amazing container which may or may not contain a non-null value.

You create an Optional with the revolutionary of method:
import java.util.Optional;

Optional<String> os = Optional.of("Java 8");
Creating an empty Optional is so easy with the empty method:
Optional<String> empty = Optional.empty();
You get a value from an Optional with the get method:
os.get(); // "Java 8"
To check if an Optional contains a value, you use the isPresent method:
os.isPresent(); // true
empty.isPresent(); // false
Optional.filter method is fantastic. If the filter predicate returns true for Optional value then it returns the Optional itself. If the filter predicate returns false then it returns an empty Optional.
os.filter(s -> s.startsWith("J")); // Optional[Java 8]
os.filter(s -> s.equals("C++"))); // Optional.empty
Filter method works for an empty Optional as well. It simply returns an empty Optional.
empty.filter(s -> s.startsWith("J")); // Optional.empty
Optional.map method is so cool. It applies a function to an Optional value and returns a new Optional.
os.map(s -> s + " is the best Java we've ever made"); 
// Optional[Java 8 is the best Java we've ever made]
As you can guess it works for an empty Optional as well:
empty.map(s -> s + " is the best Java we've ever made"); 
// Optional.empty
Optional.orElse method returns an Optional value or the default value if Optional is empty.
os.orElse("C++"); // Java 8
empty.orElse("Java 8"); // Java 8
But we didn't stop with the Optional<T>. We've added the OptionalInt, OptionalLong, and OptionalDouble classes so Java 8 has now 3 Optional types more than Haskell.

We've expanded your favorite JDK classes to take advantage of the fantastic streams API.

Files.lines method is the most advanced way to get all lines in a file.
import java.nio.file.Files;
import java.nio.file.Paths;

Stream<String> lines = Files.lines(Paths.get("Java.java"));
We've also added lines method to the BufferedReader class:
import java.io.BufferedReader;
import java.nio.file.Files;
import java.nio.file.Paths;

BufferedReader reader = Files.newBufferedReader(
                          Paths.get("Java.java"));
Stream<String> lines = reader.lines();
By the way Files.newBufferedReader is new in Java 8. There is also of course Files.newBufferedWriter.

We've added the stream method to the Collection interface:
import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Stream;

Collection<String> languagesCollection = new ArrayList<>();
languagesCollection.add("Java 8");
languagesCollection.add("C++");
Stream<String> languagesStream = languagesCollection.stream();
It's the best way to convert a collection to a stream.
java.util.Arrays utility class also has the stream method:
import java.utils.Arrays;

int[] intNumbers = new int[] { 8, 9, 10};
    Arrays.stream(intNumbers); // stream of integers
The stream method is overloaded so it works with all your favorite types. Unless your favorite type is byte.
import java.utils.Arrays;

double[] doubleNumbers = new double[] { 2.71, 3.14, 69.69};
String[] strings = new String[] { "Java 6", "Java 7", "Java 8"};
Arrays.stream(doubleNumbers).average(); // OptionalDouble[25.18]
Arrays.stream(strings).filter(s -> s.endsWith("8")).findFirst() 
// Optional[Java 8]
While we're at Arrays. Java 8 is 100% ready for the multicore revolution with the fantastic Arrays.parallelSort method:
import java.utils.Arrays;

double[] doubleNumbersToSort = new double[] { 69.69, 2.71, 3.14, };
Arrays.parallelSort(doubleNumbersToSort); // in-place sort: 2.71, 3.14, 69.69
Removing nulls from Collection is so simple with our brand new removeIf and Objects.isNull methods:
import java.util.Objects;
languagesCollection.removeIf(Objects::isNull); 
// modifies collection in-place
Pure stream solution is also amazing:
languagesStream.filter(Objects::nonNull); 
// returns a new stream with non null objects.
It's incredible.

While we're at collections, List interface has the stunning sort method now:
import java.util.ArrayList;
import java.util.Comparator;

ArrayList<String> languagesList = new ArrayList<>();

languagesList.add("Java 8");
languagesList.add("C++");
languagesList.add(null);
languagesList.sort(
  Comparator.nullsFirst(Comparator.naturalOrder())); 
// null, C++, Java 8
You can handle nulls in the list by using Comparator.nullsFirst method. No exception is thrown. It's amazing. Comparator also has the nullsLast method.

Random class has the ints, doubles, and longs methods that return streams:
import java.util.Random;

(new Random()).ints().limit(100); // stream of 100 random integers
(new Random()).ints(); // infinite stream of random integers
Now that's all great and amazing but I hear you asking what about the argument types? What is the argument type of the Stream.filter method? What is the argument type of the Stream.map method?

Let's look at the source code of our brand new Stream class:
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
Stream<T> filter(Predicate<? super T> predicate);
The team has added Function and Predicate types. Function and Predicate are of course functional interfaces and you can instantiate them with lambdas. That's why streams are so fun in Java 8. But the team didn't stop there. They've added some fantastic methods to the Function and Predicate interfaces.

It starts of course with the Function.compose method - the best way to compose functions:
import java.util.function.Function;

Function<String, String> toUpper = String::toUpperCase;
Function<String, String> concatAndToUpper = toUpper
  .compose(s -> s + " is the best Java we've ever made");
concatAndToUpper.apply("Java 8"); 
// JAVA 8 IS THE BEST JAVA WE'VE EVER MADE
And then there's amazing andThen method that constructs a new function with a slight twist:
Function<String, String> toUpperAndConcat = toUpper
  .andThen(s -> s + " is the best Java we've ever made");
toUpperAndConcat.apply("Java 8"); 
// JAVA 8 is the best Java we've ever made
Static method Function.identity can be helpful at lazy Sunday mornings when you don't want to do anything:
import java.util.function.Function;

Function<String, String> returnTheArgument = Function.identity();
returnTheArgument.apply("Java 8"); // Java 8
And that was the Function class. But there's so much more.

The Predicate class is stunning. Predicate is basically a function that takes one argument and returns a boolean.
import java.util.function.Predicate;

Predicate<String> isAmazing = s -> s.equals("Java 8");
isAmazing.test("Java 8"); // true
isAmazing.test("Haskell"); // false
Predicate interface has some incredible default methods. Predicate.or is fantastic for chaining or conditions:
Predicate<String> isGreatOrAmazing = isAmazing
  .or(s -> s.equals("Java 7"));
isGreatOrAmazing.test("Java 7"); // true
It also has the similar and method. Predicate.negate is beautiful:
Predicate<String> isNotAmazing = isAmazing.negate();
isNotAmazing.test("Java 7"); // true
isNotAmazing.test("Java 8"); // false
My favorite Predicate method is the static method isEqual. It returns a new Predicate that checks for equality to a specified object. It plays so nice with the brand new stream API:
import java.util.stream.Stream;
import static java.util.function.Predicate.isEqual;

Stream.of("C", "Java 8", "C++").filter(isEqual("Java 8")); 
// Stream of Java 8
Predicates are so useful that we've added the asPredicate method to the java.util.regex.Pattern class.
import java.util.regex.Pattern;
import java.util.stream.Stream;

Pattern isJava = Pattern.compile("^Java \\d+$");
Stream.of("Java 8", "C", "C++", "Java 7")
      .filter(isJava.asPredicate()); // Stream of Java 8, Java 7
And that was the new Predicate and Function interfaces. But the team didn't stop there.

They've enhanced the function types even further. They've added the first in history LGBT-friendly types BiFunction and BiPredicate. They're are similar to Function and Predicate but take two arguments instead of one. It's incredible.

But the team didn't stop there. They've added IntToLongFunction, IntToDoubleFunction, LongToIntFunction, LongToDoubleFunction, DoubleToIntFunction, DoubleToLongFunction, ToIntFunction, ToLongFunction, ToDoubleFunction, ToIntBiFunction, ToLongBiFunction, ToDoubleBiFunction, IntUnaryOperator, LongUnaryOperator, DoubleUnaryOperator, IntPredicate, LongPredicate, and DoublePredicate.

So you can work with all your favorite primitive types. That's by the way about 18 function types more than in Haskell. That's what Java 8 is all about: Java 8 is not a functional language, but it's so much better functional language than any functional language in the world.

[ Applause ]
Perhaps one of the most important features of Java is the JDK. And we have JDK 8, the latest version of the world's most advanced standard library. And it's been designed from the very beginning to take full advantage of this beautiful lambdas.

We are really excited about JDK 8. It has so many new features in it and I like to go ahead and show just a few of those for you here. We'll start off with our brand new java.util.StringJoiner class.

Let me go ahead and join some strings:
import java.utils.StringJoiner;

StringJoiner sj = new StringJoiner(", ");
sj.add("Java 8").add("C++").add("Haskell");
sj.toString(); // Java 8, C++, Haskell
And we also have this great constructor StringJoiner(String delimiter, String prefix, String suffix):
import java.utils.StringJoiner;

StringJoiner sj = 
  new StringJoiner(", ", "The most advanced languages are: ", ".");
sj.add("Java 8").add("C++").add("Haskell");
sj.toString(); 
// "The most advanced languages are: Java 8, C++, Haskell."
We've also got great Base64 class.
import java.util.Base64;

byte[] encodedMessage = Base64
  .getEncoder()
  .encode("Java 8 is the best Java we've ever made".getBytes());
new String(Base64.getDecoder().decode(encodedMessage)); 
// "Java 8 is the best Java we've ever made"
It's just gorgeous.

Next step, I'd like to show a nice little enhancement we made to the Math class. Now you can add numbers without worrying about silent integer overflow. Math.addExact adds two ints and throws ArithmeticException if sum overflows an int.
int maxValuePlusOne = Integer.MAX_VALUE + 1; 
// silent overflow, -2147483648

Math.addExact(Integer.MAX_VALUE, 1); // throws ArithmeticException
We've also added subtractExact, multiplyExact, incrementExact, decrementExact, and negateExact methods to the Math class. If your favorite integer type is long - every of the exact methods is overloaded on long.

It's that easy to safely work with numbers now.

The next step, let me show you some enhancements we've made to the Byte class.
First, let me convert a byte to an int:
// Java 7 way
byte theBiggestByteWeHaveEverMade = (byte)255;
int byteAsInt = theBiggestByteWeHaveEverMade & 0xFF;
This is the old way. And it's fine. But it's so much nicer with the brand new Byte.toUnsignedInt method:
int byteAsIntBrandNewWay = 
  Byte.toUnsignedInt(theBiggestByteWeHaveEverMade);
We've also provided Byte.toUnsignedLong method if your bytes are so big that they don't fit into an int.

Next, I'd like to show you some enhancements to the java.math.BigInteger. We've added the ultrasafe BigInteger.intValueExact method that throws an ArithmeticException if BigInteger doesn't fit into an int:
import java.math.BigInteger;

BigInteger big = BigInteger.valueOf(Integer.MAX_VALUE)
                           .add(BigInteger.ONE);
int bigAsInt = big.intValueExact(); // throws ArithmeticException
But we didn't stop there. We went on to add the byteValueExact, shortValueExact, and longValueExact methods so you can work with all your favorite primitive types. Even if your favorite primitive type is byte.
long bigAsLong = big.longValueExact(); // that's ok
Next, string joining. The team worked really hard at it and we have two join methods in the String class. The first one takes a delimiter and any number of strings.
String joinedString = String.join(", ", "Java 8", "Java 7", "C++", 
                                  "Haskell"); 
// "Java 8, Java 7, C++, Haskell"
Let me show you the second join method. It takes a delimiter and an Iterable.
import java.util.Arrays;
import java.util.List;

List<String> someStrings = Arrays.asList("Java 8", "Java 7", "C++", 
                                         "Haskell");
String joinedStringIterableWay = String.join(", ", someStrings); 
// "Java 8, Java 7, C++, Haskell"
Next, I'd like to show you a brand new class and that is java.util.stream.Collectors.
The Collectors class is the best way to collect all of your stream elements into one place.
Here I have a stream of strings:
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Stream<String> languages = Stream.of("C", "Java 8", "C++", 
                                     "Haskell", "C");
Let's convert this stream to a list. It's so easy with the Collectors.toList method.
List<String> asList = languages.collect(Collectors.toList());
List is of course an Iterable so our brand new forEach method is fantastic here:
asList.forEach(System.out::println);
Collectors.toSet is the most advanced way to convert a stream to a set.
Set<String> asSet = languages.collect(Collectors.toSet());
And one of the really cool things about Collectors is the joining method. Joining a stream of strings has never been that easy. No more Apache Commons, no more Guava, it's just Java 8 since today.
String joined = languages.collect(Collectors.joining(", ")); 
// String "C, Java 8, C++, Haskell, C"
Here I skip the delimiter argument and it just works:
String joinedWithoutDelimiter = languages
  .collect(Collectors.joining()); // String "CJava 8C++HaskellC"
What Collectors.groupingBy does is simply astounding:
Map<Character, List<String>> groupedByFirstChar = 

languages.collect(Collectors.groupingBy(s -> s.charAt(0))); 
// {'C': ["C", "C++", "C"], 'J': ["Java 8"], 'H': ["Haskell"]}
Let's print this map using the brand new Map.forEach method:
groupedByFirstChar.forEach(c, langs) -> 
  System.out.printf("%s=%s\n", c, String.join(", ", langs)));
Fantastic.

Collectors.partitioningBy is incredible for boolean grouping:
Map<Boolean, List<String>> paritioned = 
languages.collect(Collectors.partitioningBy(s -> s.startsWith("J"))); 
// {true: ["Java 8"], false: ["C", "C++", "Haskell", "C"]}
[ Applause ]
And that is Collectors.

Without doubt, JDK 8 is the most advanced standard library in the world and it powers Java 8.

The team has packed in innovation at every level of the design. It's the first language in the history that has lambdas. It's got more function types than any functional language. The brand new stream API would double the programmer productivity.

The all new date and time API, as well as Iterable.forEach method and that killer string joining feature.
It's got all new Optional type with the map method, and of course JDK 8.

Java 8 is the best Java we have ever made.

When you think about Java, it's probably the language that you use most at your work. It's the language that you have with you all the time. It powers Android, Hadoop, and Java Server Pages.

With this unique relationship people have with Java, we take changing it really seriously. We don't want to just make a new language. We want to make a much better language.

Java 8 is the result of this approach.

[ Applause ]

Well, I don't know about you but for me I think there's just two questions left. How much do I have to spend to get one?

Well, the great news, it's the same price as the Java 7 it replaces. It's completely free and comes with an awesome Ask Toolbar. That's amazing.

And the second question, when can I get my hands on one? Well, the great news it's getting out in the world today. Just check our downloads page.

[ Applause ]
Thank you.

Java 8, the biggest thing to happen to Java since Java 7. And I am so incredibly proud of everyone at Oracle that helps makes today occur. This language is simply amazing.

I hope you're as excited as we are by what you saw today. Thanks very much for coming.

Sunday, January 5, 2014

Easy guide to comparing Python classes to lambda functions

Flirting with TypeError

I was reading Peter Norvig's spelling corrector essay and stumbled on this:
This [code] works because max(None, i) is i for any integer i.
I was all like: wow, Python gonna let this punk get away with that? What's the matter? What's the world coming to?
nigel@codumentary:~$ python
Python 3.2.3 (default, Nov 22 1963, 18:30:12)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> help
Type help() for interactive help, or help(object) for help about object.
>>> max(None, 12)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() > NoneType()
That's what the world is coming to: TypeError.

But. There is a huge but.

A Huge But

pnorvig@google:/usr/local/secret-project/popups$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> # it works
>>> max(None, 12)
12
>>> # there is more
>>> class FarmAnimal(object): pass
>>> max(lambda x: x + 12, FarmAnimal)
<class '__main__.FarmAnimal'>
Yes, you can compare class to lambda function in Python2. Python3 throws an exception.

I think that's a really weird behaviour. I mean Python2 allowing it, not Python3 throwing an exception. What's going on here? Has Guido van Rossum gone completely Brendan Eich? What does Python documentation say?

Read the Docs

To quote the docs:
Objects of different types, except different numeric types and different string types, never compare equal; such objects are ordered consistently but arbitrarily (so that sorting a heterogeneous array yields a consistent result)
CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.
Let's check it:
>>> type(FarmAnimal).__name__
'type'
>>> type(lambda x: x + 12).__name__ 'function'
Type name of FarmAnimal is 'type', type name of lambda x: x + 12 is 'function'. That's why FarmAnimal is greater than lambda x: x + 12 (string 'type' is greater than string 'function').

What about this:
>>> max(None, 12)
12
Is it because string 'int' is greater than string 'NoneType'?
Documentation mentions that numeric types are exception to the rule - not very helpful.

Let's have a look at CPython2 source code.

Read the Source

The source says:
static int
default_3way_compare(PyObject *v, PyObject *w) {    ...    ...     /* None is smaller than anything */     if (v == Py_None)         return -1;     if (w == Py_None)         return 1;     /* different type: compare type names; numbers are smaller */     if (PyNumber_Check(v))         vname = "";     else         vname = v->ob_type->tp_name;     if (PyNumber_Check(w))         wname = "";     else         wname = w->ob_type->tp_name;     c = strcmp(vname, wname);     if (c < 0)         return -1;     if (c > 0)         return 1;     /* Same type name, or (more likely) incomparable numeric types */     return ((Py_uintptr_t)(v->ob_type) < (         Py_uintptr_t)(w->ob_type)) ? -1 : 1; }

Source is good

I was wrong. The answer to question 
Is it because string 'int' is greater than string 'NoneType'? 
is no.

12 is greater than None not because 'int' is greater than 'NoneType'.
12 is greater than None because of this C code:
    /* None is smaller than anything */
    if (v == Py_None)
        return -1;
    if (w == Py_None)
        return 1;
Anything is greater than None in CPython2. None is Python's absolute zero. Here's the proof:
>>> cow = FarmAnimal()
>>> type(cow).__name__
'FarmAnimal'
>>> 'FarmAnimal' > 'NoneType'
False
>>> # Comparing to None ignores type names >>> max(None, cow) <__main__.FarmAnimal object at 0xdeadbeef>

Twitterize it

CPython2 comparison tower is:
1) None
2) Numeric types ordered by type name
3) Non-numeric types ordered by type name


Unleash Hell

And that's why max(None, i) is i for any integer i.

Now you can compare all sorts of stuff in your Python2 programs. Apples to tuples, ashes to exceptions, numbers to None.
Unleash hell!

You should be aware of two things though:
1) Hell won't work in Python3
2) Nobody wants extra troubles while porting code to Python3