Tutorial by Examples

In addition to named imports, you can provide a default export. // circle.js export const PI = 3.14; export default function area(radius) { return PI * radius * radius; } You can use a simplified syntax to import the default export. import circleArea from './circle'; console.log(circle...
Sometimes you have a module that you only want to import so its top-level code gets run. This is useful for polyfills, other globals, or configuration that only runs once when your module is imported. Given a file named test.js: console.log('Initializing...') You can use it like this: import '...
In ECMAScript 6, when using the module syntax (import/export), each file becomes its own module with a private namespace. Top-level functions and variables do not pollute the global namespace. To expose functions, classes, and variables for other modules to import, you can use the export keyword. /...
Given that the module from the Defining a Module section exists in the file test.js, you can import from that module and use its exported members: import {doSomething, MyClass, PI} from './test' doSomething() const mine = new MyClass() mine.test() console.log(PI) The somethingPrivate()...
A C++ namespace is a collection of C++ entities (functions, classes, variables), whose names are prefixed by the name of the namespace. When writing code within a namespace, named entities belonging to that namespace need not be prefixed with the namespace name, but entities outside of it must use t...
Creating a namespace is really easy: //Creates namespace foo namespace Foo { //Declares function bar in namespace foo void bar() {} } To call bar, you have to specify the namespace first, followed by the scope resolution operator ::: Foo::bar(); It is allowed to create one names...
A useful feature of namespaces is that you can expand them (add members to it). namespace Foo { void bar() {} } //some other stuff namespace Foo { void bar2() {} }
The keyword 'using' has three flavors. Combined with keyword 'namespace' you write a 'using directive': If you don't want to write Foo:: in front of every stuff in the namespace Foo, you can use using namespace Foo; to import every single thing out of Foo. namespace Foo { void bar() {} ...
Opening a file is done in the same way for all 3 file streams (ifstream, ofstream, and fstream). You can open the file directly in the constructor: std::ifstream ifs("foo.txt"); // ifstream: Opens file "foo.txt" for reading only. std::ofstream ofs("foo.txt"); // ...
There are several ways to read data from a file. If you know how the data is formatted, you can use the stream extraction operator (>>). Let's assume you have a file named foo.txt which contains the following data: John Doe 25 4 6 1987 Jane Doe 15 5 24 1976 Then you can use the following...
There are several ways to write to a file. The easiest way is to use an output file stream (ofstream) together with the stream insertion operator (<<): std::ofstream os("foo.txt"); if(os.is_open()){ os << "Hello World!"; } Instead of <<, you can also ...
When creating a file stream, you can specify an opening mode. An opening mode is basically a setting to control how the stream opens the file. (All modes can be found in the std::ios namespace.) An opening mode can be provided as second parameter to the constructor of a file stream or to its open(...
Explicitly closing a file is rarely necessary in C++, as a file stream will automatically close its associated file in its destructor. However, you should try to limit the lifetime of a file stream object, so that it does not keep the file handle open longer than necessary. For example, this can be ...
In addition to importing named members from a module or a module's default export, you can also import all members into a namespace binding. import * as test from './test' test.doSomething() All exported members are now available on the test variable. Non-exported members are not available, j...
Sometimes you may encounter members that have really long member names, such as thisIsWayTooLongOfAName(). In this case, you can import the member and give it a shorter name to use in your current module: import {thisIsWayTooLongOfAName as shortName} from 'module' shortName() You can import m...
Common values from both sets: You can use the intersect(_:) method to create a new set containing all the values common to both sets. let favoriteColors: Set = ["Red", "Blue", "Green"] let newColors: Set = ["Purple", "Orange", "Green"] ...
To create a new branch, while staying on the current branch, use: git branch <name> Generally, the branch name must not contain spaces and is subject to other specifications listed here. To switch to an existing branch : git checkout <name> To create a new branch and switch to it...
Let's say you've got a list of restaurants -- maybe you read it from a file. You care about the unique restaurants in the list. The best way to get the unique elements from a list is to turn it into a set: restaurants = ["McDonald's", "Burger King", "McDonald's", &qu...
Counter is a dict sub class that allows you to easily count objects. It has utility methods for working with the frequencies of the objects that you are counting. import collections counts = collections.Counter([1,2,3]) the above code creates an object, counts, which has the frequencies of all ...
collections.defaultdict(default_factory) returns a subclass of dict that has a default value for missing keys. The argument should be a function that returns the default value when called with no arguments. If there is nothing passed, it defaults to None. >>> state_capitals = collections.d...

Page 60 of 1336