https://github.com/anirban166/scripted-artifice
A collection of tricks and quirks.
https://github.com/anirban166/scripted-artifice
Last synced: 7 months ago
JSON representation
A collection of tricks and quirks.
- Host: GitHub
- URL: https://github.com/anirban166/scripted-artifice
- Owner: Anirban166
- Created: 2022-02-03T04:31:52.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-02-05T01:15:09.000Z (over 3 years ago)
- Last Synced: 2025-01-24T18:09:37.527Z (8 months ago)
- Language: C++
- Homepage:
- Size: 29.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### R
- Call/invoke a function without the name, by first assigning it to be the opening parenthesis:
```r
'(' = median
(1:9) # 5
```
- Load one of two libraries with the same name:
```r
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())`.
- Use`traceback()`, `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:
```cpp
#define varDebug(v) std::cout << #v << " = " << v // << std::endl
```
- Templates
- Generic print:
```cpp
// Single variable:
template
void print(T value) {
std::cout << value << " ";
}
// Multiple/Any number of arguments:
template
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:
```cpp
print("\n", ((1 << 10) << 10), "Kilobytes make up", (1048576 >> 20), "Gigabyte.");
// 1048576 Kilobytes make up 1 Gigabyte.
```
- Mess with a particular bit:
```cpp
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)
```cpp
// 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:
```cpp
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:
```cpp
bool powerOfTwo = n && !(n & (n - 1));
if(powerOfTwo) print(n, "tends to be a power of 2.");
```