Tutorial by Examples: n

To check if a key exists in a Map, use the .has() method: map.has(key); Example: const map = new Map([[1, 2], [3, 4]]); console.log(map.has(1)); // true console.log(map.has(2)); // false
Map has three methods which returns iterators: .keys(), .values() and .entries(). .entries() is the default Map iterator, and contains [key, value] pairs. const map = new Map([[1, 2], [3, 4]]); for (const [key, value] of map) { console.log(`key: ${key}, value: ${value}`); // logs: // ke...
Use .get(key) to get value by key and .set(key, value) to assign a value to a key. If the element with the specified key doesn't exist in the map, .get() returns undefined. .set() method returns the map object, so you can chain .set() calls. const map = new Map(); console.log(map.get(1)); // und...
To get the numbers of elements of a Map, use the .size property: const map = new Map([[1, 2], [3, 4]]); console.log(map.size); // 2
EntityState.Added can be set in two fully equivalent ways: By setting the state of its entry in the context: context.Entry(entity).State = EntityState.Added; By adding it to a DbSet of the context: context.Entities.Add(entity); When calling SaveChanges, the entity will be inse...
To enable code shrinking with ProGuard, add minifyEnabled true to the appropriate build type in your build.gradle file. android { buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile(‘proguard-android.txt'), 'pro...
To enable resource shrinking, set the shrinkResources property to true in your build.gradle file. android { ... buildTypes { release { minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'p...
All libraries come with resources that are not necessary useful to your application. For example Google Play Services comes with translations for languages your own application don’t even support. You can configure the build.gradle file to specify which resource you want to keep. For example: de...
Setting the state of an object graph (a collection of related entities) to Added is different than setting a single entity as Added (see this example). In the example, we store planets and their moons: Class model public class Planet { public Planet() { Moons = new HashSet<...
Using requestAnimationFrame may on some systems update at more frames per second than the 60fps. 60fps is the default rate if the rendering can keep up. Some systems will run at 120fps maybe more. If you use the following method you should only use frame rates that are integer divisions of 60 so th...
An event allows the developer to implement a notification pattern. Simple example public class Server { // defines the event public event EventHandler DataChangeEvent; void RaiseEvent() { var ev = DataChangeEvent; if(ev != null) { ev(t...
Building project dependencies can sometimes be a tedious task. Instead of publishing a package version to NPM and installing the dependency to test the changes, use npm link. npm link creates a symlink so the latest code can be tested in a local environment. This makes testing global tools and proje...
To locate comments in BeautifulSoup, use the text (or string in the recent versions) argument checking the type to be Comment: from bs4 import BeautifulSoup from bs4 import Comment data = """ <html> <body> <div> <!-- desired text --&gt...
The %timeit magic runs the given code many times, then returns the speed of the fastest result. In [1]: %timeit sum(range(100000)) 100 loops, best of 3: 2.91 ms per loop The %%timeit cell magic can be used to time blocks of code. In [2]: %%timeit ...: a = 0 ...: for i in range(100000):...
R provides two additional looping constructs, while and repeat, which are typically used in situations where the number of iterations required is indeterminate. The while loop The general form of a while loop is as follows, while (condition) { ## do something ## in loop body } whe...
First, let's see three different ways of extracting data from a file. string fileText = File.ReadAllText(file); string[] fileLines = File.ReadAllLines(file); byte[] fileBytes = File.ReadAllBytes(file); On the first line, we read all the data in the file as a string. On the second line, we r...
Iterating over connected serial ports using System.IO.Ports; string[] ports = SerialPort.GetPortNames(); for (int i = 0; i < ports.Length; i++) { Console.WriteLine(ports[i]); } Instantiating a System.IO.SerialPort object using System.IO.Ports; SerialPort port = new SerialPort(); ...
This example is about replacing a List element while ensuring that the replacement element is at the same position as the element that is replaced. This can be done using these methods: set(int index, T type) int indexOf(T type) Consider an ArrayList containing the elements "Program sta...
A conversion that involves calling an explicit constructor or conversion function can't be done implicitly. We can request that the conversion be done explicitly using static_cast. The meaning is the same as that of a direct initialization, except that the result is a temporary. class C { std:...
static_cast can perform any implicit conversion. This use of static_cast can occasionally be useful, such as in the following examples: When passing arguments to an ellipsis, the "expected" argument type is not statically known, so no implicit conversion will occur. const double x = ...

Page 594 of 1088