Skip to content

Instantly share code, notes, and snippets.

View domluna's full-sized avatar
🐺
howling time

Dominique Luna domluna

🐺
howling time
View GitHub Profile
@domluna
domluna / dict_iterate.py
Created April 30, 2013 16:23
Better way to iterate over dictionaries
for key, val in dictionary.iteritems():
print key, val
for key in dictionary.iterkeys():
print key
for val in dictionary.itervalues():
print val
@domluna
domluna / append.go
Last active December 20, 2015 21:09
You ever have two slices in Go you wanted to combine? Of course you did. Did you use a for loop? Of course you did. Now what if I told you that for loop was bogus!
a := []int{1,2,3}
b := []int{4,5,6}
// Bad way
for _, number := range b {
a = append(a, number)
}
// Super awesome way
a = append(a, b...)
struct Foo<'self> {
name: ~str,
ref_bar: &'self Bar // Bar lives as long as Foo
// When Foo is freed, Bar is freed
// No memory leaks!
}
struct Bar {
name: ~str
}
@domluna
domluna / unreachable.js
Created November 13, 2013 03:32
Example of unreachable function in JS closure, good to keep in mind for when writing JS code Note browsers need functions in closures to be "explicitly unreachable" in order for garbage collection.
var someFunc = function() {};
function f() {
var some = new someFunc(); // this won't be garbage collected!!!
function unreachable() {};
return function() {};
}
window.f_ = f():
@domluna
domluna / blah.txt
Created July 16, 2014 20:49
2 files > 1 file
blahhing away
@domluna
domluna / ssh.go
Created July 18, 2014 03:32
Playing around with ssh clients in Go
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"os/user"
"time"
@domluna
domluna / resize_linux.txt
Created August 24, 2014 20:45
How to resize linux partitions without cd/usb
Resize Linux partition without any disks/cds/usb things!
Works on Ubuntu 14.04, probably fine on others
1.
sudo losetup /dev/loop0
2. /path/to/file is found with 1., xxx is the number of MiBs to allocate, note: 1MiB ~ 1MB
sudo dd if=/dev/zero bs=1MiB of=/path/to/file conv=notrunc oflag=append count=xxx
@domluna
domluna / pointer.rs
Created September 13, 2014 22:51
Rust pointers, boxing/heap
// Pretend this is an actual big struct in which it would be
// expensive to copy by value.
struct BigStruct {
one: int,
two: int,
// etc
one_hundred: int,
}
// An antipattern would be for the return type to be
@domluna
domluna / macro.rs
Created September 13, 2014 23:02
Example of a macro in rust
#![feature(macro_rules)]
macro_rules! loop_x {
($e: expr) => {
// $e will not interact with this 'x
// refers to the 'x in the outside loop
'x: loop {
println!("Hello!");
$e
}
@domluna
domluna / pre-commit_example
Created September 13, 2014 23:03
Example of pre-commit for git, in this case it's making sure go files are fmted
#!/bin/sh
# Copyright 2012 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
# git gofmt pre-commit hook
#
# To use, store as .git/hooks/pre-commit inside your repository and make sure
# it has execute permissions.
#