Skip to content

Instantly share code, notes, and snippets.

View lqt0223's full-sized avatar

lqt0223 lqt0223

  • Shanghai, China
View GitHub Profile
@lqt0223
lqt0223 / promise.js
Last active April 12, 2024 12:23
42. A promise implementation that is A+ conformant
// (Tested on Node.js 18.15.0) A promise implementation that is A+ conformant
// To implement something promise-ish (ignoring all edge-cases from A+ spec), there are some guidelines below:
// - manage the state of promise object, to avoid transition between two settled state
// - use 'then' method to store callbacks for resolving / rejecting the promise.
// - implement 3 kinds of chaining behaviors of a promise
// - when resolving a promise, recursively unwrap until a non-thenable value is found
// - a promise can invoke then (to add callbacks) multiple times. When the promise is settled, all cbs should be invoked sequentially
// - the propagation of promise chain
// warn: this does not check if val.then is a function
@lqt0223
lqt0223 / co.js
Created November 18, 2023 09:36
41 async-await style in generator
function apiReq(result, delay) {
return new Promise(resolve => {
setTimeout(() => {
resolve(result)
}, delay*1000)
})
}
// the generator function version of async-await programming scheme
// function* -> async
@lqt0223
lqt0223 / parse.js
Created November 16, 2023 06:38
40 parsing LISP like expression
/*
* a function that parses LISP like expression into AST
* the CFG will be like
* C = ( op args ) --- an expression is enclosed in a parenthesis, with 'op' as head, and 'args' as tail
* args = arg* --- an args (argument list) contains zero or variadic-length arguments
* arg => num | C --- an arg is either a num or an expression
* op = +|-|*|/
* num = [0-9]*
*/
const parse = function(s) {
@lqt0223
lqt0223 / curry.js
Created July 7, 2023 12:18
39 currying
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) {
return fn.call(this, ...args)
} else {
return curried.bind(this, ...args)
}
}
}
@lqt0223
lqt0223 / co.js
Created July 7, 2023 12:16
38 a simple co(async generator function runner)
function sleep(ms, message) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(message)
}, ms)
})
}
function co(fn) {
const generator = fn()
@lqt0223
lqt0223 / knapsack.js
Created April 11, 2017 11:52
20 0-1 Knapsack problem in JavaScript
/* 0-1 knapsack problem
For an overall introduction to knapsack problem, see https://en.wikipedia.org/wiki/Knapsack_problem
Function name: knapsack
Param:
items: an array of {w: v:} (where 'w' stands for weight, and 'v' stands for value)
capacity: a positive integer number
Will return max sum value that can reach, and the chosen subset to add up to the value.
@lqt0223
lqt0223 / g.md
Last active January 10, 2023 00:08
14 Simple algorithm for chromatic aberration effect in JavaScript

Chromatic aberration effect - a simple implementation in JavaScript

A chromatic aberration is an optical effect caused by one or more color channels being displaced. Although it is an optical failure and should be avoided for displaying or image capturing devices, chromatic aberration can be used to make graphics be more realistic in some other applications like 3D games.

The following codes is an example of implementing the effect using ImageData API of HTML5 canvas. By fetching and manipulating pixel data of the image, the chromatic aberration effect is easy to achieve.

<html>
<body>
<canvas id="canvas"></canvas>
@lqt0223
lqt0223 / g.md
Last active September 7, 2022 07:56
04 Google OAuth2.0 in Java with no library

使用Java 无类库完成Google OAuth2.0验证

对于我们这种初学者来说,涉及到网络的编程学问真的很多。如果需要了解每一个网络请求或服务器的细节的话,协议、安全验证、跨域、请求头的每项的意义之类,需要了解的东西真的非常多。

这篇博文,我想从很简单的角度出发,总结一下网络编程中最基本的两项任务ーー建立一个服务器、发起一个请求ーー在Java中的实现。

以实现一个实际所需要的功能来进行学习,是很有效的学习方法。所以这一次我选择的课题就是:用Java建立一个服务器端程序,用它来完成Google的OAuth2.0验证。

这个小课题帮我GET到的Java小技能主要有以下这些,还是收获挺大的。

@lqt0223
lqt0223 / _readme.txt
Last active July 8, 2022 02:04
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
@lqt0223
lqt0223 / json_parser.js
Created December 27, 2017 09:55
18 JSON parser
// This is a coding exercise of implementing a parser. Some minor parsing capabilities are not implemented.
// The algorithm are mainly from Douglas Crockford's 'https://github.com/douglascrockford/JSON-js/blob/master/json_parse.js'
function json_parse(str) {
var i = 0
var ch = str[i]
// core function that recursively called to move forward scanning pointers and accept tokens
function next(c) {
if (c) {