Skip to content

Instantly share code, notes, and snippets.

View why-jay's full-sized avatar

YJ Yang why-jay

View GitHub Profile
@why-jay
why-jay / random_parser_api.json
Created December 8, 2014 05:08
"var x = 1" in Mozilla Parser API
{
"type": "Program",
"body": [
{
"type": "VariableDeclaration",
"declarations": [
{
"type": "VariableDeclarator",
"id": {
"type": "Identifier",
@why-jay
why-jay / gist:e6c1946b5d8808d81f6a
Created December 8, 2014 05:14
A Mozilla Parser API snippet in Rust
pub enum Pattern<'a> {
Expression(Expression<'a>),
ObjectPattern(ObjectPattern<'a>),
ArrayPattern(ArrayPattern<'a>)
}
@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")
}
}
class Foo : IDisposable {
// blah
}
// a couple thousand lines later...
using(Foo f = new Foo()) {
// blah
}
@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!
}
// 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: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();
}
}
vector<pair<const char *,const char *>> tokenize_string2(const string &text);
auto v = tokenize_string2(get_input_string()); // Disaster strikes!
munge(v);
JPanel panel = new JPanel();
panel.add(new JButton(new AbstractAction("name of button") {
public void actionPerformed(ActionEvent e) {
//do stuff here
}
}));
let panel = document.createElement('div');
let button = document.createElement('button');
button.addEventListener('click', function actionPerformed(e) {
// do stuff here
});
panel.appendChild(button);