{{ message }}

Instantly share code, notes, and snippets.

• Shanghai, China
Created Jun 13, 2019
37 perspective, view matrix derivation in vertex shader program
View matrix.vert
 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;
Last active May 10, 2018
36 the haversine function and distance between locations
View latlon.md

# 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;
}```
Created Apr 28, 2018
 // 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] }
Last active Apr 19, 2018
34 a more declarative permutation & combination
View p&c.js
 // 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) {
Last active Apr 8, 2018
33 pattern matching in a recursive style
View pm.js
 /* 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
Last active Apr 2, 2018
32 recursive descent parsing - arithmetic expression as an example
View rdp.js
 /* 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 ) */
Last active Mar 31, 2018
31 the eval-apply metacircular evaluator for Lisp implemented in JavaScript
 This gist contains the implmentation of a eval-apply metacircular evaluator for Lisp. Brief introduction on the following files: test.js - where you play with the evaluator index.js - where the global environment is set and the AST is constructed and evaulated env.js - the data structure for environment used in evaluation parse.js - tokenizing and parsing Lisp code eval.js - where the eval / apply mechanisms are implemented primitives.js - where some primitive values and procedures of Lisp language are implemented
Created Mar 26, 2018
30 currify a function
View curry.js
 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) } }
Created Mar 13, 2018
29 n-queens puzzle - the backtracking solution
View queens.js
 // 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 }
Last active Mar 12, 2018
28 n-queens puzzle - a declarative solution
View queens.js
 function queen(n) { // a array of numerical sequence from 0 to n - 1 var sequence = enumerateInterval(0, n - 1) // returns the sub-solution for n-queen's problem while placing k(k < n) queens on the first k rows of the board function queenRow(k) { if (k == 0) { return [[]] } else { var restQueens = queenRow(k - 1) // just place the nth queen on any place of the new row to generate a bunch of new solutions
You can’t perform that action at this time.