Skip to content

Instantly share code, notes, and snippets.

View why-jay's full-sized avatar

YJ Yang why-jay

View GitHub Profile
names.filter(name => name.indexOf('A') !== -1 );
function filterNamesWithA(names) { // names is an array
return names.filter(function (name) { return name.indexOf('A') !== -1; });
}
let panel = document.createElement('div');
let button = document.createElement('button');
button.addEventListener('click', function actionPerformed(e) {
// do stuff here
});
panel.appendChild(button);
JPanel panel = new JPanel();
panel.add(new JButton(new AbstractAction("name of button") {
public void actionPerformed(ActionEvent e) {
//do stuff here
}
}));
vector<pair<const char *,const char *>> tokenize_string2(const string &text);
auto v = tokenize_string2(get_input_string()); // Disaster strikes!
munge(v);
@why-jay
why-jay / gist:0a31ac34200667464469
Created December 10, 2014 13:05
Resource release in finally
// borrowed from docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
static String readFirstLineFromFileWithFinallyBlock(String path) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
if (br != null) br.close();
}
}
// We need nested scopes here, because the intermediate results can keep
// large chunks of memory alive and we want to free them as soon as
// possible to keep the peak memory usage low
let (outputs, trans, sess) = {
let (outputs, expanded_crate, id) = {
let krate = phase_1_parse_input(&sess, cfg, input);
if stop_after_phase_1(&sess) { return; }
let outputs = build_output_filenames(input,
outdir,
output,
@why-jay
why-jay / gist:f1fd2700e1cf9173d273
Last active August 29, 2015 14:11
Box operator in Rust
struct Point {
x: f64,
y: f64,
}
fn foo() {
{
let p: Box<Point> = box Point { x: 1.0, y: 2.0 };
} // p's heap memory is released!
}
class Foo : IDisposable {
// blah
}
// a couple thousand lines later...
using(Foo f = new Foo()) {
// blah
}
@why-jay
why-jay / gist:443650e22cda9c3e9535
Created December 8, 2014 06:32
Demo of Rust's Option<T> Type
fn foo(x: Option<int>) {
match x {
Some(val) -> println!("{}", val),
None -> println!("Sad")
}
}