Anirban166 / Scripted-Artifice

A collection of tricks and quirks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

R

  • Call/invoke a function without the name, by first assigning it to be the opening parenthesis:
'(' = median
(1:9) # 5
  • Load one of two libraries with the same name:
lattice <- "ggplot2"
library(lattice) #lattice, check with ?xyplot()
library(lattice, character.only = TRUE) #ggplot2, check with ?aes()
  • Inspect the structure and contents of an object using a mix of functions such as str(), attributes(), class() and unclass().
  • Use backticks for invalid (numeric constants, string with whitespaces or special symbols) column names.
  • Search for a file using Tab inside quotes (RStudio).
  • Print an assignment by enclosing it within parantheses.
  • Clear environment variables using rm(list = ls()).
  • Usetraceback(), broswer() (breakpoints), conditions (stopifnot("message" = bad.condition) and prints for debugging.
  • Summon a pipe: Ctrl+Shift+M. Chain dplyr functions (for instance, mtcars %>% rename(i = mpg, j = cyl) %>% select(i, j) %>% head() and mtcars %>% select(contains("hp")) %>% tail()) to create cool combos.
  • Use options (for instance, options(error = recover) and options(max.print = 1e+n)).

C++

  • Macros
    • Stringify for debugging:
    #define varDebug(v) std::cout << #v << " = " << v // << std::endl
  • Templates
    • Generic print:
    // Single variable:
    template<typename T>
    void print(T value) {
      std::cout << value << " ";
    }  
    // Multiple/Any number of arguments:
    template<typename ...T>
    void print(T&&... args) {
      ((std::cout << args << " "), ...);
    }

General

  • Bit manipulation
    • & over % for parity checks - n & 1 would evaluate to be true (a boolean to be used inside an if-conditional over the ordinary n % 2 != 0) if n is odd (given that the least significant bit is always set if the number is not even).
    • <</>> for mul/div operations with the powers of two. Ideal for conversions in between the units of information:
    print("\n", ((1 << 10) << 10), "Kilobytes make up", (1048576 >> 20), "Gigabyte.");
    // 1048576 Kilobytes make up 1 Gigabyte. 
    • Mess with a particular bit:
    n |= 1 << bitNum; // Set
    n &= ~(1 << bitNum); // Clear
    n ^= 1 << bitNum; // Toggle, much like https://stackoverflow.com/a/60946658/11422223
    • RGB breakdown: (conversions to and from Hex or RGBA possible as well)
    // Considering red-green-blue to be represented by three sets of 8 bits, from left to right or from the MSB to the LSB:
    int blue = rgb & 0xFF0000, green = (rgb >> 8) & 0xFF00, red = (rgb >> 16) & 0xFF;
    rgbAgain = (red << 16) + (green << 8) + blue; 
    • Given that the difference between the lowercase and uppercase versions of ASCII alphabets in integers is 32, a quick switch to those would be to respectively set or clear the sixth bit (since 26 - 1 = 32, with the rightmost bit being 20) from the right using appropriate characters:
    print((char)('a' & '_'), (char)('a' | ' ')); // A a
    // As expected, toggle works as well:
    std::cout << (char)('X' ^ ' ') << (char)('d' ^ ' '); // xD 
    • Check for a number to be a power of 2:
    bool powerOfTwo = n && !(n & (n - 1));
    if(powerOfTwo) print(n, "tends to be a power of 2.");

About

A collection of tricks and quirks.


Languages

Language:C++ 100.0%