Skip to content

Instantly share code, notes, and snippets.

View christopheragnus's full-sized avatar
💭
Furiously coding

Christopher Lam christopheragnus

💭
Furiously coding
View GitHub Profile
@christopheragnus
christopheragnus / gale.js
Created June 20, 2019 07:16
Gale-Shapley algorithm
function stableMatching {
Initialize all m ∈ M and w ∈ W to free
while ∃ free man m who still has a woman w to propose to {
w = first woman on m’s list to whom m has not yet proposed
if w is free
(m, w) become engaged
else some pair (m', w) already exists
if w prefers m to m'
m' becomes free
(m, w) become engaged
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
@christopheragnus
christopheragnus / vuejs-actions-events.js
Created October 29, 2018 14:01
Vuejs Actions / Events
// Calls addToCart method on component:
<button v-on:click="addToCart">
// Shorthand
<button @click="addToCart">
// Arguments can be passed:
<button @click="addToCart(product)">
// To prevent default behaviour eg. page reload
@christopheragnus
christopheragnus / Vuejs-binding.js
Last active October 29, 2018 13:52
Vuejs bindings
<a v-bind:href="url">...</a>
//Shorthand version
<a :href="url">...</a>
// True of false will add or remove attribute:
<button :disabled="isButtonDisabled">
//If isActive is truthy, the class 'active' will appear:
<div :class="{ active: isActive }">
@christopheragnus
christopheragnus / vuejs-list-rendering.js
Last active October 29, 2018 13:41
Vuejs List Rendering
<li v-for="item in items" :key="item.id"> {{ item }}</li>
// To access the position in the array:
<li v-for="(item, index) in items">
// To iterate through objects:
<li v-for="(value, key) in object">
// Using v-for with a component
<cart-product v-for="item in products" :product="item" :key="item.id">
@christopheragnus
christopheragnus / Vuejs-directives.js
Created October 29, 2018 13:36
Vuejs Directives
//Element inserted/removed based on truthiness:
<p v-if="inStock">{{ product }}</p>
<p v-else-if="onSale">...</p>
<p v-else>...</p>
//Toggles the display: none CSS property:
<p v-show="showProductDetails">...</p>
//Two-way data binding:
@christopheragnus
christopheragnus / vuejs-expressions.js
Created October 29, 2018 13:32
Vuejs expressions
<div id="app">
<p>I have a {{ product }}</p>
<p>{{ product + 's' }}</p>
<p>{{ isWorking ? 'YES' : 'NO' }}</p> <p>{{ product.getSalePrice() }}</p>
</div>
@christopheragnus
christopheragnus / closure2.js
Last active October 24, 2018 23:43
Example of an Javascript closure
function outside(x) {
function inside(y) {
return x + y;
}
return inside;
}
fn_inside = outside(3); // Think of it like: give me a function that
// adds 3 to whatever you give it
result = fn_inside(5); // returns 8
@christopheragnus
christopheragnus / closures.js
Created October 24, 2018 23:33
Example of JS closure
function addSquares(a, b) {
function square(x) {
return x * x;
}
return square(a) + square(b);
}
a = addSquares(2, 3); // returns 13
b = addSquares(3, 4); // returns 25
c = addSquares(4, 5); // returns 41