Skip to content

Instantly share code, notes, and snippets.

View Kangaroux's full-sized avatar
🌈
feeling colorful

Jessie Kangaroux

🌈
feeling colorful
  • US
View GitHub Profile
@Kangaroux
Kangaroux / main_test.go
Last active June 28, 2024 03:57
Go benchmark optimizations: be careful
/*
This is an example of why you should always inspect your benchmark results,
and why you can't rely on -gcflags=-N to disable all optimizations.
The benchmarks compare copying a 1024 byte array, one from the stack and the
other from the heap. BenchmarkHeapBad will be optimized by the compiler even
with optimization disabled. It sees a constant in make() and that the variable
never escapes, so it converts it to the stack. BenchmarkHeapGood instead passes
the size in as a function argument, avoiding the heap->stack optimization.
@Kangaroux
Kangaroux / hex_to_rgba.go
Last active April 19, 2022 18:22
Golang: Hex to RGB/RGBA
// HexToRGB converts a 24 bit hex number into its corresponding RGB color
// with 100% alpha
//
// clr := HexToRGB(0xFFAA00)
// fmt.Printf("%+v\n", clr) // {R:255 G:170 B:0 A:255}
func HexToRGB(hex uint32) color.RGBA {
r := uint8((hex & 0xFF0000) >> 16)
g := uint8((hex & 0xFF00) >> 8)
b := uint8(hex & 0xFF)
@Kangaroux
Kangaroux / slice_pop.go
Created April 19, 2022 00:06
Golang: Generic pop function for slices
// SlicePop pops an element from a slice and returns the new slice and element.
//
// SlicePop modifies the slice. To preserve the original slice, make a copy.
//
// arr := []int{1, 2, 3}
// arr, elem := SlicePop(arr, 1)
// fmt.Println(arr, elem) // [1 3] 2
func SlicePop[T any](s []T, i int) ([]T, T) {
elem := s[i]
s = append(s[:i], s[i+1:]...)
@Kangaroux
Kangaroux / observable.ts
Created August 17, 2021 18:43
Observable class for typescript
/**
* Simple class for managing callable observers.
*/
export class Observable<Fn extends (...args) => any> {
private observers: Set<Fn> = new Set();
add(o: Fn): void {
this.observers.add(o);
}
@Kangaroux
Kangaroux / .go
Created May 20, 2021 02:40
Golang: Parse JSON field name from a struct field
// ParseFieldName takes a reflected struct field and returns the JSON name for the field,
// as well if the field is ignored by JSON (using `json:"-"`).
func ParseFieldName(f reflect.StructField) (name string, ignore bool) {
tag := f.Tag.Get("json")
if tag == "" {
return f.Name, false
}
if tag == "-" {
@Kangaroux
Kangaroux / gist:46bb853f13a637c071b1f890e1217724
Created July 16, 2020 01:24
Disable automatic audio device switching for linux, ubuntu (USB, controller, dualshock)
# If you want to stop your system from automatically switching audio devices,
# there is a simple edit to your PulseAudio config to disable that feature.
# Open PulseAudio config
sudo vim /etc/pulse/default.pa
# Comment out the line with "load-module module-switch-on-connect"
# Restart PulseAudio
pulseaudio -k
# Declare a variable and use it later.
foo: int
# ...
foo = 5
# Add type hints for function arguments and return types.
def say_hello(name: str) -> str:
return f"Hello {name}!"
pip install mypy
# Check a single file
mypy script.py
# Check a folder
mypy src/
# Check a package
mypy -p my_package
# Old type comments:
foo = "bar" # type: str
# New type hints for Python 3.5+:
foo: str = "bar"
# Declare `foo` to be a string variable.
foo = "bar" # type: str
# Type error!
foo = True