Skip to content

Instantly share code, notes, and snippets.

@yang-wei
yang-wei / note.md
Last active April 19, 2021 14:49
Vue.js tips and tricks

Notes: All the examples below are only tested on Vue.js 1.0 (above).

Notes: The examples below use ES6 so it's recommended to use browserify or webpack to easily integrate babel.

when or where can we use this.$el

When you need to access DOM attribute, be careful with the lifecycle. Vue.js has a few useful lifecycle hooks.

Let's say we want to scroll our component to the bottom (imagine it's a long list with overflow-y: auto) once it's instantiate.

@yang-wei
yang-wei / README.md
Last active November 8, 2015 13:06
Awesome screencast
@yang-wei
yang-wei / note.md
Last active September 25, 2016 13:56
Thing I learned from refactoring MySQL database

Execute mysql command from CLI

mysql -u user -p database_name -e "SELECT * FROM users"

Hate to fill in password each time?

Use the .my.cnf. If you don't have create it vi ~/.my.cnf

[client]
@yang-wei
yang-wei / note.md
Last active April 2, 2024 22:27
Clojure Thread-first vs Thread-last Macros

Repost from:

http://yangweilim.com/blog/2015/11/18/clojure-thread-first-vs-thread-last-macros/

In this post, I am going to show you {when|how} to use Clojure marcos, ->> aka thread-last and -> aka thread-first. In some case, -> and ->> may perform the same operation if you do not pay enough attention. So I will also show you what's the difference between them. Note that doc -> and docs ->> din't make sense for me at first, so if same thing happened to you I hope that this post will make it clear.

If I am coding a function in Clojure, I would not think to write in macro firstly(maybe I am still new to it?). Macros like -> and ->> only come in my mind when it comes to refactoring. They are not neccessary in our program but they will make it elegant.

To explain how both of these macros work, let's us solve a quiz together.

[Write a function which calculates factorials.](http://www.4clojure.com/prob

@yang-wei
yang-wei / notes.md
Created November 16, 2015 05:33
Why counterCache cannot be used

I realised the design of database becomes more and more trivial to me when it comes to refactoring of application or scaling. In web application frameworks like Ruby on rails and CakePHP there is a feature known as counterCache. This feature let you record the length of associated data, mostly when they have belongsTo relationship.

This feature is very convenient but it is not applicable to all database design. For example, when we never want to delete user data when she did it, most probably we will add a column like delete_flag in our table. So when user deletes the data, delete_flag will become 1 or true. Our select sql query looks like:

SELECT * FROM table_name WHERE delete_flag=0

Back to counterCache, it mainly do 2 thing.

  • increase the counter when data is inserted
@yang-wei
yang-wei / README.md
Last active April 6, 2021 15:49
ES6 destructing (and rest parameter)

We will first discussed how destructing and rest parameters can be used in ES6 - in arrays and objects. Then we will look at a few examples and also discuss some quiz.

arrays

var array = [1, 2, 3, 4];
var nestedArray = [1, 2, 3, 4, [7, 8, 9]];

var [a, b, c, d] = array;
console.log(a, b, c, d)
@yang-wei
yang-wei / destructuring.md
Last active February 20, 2024 04:40
Elm Destructuring (or Pattern Matching) cheatsheet

Should be work with 0.18

Destructuring(or pattern matching) is a way used to extract data from a data structure(tuple, list, record) that mirros the construction. Compare to other languages, Elm support much less destructuring but let's see what it got !

Tuple

myTuple = ("A", "B", "C")
myNestedTuple = ("A", "B", "C", ("X", "Y", "Z"))
@yang-wei
yang-wei / bmi.cljs
Created February 29, 2016 10:41
ClojureScript BMI Calculator
;; -------------------------
;; BMI Calculator
(def bmi-data
(reagent/atom {:height 180
:weight 60}))
(defn slider [min max val param]
[:input {:type "range"
:value val
:min min
@yang-wei
yang-wei / keyValuePairs.elm
Last active May 1, 2019 05:36
Json.Decode example
import Graphics.Element exposing (Element, show)
import Task exposing (Task, andThen)
import Json.Decode exposing (Decoder, int, string, object3, (:=), at, keyValuePairs)
import Http
type alias Languages =
List (String, Int)
mailbox =
@yang-wei
yang-wei / decode.md
Last active April 2, 2024 20:18
Elm Json.Decode tutorial and cheatsheet

When receiving JSON data from other resources(server API etc), we need Json.Decode to convert the JSON values into Elm values. This gist let you quickly learn how to do that.

I like to follow working example code so this is how the boilerplate will look like:

import Graphics.Element exposing (Element, show)
import Task exposing (Task, andThen)
import Json.Decode exposing (Decoder, int, string, object3, (:=))

import Http