Tutorial by Examples: check

The container std::map has a member function empty(), which returns true or false, depending on whether the map is empty or not. The member function size() returns the number of element stored in a std::map container: std::map<std::string , int> rank {{"facebook.com", 1} ,{"goo...
A bit counter-intuitive to the way most other languages' standard I/O libraries do it, Haskell's isEOF does not require you to perform a read operation before checking for an EOF condition; the runtime will do it for you. import System.IO( isEOF ) eofTest :: Int -> IO () eofTest line = do ...
public bool Check() { string input = "Hello World!"; string pattern = @"H.ll. W.rld!"; // true return Regex.IsMatch(input, pattern); }
Prefer public class Order { public OrderLine AddOrderLine(OrderLine orderLine) { if (orderLine == null) throw new ArgumentNullException(nameof(orderLine)); ... } } Over public class Order { public OrderLine AddOrderLine(OrderLine orderLine) { ...
All Java exceptions are instances of classes in the Exception class hierarchy. This can be represented as follows: java.lang.Throwable - This is the base class for all exception classes. Its methods and constructors implement a range of functionality common to all exceptions. java.lang.Excep...
The function in_array() returns true if an item exists in an array. $fruits = ['banana', 'apple']; $foo = in_array('banana', $fruits); // $foo value is true $bar = in_array('orange', $fruits); // $bar value is false You can also use the function array_search() to get the key of a specifi...
This type of Singleton is thread safe, and prevents unnecessary locking after the Singleton instance has been created. Java SE 5 public class MySingleton { // instance of class private static volatile MySingleton instance = null; // Private constructor private MySingleton()...
Map<String, String> num = new HashMap<>(); num.put("one", "first"); if (num.containsKey("one")) { System.out.println(num.get("one")); // => first } Maps can contain null values For maps, one has to be carrefull not to confuse &quot...
The quickCheck function tests a property on 100 random inputs. ghci> quickCheck prop_reverseDoesNotChangeLength +++ OK, passed 100 tests. If a property fails for some input, quickCheck prints out a counterexample. prop_reverseIsAlwaysEmpty xs = reverse xs == [] -- plainly not true for all ...
quickCheckAll is a Template Haskell helper which finds all the definitions in the current file whose name begins with prop_ and tests them. {-# LANGUAGE TemplateHaskell #-} import Test.QuickCheck (quickCheckAll) import Data.List (sort) idempotent :: Eq a => (a -> a) -> a -> Bool ...
This thread-safe version of a singleton was necessary in the early versions of .NET where static initialization was not guaranteed to be thread-safe. In more modern versions of the framework a statically initialized singleton is usually preferred because it is very easy to make implementation mistak...
Array.isArray(obj) returns true if the object is an Array, otherwise false. Array.isArray([]) // true Array.isArray([1, 2, 3]) // true Array.isArray({}) // false Array.isArray(1) // false In most cases you can instanceof to check if an object is an Array. []...
os.access is much better solution to check whether directory exists and it's accesable for reading and writing. import os path = "/home/myFiles/directory1" ## Check if path exists os.access(path, os.F_OK) ## Check if path is Readable os.access(path, os.R_OK) ## Check if path i...
Python makes it very simple to check whether an item is in a list. Simply use the in operator. lst = ['test', 'twest', 'tweast', 'treast'] 'test' in lst # Out: True 'toast' in lst # Out: False Note: the in operator on sets is asymptotically faster than on lists. If you need to use it ...
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...
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
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...
If you want to check that a string contains only a certain set of characters, in this case a-z, A-Z and 0-9, you can do so like this, import re def is_allowed(string): characherRegex = re.compile(r'[^a-zA-Z0-9.]') string = characherRegex.search(string) return not bool(string) ...
Letters 3.0 let letters = CharacterSet.letters let phrase = "Test case" let range = phrase.rangeOfCharacter(from: letters) // range will be nil if no letters is found if let test = range { print("letters found") } else { print("letters not found") }...
Simple check To check if constant is defined use the defined function. Note that this function doesn't care about constant's value, it only cares if the constant exists or not. Even if the value of the constant is null or false the function will still return true. <?php define("GOOD&quo...

Page 2 of 12