Tutorial by Examples

// Java: long count = items.stream().filter( item -> item.startsWith("t")).count(); // Kotlin: val count = items.filter { it.startsWith('t') }.size // but better to not filter, but count with a predicate val count = items.count { it.startsWith('t') }
// Java: List<String> myList = Arrays.asList("a1", "a2", "b1", "c2", "c1"); myList.stream() .filter(s -> s.startsWith("c")) .map(String::toUpperCase) .sorted() .forEach(System.out::println); // C1 ...
// Java: Arrays.asList("a1", "a2", "a3") .stream() .findFirst() .ifPresent(System.out::println); // Kotlin: listOf("a1", "a2", "a3").firstOrNull()?.apply(::println) or, create an extension function on String calle...
// Java: String phrase = persons .stream() .filter(p -> p.age >= 18) .map(p -> p.name) .collect(Collectors.joining(" and ", "In Germany ", " are of legal age.")); System.out.println(phrase); // In Germany Max and Peter a...
// Java: Map<Integer, String> map = persons .stream() .collect(Collectors.toMap( p -> p.age, p -> p.name, (name1, name2) -> name1 + ";" + name2)); System.out.println(map); // {18=Max, 23=Peter;Pamela, ...
// Java (verbose): Collector<Person, StringJoiner, String> personNameCollector = Collector.of( () -> new StringJoiner(" | "), // supplier (j, p) -> j.add(p.name.toUpperCase()), // accumulator (j1, j2) -> j1.merge(j2), // c...
// Java: IntSummaryStatistics ageSummary = persons.stream() .collect(Collectors.summarizingInt(p -> p.age)); System.out.println(ageSummary); // IntSummaryStatistics{count=4, sum=76, min=12, average=19.000000, max=23} // Kotlin: // something to hold the stats... da...
CRTP is a powerful, static alternative to virtual functions and traditional inheritance that can be used to give types properties at compile time. It works by having a base class template which takes, as one of its template parameters, the derived class. This permits it to legally perform a static_c...
Subroutines are created by using the keyword sub followed by an identifier and a code block enclosed in braces. You can access the arguments by using the special variable @_, which contains all arguments as an array. sub function_name { my ($arg1, $arg2, @more_args) = @_; # ... } Sin...
/* ToNumber(ToPrimitive([])) == ToNumber(false) */ [] == false; // true When [].toString() is executed it calls [].join() if it exists, or Object.prototype.toString() otherwise. This comparison is returning true because [].join() returns '' which, coerced into 0, is equal to false ToNumber. Bew...
Basic table usage includes accessing and assigning table elements, adding table content, and removing table content. These examples assume you know how to create tables. Accessing Elements Given the following table, local example_table = {"Nausea", "Heartburn", "Indigesti...
<link rel="stylesheet" href="path/to.css" type="text/css"> The standard practice is to place CSS <link> tags inside the <head> tag at the top of your HTML. This way the CSS will be loaded first and will apply to your page as it is loading, rather th...
Synchronous <script src="path/to.js"></script> Standard practice is to place JavaScript <script> tags just before the closing </body> tag. Loading your scripts last allows your site's visuals to show up more quickly and discourages your JavaScript from trying to...
<link rel="icon" type="image/png" href="/favicon.png"> <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico"> Use the mime-type image/png for PNG files and image/x-icon for icon (*.ico) files. For the difference...
You can pass data to a template in a custom variable. In your views.py: from django.views.generic import TemplateView from MyProject.myapp.models import Item class ItemView(TemplateView): template_name = "item.html" def items(self): """ Get all Ite...
CMake generates build environments for nearly any compiler or IDE from a single project definition. The following examples will demonstrate how to add a CMake file to the cross-platform "Hello World" C++ code. CMake files are always named "CMakeLists.txt" and should already exis...
Creating ***bold italic*** text is simply a matter of using both **bold** (two asterisks) and *italic* (one asterisk) at the same time, for a total of three asterisks on either side of the text you want to format at once. Creating bold italic text is simply a matter of using both bold (two a...
Once you have a Dockerfile, you can build an image from it using docker build. The basic form of this command is: docker build -t image-name path If your Dockerfile isn't named Dockerfile, you can use the -f flag to give the name of the Dockerfile to build. docker build -t image-name -f Dockerfi...
FROM node:5 The FROM directive specifies an image to start from. Any valid image reference may be used. WORKDIR /usr/src/app The WORKDIR directive sets the current working directory inside the container, equivalent to running cd inside the container. (Note: RUN cd will not change the current ...
Have you ever forgotten to add a trap to clean up a temporary file or do other work at exit? Have you ever set one trap which canceled another? This code makes it easy to add things to be done on exit one item at a time, rather than having one large trap statement somewhere in your code, which may...

Page 89 of 1336