Tutorial by Examples

Unnamed class types may also be used when creating type aliases, i.e. via typedef and using: C++11 using vec2d = struct { float x; float y; }; typedef struct { float x; float y; } vec2d; vec2d pt; pt.x = 4.f; pt.y = 3.f;
extern crate serde; extern crate serde_json; #[macro_use] extern crate serde_derive; use std::collections::BTreeMap as Map; #[derive(Serialize)] struct Resource { // Always serialized. name: String, // Never serialized. #[serde(skip_serializing)] hash: String, ...
mutable modifier in this context is used to indicate that a data field of a const object may be modified without affecting the externally-visible state of the object. If you are thinking about caching a result of expensive computation, you should probably use this keyword. If you have a lock (for ...
By default, the implicit operator() of a lambda is const. This disallows performing non-const operations on the lambda. In order to allow modifying members, a lambda may be marked mutable, which makes the implicit operator() non-const: int a = 0; auto bad_counter = [a] { return a++; // er...
Maps and keyword lists have different application. For instance, a map cannot have two keys with the same value and it's not ordered. Conversely, a Keyword list can be a little bit hard to use in pattern matching in some cases. Here's a few use cases for maps vs keyword lists. Use keyword lists wh...
This example shows a simple image cropping function that takes an image and cropping coordinates and returns the cropped image. function cropImage(image, croppingCoords) { var cc = croppingCoords; var workCan = document.createElement("canvas"); // create a canvas workCan.wi...
You can also use the IEx (Interactive Elixir) shell to evaluate expressions and execute code. If you are on Linux or Mac, just type iex on your bash and press enter: $ iex If you are on a Windows machine, type: C:\ iex.bat Then you will enter into the IEx REPL (Read, Evaluate, Print, Loop),...
f = fn {:a, :b} -> IO.puts "Tuple {:a, :b}" [] -> IO.puts "Empty list" end f.({:a, :b}) # Tuple {:a, :b} f.([]) # Empty list
{ a, b, c } = { "Hello", "World", "!" } IO.puts a # Hello IO.puts b # World IO.puts c # ! # Tuples of different size won't match: { a, b, c } = { "Hello", "World" } # (MatchError) no match of right hand side value: { "Hello",...
When you document your code with @doc, you can supply code examples like so: # myproject/lib/my_module.exs defmodule MyModule do @doc """ Given a number, returns `true` if the number is even, otherwise `false`. ## Example iex> MyModule.even?(2) true ie...
The code listing below attempts to classify handwritten digits from the MNIST dataset. The digits look like this: The code will preprocess these digits, converting each image into a 2D array of 0s and 1s, and then use this data to train a neural network with upto 97% accuracy (50 epochs). "...
struct FileAttributes { unsigned int ReadOnly: 1; unsigned int Hidden: 1; }; Here, each of these two fields will occupy 1 bit in memory. It is specified by : 1 expression after the variable names. Base type of bit field could be any integral type (8-bit int to 64-bit int). Using u...
C++11 In C++11, compilers are required to implicitly move from a local variable that is being returned. Moreover, most compilers can perform copy elision in many cases and elide the move altogether. As a result of this, returning large objects that can be moved cheaply no longer requires special ha...
Let's have a basic failing program: #include <iostream> void fail() { int *p1; int *p2(NULL); int *p3 = p1; if (p3) { std::cout << *p3 << std::endl; } } int main() { fail(); } Build it (add -g to include debug info): g++ -g -o m...
Initializing std::array<T, N>, where T is a scalar type and N is the number of elements of type T If T is a scalar type, std::array can be initialized in the following ways: // 1) Using aggregate-initialization std::array<int, 3> a{ 0, 1, 2 }; // or equivalently std::array<int, 3...
This example has been lifted from the Q & A section here:http://stackoverflow.com/a/1008289/3807729 See this article for a simple design for a lazy evaluated with guaranteed destruction singleton: Can any one provide me a sample of Singleton in c++? The classic lazy evaluated and correctly de...
class API { public: static API& instance(); virtual ~API() {} virtual const char* func1() = 0; virtual void func2() = 0; protected: API() {} API(const API&) = delete; API& operator=(const API&) = delete; }; class WindowsAPI ...
You can generate a rails migration file from the terminal using the following command: rails generate migration NAME [field[:type][:index] field[:type][:index]] [options] For a list of all the options supported by the command, you could run the command without any arguments as in rails generate ...
Type check: variable.isInstanceOf[Type] With pattern matching (not so useful in this form): variable match { case _: Type => true case _ => false } Both isInstanceOf and pattern matching are checking only the object's type, not its generic parameter (no type reification), except fo...
Let's say we need to add a button for each piece of loadedData array (for instance, each button should be a slider showing the data; for the sake of simplicity, we'll just alert a message). One may try something like this: for(var i = 0; i < loadedData.length; i++) jQuery("#container&q...

Page 344 of 1336