Tutorial by Examples: ch

The following code is based on the examples provided by the documentation on std::net::TcpListener. This server application will listen to incoming requests and send back all incoming data, thus acting as an "echo" server. The client application will send a small message and expect a reply...
prop_evenNumberPlusOneIsOdd :: Integer -> Property prop_evenNumberPlusOneIsOdd x = even x ==> odd (x + 1) If you want to check that a property holds given that a precondition holds, you can use the ==> operator. Note that if it's very unlikely for arbitrary inputs to match the precondit...
A try/catch block is used to catch exceptions. The code in the try section is the code that may throw an exception, and the code in the catch clause(s) handles the exception. #include <iostream> #include <string> #include <stdexcept> int main() { std::string str("foo&...
The recommended way (Since ES5) is to use Array.prototype.find: let people = [ { name: "bob" }, { name: "john" } ]; let bob = people.find(person => person.name === "bob"); // Or, more verbose let bob = people.find(function(person) { return person.na...
The standard doesn't specify if char should be signed or unsigned. Different compilers implement it differently, or might allow to change it using a command line switch.
# example data test_sentences <- c("The quick brown fox", "jumps over the lazy dog") Is there a match? grepl() is used to check whether a word or regular expression exists in a string or character vector. The function returns a TRUE/FALSE (or "Boolean") vecto...
The emptiness of a list is associated to the boolean False, so you don't have to check len(lst) == 0, but just lst or not lst lst = [] if not lst: print("list is empty") # Output: list is empty
You can commit changes made to specific files and skip staging them using git add: git commit file1.c file2.h Or you can first stage the files: git add file1.c file2.h and commit them later: git commit
In some cases you may want to wrap a synchronous operation inside a promise to prevent repetition in code branches. Take this example: if (result) { // if we already have a result processResult(result); // process it } else { fetchResult().then(processResult); } The synchronous and async...
git checkout --orphan new-orphan-branch The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits. source
Any PHP expression within double curly braces {{ $variable }} will be echoed after being run through the e helper function. (So html special characters (<, >, ", ', &) are safely replaced for the corresponding html entities.) (The PHP expression must evaluate to string, otherwise an ...
A simple type switch: // assuming x is an expression of type interface{} switch t := x.(type) { case nil: // x is nil // t will be type interface{} case int: // underlying type of x is int // t will be int in this case as well case string: // underlying type of x is st...
To get a value from the map, you just have to do something like:00 value := mapName[ key ] If the map contains the key, it returns the corresponding value. If not, it returns zero-value of the map's value type (0 if map of int values, "" if map of string values...) m := map[string]s...
It is possible to create a function that accepts objects that implement a specific trait. Static Dispatch fn generic_speak<T: Speak>(speaker: &T) { println!("{0}", speaker.speak()); } fn main() { let person = Person {}; let dog = Dog {}; generic_speak(...
To switch between time zones, you need datetime objects that are timezone-aware. from datetime import datetime from dateutil import tz utc = tz.tzutc() local = tz.tzlocal() utc_now = datetime.utcnow() utc_now # Not timezone-aware. utc_now = utc_now.replace(tzinfo=utc) utc_now # Timezon...
[alias] logp=log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short lg = log --graph --date-order --first-parent \ --pretty=format:'%C(auto)%h%Creset %C(auto)%d%Creset %s %C(green)(%ad) %C(bold cyan)<%an>%Creset' lgb = log --graph --date-order --branches --first-paren...
The background-attachment property sets whether a background image is fixed or scrolls with the rest of the page. body { background-image: url('img.jpg'); background-attachment: fixed; } ValueDescriptionscrollThe background scrolls along with the element. This is default.fixedThe backgro...
Example that builds an executable (editor) and links a library (highlight) to it. Project structure is straightforward, it needs a master CMakeLists and a directory for each subproject: CMakeLists.txt editor/ CMakeLists.txt src/ editor.cpp highlight/ CMakeLists.txt in...
Unlike many other programming languages, the throw and catch keywords are not related to exception handling in Ruby. In Ruby, throw and catch act a bit like labels in other languages. They are used to change the control flow, but are not related to a concept of "error" like Exceptions are...
Within a catch block the throw keyword can be used on its own, without specifying an exception value, to rethrow the exception which was just caught. Rethrowing an exception allows the original exception to continue up the exception handling chain, preserving its call stack or associated data: try...

Page 14 of 109