Skip to content

Instantly share code, notes, and snippets.

View lqt0223's full-sized avatar

lqt0223 lqt0223

  • Shanghai, China
View GitHub Profile
@lqt0223
lqt0223 / matrix.vert
Created June 13, 2019 03:37
37 perspective, view matrix derivation in vertex shader program
uniform mat4 u_modelMatrix;
uniform vec2 u_resolution;
uniform vec3 u_camera;
attribute vec4 a_position;
varying vec4 v_position;
attribute vec4 a_color;
varying vec4 v_color;
attribute vec3 a_normal;
varying vec3 v_normal;
@lqt0223
lqt0223 / xml_parse.js
Last active May 9, 2019 03:58
20 XML parser
// A coding exercise of implementing a XML parser
// Caution: this is not a full implementation. Some features(ex. self-closing tag) are not implemented.
// Further tests need to be done
function xml_parse(xml) {
var i = 0
var ch = xml[i]
var focusedNode
function next(c) {
if (c) {
@lqt0223
lqt0223 / latlon.md
Last active May 10, 2018 01:14
36 the haversine function and distance between locations

Deriving the Haversine Formula

The haversine formula is useful in calculating the distance between two locations on the Earth, when the latitudes & longitudes of the two locations are provided.

An implementation of calcDistance function based on haversine formula in JavaScript is as follows: (the degToRad function is solely a unit converter)

function degToRad(degrees) {
  return degrees * Math.PI / 180;
}
@lqt0223
lqt0223 / adder.js
Created April 28, 2018 10:02
35 binary adder
// a half-adder takes 2 binary bits as input, and return as result the sum and carry flag
function halfAdder(a, b) {
// the sum is 0 when two bits are the same (1 + 1 or 0 + 0)
var s = a ^ b
// the carry flag is set when 1 + 1
// the bitwise expression !(a ^ b) is for testing equality between a and b
var c = !(a ^ 1) && (!a ^ b)
return [c, s]
}
@lqt0223
lqt0223 / p&c.js
Last active April 19, 2018 16:10
34 a more declarative permutation & combination
// a more declarative algorithm for finding permutations & combinations of a sequence
// first, we introduce a higher-order fuction: flat-map
// it works like map, but will flatten the result by one level (only one level, not recursive)
// ex.
// - map the procedure (x) => [x, 2 * x] onto array [1, 2, 3] will produce
// a 2-dimensional array [[1, 2], [2, 4], [3, 6]]
// - instead, flat-map the procedure (x) => [x, 2 * x] onto array [1, 2, 3] will produce
// a flattened 1-dimensional array [1, 2, 2, 4, 3, 6]
function flatMap(arr, proc) {
@lqt0223
lqt0223 / sequence.js
Last active April 13, 2018 08:31
17 Running asynchronous jobs in sequence (without paralleling) - A Promise based implementation
// Running asynchronous jobs in sequence (without paralleling)
// A Promise based implementation
// The 'sequence' function receive an array and an iteratee as parameters.
// The iteratee is a function that returns a promise.
// You should incapsulate your desired asynchronous job in this function.
// The array is the parameters for the iteratee function in order.
// The 'sequence' function returns a promise to make it thenable.
// The promise will resolve all results of asynchronous jobs in an array,
@lqt0223
lqt0223 / pm.js
Last active April 8, 2018 11:05
33 pattern matching in a recursive style
/*
pattern matching
- a pattern is an expression with variables
- a datum is another expression to be tested if it matches the provided pattern
- a dictionary is a data-structure used to record and test bindings between
variables in pattern and values in datum
a pattern matching procedure takes a pattern, a datum for testing and a dictionary as input
- if the datum matches the pattern, the procedure will output an extended dictionary with
the new bindings
@lqt0223
lqt0223 / rdp.js
Last active April 2, 2018 13:10
32 recursive descent parsing - arithmetic expression as an example
/*
parsing arithmetic expressions containing 1 digit integers as operands
and '+','-','*','/' as operators
context free grammar for arithmetic expressions with left-recursion eliminated:
- expr -> term + expr | term - expr | term
- term -> factor * term | factor / term | factor
- factor -> num | ( expr )
*/
@lqt0223
lqt0223 / curry.js
Created March 26, 2018 08:26
30 currify a function
var curry = (f) => {
var argc = f.length
var _curry = (fn) => {
return (...args) => {
if (args.length == argc) {
return fn(...args)
} else {
return fn.bind(null, ...args)
}
}
@lqt0223
lqt0223 / queens.js
Created March 13, 2018 05:31
29 n-queens puzzle - the backtracking solution
// the backtracking (one kind of recursion) code paradigm:
// - build a helper function with extra arguments to pass on choices and other useful information during recursive calls
// - in the helper function, the base case is for handling the result after series of choices
// - in the helper function, the recursive case is usually in the 'choose-explore-unchoose' pattern
function queen(size) {
var result = queenHelper(size, [], [])
return result
}