Tutorial by Examples

In languages such as C, the @goto statement is often used to ensure a function cleans up necessary resources, even in the event of an error. This is less important in Julia, because exceptions and try-finally blocks are often used instead. However, it is possible for Julia code to interface with C ...
Sometimes, one wants to run some initialization code once before testing a condition. In certain other languages, this kind of loop has special do-while syntax. However, this syntax can be replaced with a regular while loop and break statement, so Julia does not have specialized do-while syntax. Ins...
0.5.0 (Although this example is written using syntax introduced in version v0.5, it can work with few modifications on older versions also.) This implementation of breadth-first search (BFS) on a graph represented with adjacency lists uses while loops and the return statement. The task we will sol...
If you implement or create a listener in an Activity, always pay attention to the lifecycle of the object that has the listener registered. Consider an application where we have several different activities/fragments interested in when a user is logged in or out. One way of doing this would be to h...
% set mypath /home/tcluser/sources/tcl/myproject/test.tcl /home/tcluser/sources/tcl/myproject/test.tcl % set dir [file dirname $mypath] /home/tcluser/sources/tcl/myproject % set filename [file tail $mypath] test.tcl % set basefilename [file rootname $filename] test % set extension [file exte...
Consider the following code snippet: // Print the sum of the numbers 1 to 10 int count = 0; for (int i = 1; i < 010; i++) { // Mistake here .... count = count + i; } System.out.println("The sum of 1 to 10 is " + count); A Java beginner might be surprised to know that the...
context.globalCompositeOperation = 'source-atop' source-atop compositing clips new image inside an existing shape. // gold filled rect ctx.fillStyle='gold'; ctx.fillRect(100,100,100,75); // shadow ctx.shadowColor='black'; ctx.shadowBlur=10; // restrict new draw to cover existing pixels ct...
The javadoc for the Thread class shows two ways to define and use a thread: Using a custom thread class: class PrimeThread extends Thread { long minPrime; PrimeThread(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes l...
Jumping out of nested loops would usually require use of a boolean variable with a check for this variable in the loops. Supposing we are iterating over i and j, it could look like this size_t i,j; for (i = 0; i < myValue && !breakout_condition; ++i) { for (j = 0; j < mySecondVa...
A lot of people who are new to multi-threading think that using threads automatically make an application go faster. In fact, it is a lot more complicated than that. But one thing that we can state with certainty is that for any computer there is a limit on the number of threads that can be run at ...
Returning a value One commonly used case: returning from main() #include <stdlib.h> /* for EXIT_xxx macros */ int main(int argc, char ** argv) { if (2 < argc) { return EXIT_FAILURE; /* The code expects one argument: leave immediately skipping t...
When programming in Prolog it is not always possible, or desirable, to create predicates which unify for every possible combination of parameters. For example, the predicate between(X,Y,Z) which expresses that Z is numerically between X and Y. It is easily implemented in the cases where X, Y, and Z ...
Immediately continue reading on invalid input or break on user request or end-of-file: #include <stdlib.h> /* for EXIT_xxx macros */ #include <stdio.h> /* for printf() and getchar() */ #include <ctype.h> /* for isdigit() */ void flush_input_stream(FILE * fp); int main(v...
You can take a look at the full code in this working Plunker. In this example I use a shared service to handle the communication between the pages inside the tab (child pages) and the tab container (the component that holds the tabs). Even though you probably could do it with Events I like the shar...
Given: x <- as.matrix(mtcars) One can use heatmap.2 - a more recent optimized version of heatmap, by loading the following library: require(gplots) heatmap.2(x) To add a title, x- or y-label to your heatmap, you need to set the main, xlab and ylab: heatmap.2(x, main = "My main t...
Gloss is easily installed using the Cabal tool. Having installed Cabal, one can run cabal install gloss to install Gloss. Alternatively the package can be built from source, by downloading the source from Hackage or GitHub, and doing the following: Enter the gloss/gloss-rendering/ directory and ...
In Gloss, one can use the display function to create very simple static graphics. To use this one needs to first import Graphics.Gloss. Then in the code there should the following: main :: IO () main = display window background drawing window is of type Display which can be constructed in two ...
Sometimes, programmers who are new to Java make the mistake of defining a class with a name that is the same as a widely used class. For example: package com.example; /** * My string utilities */ public class String { .... } Then they wonder why they get unexpected errors. For ex...
Render a white rectangle over an image with the composite operation ctx.globalCompositeOperation = 'difference'; The amount of the effect can be controled with the alpha setting // Render the image ctx.globalCompositeOperation='source-atop'; ctx.drawImage(image, 0, 0); // set the composite...
Remove color from an image via ctx.globalCompositeOperation = 'color'; The amount of the effect can be controled with the alpha setting // Render the image ctx.globalCompositeOperation='source-atop'; ctx.drawImage(image, 0, 0); // set the composite operation ctx.globalCompositeOperation='...

Page 777 of 1336