This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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:]...) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 == "-" { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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}!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pip install mypy | |
# Check a single file | |
mypy script.py | |
# Check a folder | |
mypy src/ | |
# Check a package | |
mypy -p my_package |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Old type comments: | |
foo = "bar" # type: str | |
# New type hints for Python 3.5+: | |
foo: str = "bar" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Declare `foo` to be a string variable. | |
foo = "bar" # type: str | |
# Type error! | |
foo = True |
NewerOlder