Skip to content

Instantly share code, notes, and snippets.

View yanfeng42's full-sized avatar
💌
I may be slow to respond.

Macro yanfeng42

💌
I may be slow to respond.
View GitHub Profile
@yanfeng42
yanfeng42 / nodejs_port_forward_by_http.js
Created July 14, 2016 14:53
http port forward for nodejs,ie access jenkins by http://yourhostname/jenkins,which located at http://yourhostname:8080/jenkins
'use strict';
/* Sample
forward :8087/jenkins to :8080/jenkins.
In product,the entityPort can be 80,so u can access jenkins
by http://yourhostname/jenkins
Good Luck! All Done Just By NodeJS!
*/
@yanfeng42
yanfeng42 / count-change-gv.scm
Created December 28, 2020 18:49
SICP练习1.14. 使用 GrapViz 可视化 零钱问题的 递归计算步骤.
(define (count-change amount)
(cc amount 5 0)
)
(define (cc amount kinds-of-coins depth)
(define from-node (cc-node-depth amount kinds-of-coins depth))
(display (cc-node-depth-label amount kinds-of-coins depth))
(cond
((= amount 0) 1)
((or (< amount 0) (= kinds-of-coins 0)) 0)
@yanfeng42
yanfeng42 / 1.14_1.scm
Created December 30, 2020 16:33
sicp 练习1.14 (2) 计算步数增长的阶(时间复杂度)
(define (count-change amount)
(cc amount 5 0)
)
(define (cc amount kinds-of-coins depth)
(define from-node (cc-node-depth amount kinds-of-coins depth))
(display (cc-node-depth-label amount kinds-of-coins depth))
(cond
((= amount 0) 1)
((or (< amount 0) (= kinds-of-coins 0)) 0)
@yanfeng42
yanfeng42 / logB.scm
Created December 31, 2020 05:27
可能很有用的工具方法: 计算任意底数的对数
(define logB
(lambda (x B)
(/ (log x) (log B))))
(define (log3 x)
(logB x 3)
)
; (log3 9) -> 2.0
@yanfeng42
yanfeng42 / sicp_1.17.scm
Created January 3, 2021 17:53
sicp 1.17 尾递归解法的证明
; ref: http://community.schemewiki.org/?sicp-ex-1.17
(define (fast-mult-by-add a b)
(define (double x) (+ x x))
(define (halve x) (/ x 2))
(define (helper a b product) ;; "add a" b times
(cond ((= b 0) product)
((even? b) (helper (double a) (halve b) product))
(else (helper a (- b 1) (+ a product)))))
(helper a b 0))
@yanfeng42
yanfeng42 / fix_grpc_int64_convert_errr.swift
Created January 7, 2021 10:05
Swift 基于 Codable 来解决 grpc 自动将 int64 转为 String 带来的不便.
final class SampleReply: Codable {
let code: Int32 // 状态码
private let _pageToken: String
lazy var pageToken: Int64 = { return Int64(self._pageToken) ?? 0 }()
private enum CodingKeys: String, CodingKey {
case code
case _pageToken = "pageToken"
}
}
@yanfeng42
yanfeng42 / my-gcd.scm
Created January 8, 2021 01:47
sicp 1.2.5 最大公约数(gcd) 的欧几里得算法
(define (my-gcd a b)
(if (= b 0)
a
(gcd b (remainder a b)))
)
@yanfeng42
yanfeng42 / application_order_or_regular_order.scm
Created January 8, 2021 02:12
sicp 1.5 检测 解释器 是 正则序还是应用序求值
(define (p) (p))
(define (test x y)
(if (= x 0)
0
y)
)
; 应用序求值(先求值参数而后应用), 会陷入死循环; 正则序求值(完全展开而后归约), 会输出0.
(test 0 (p))
@yanfeng42
yanfeng42 / fermat-test.scm
Last active January 21, 2021 02:00
sicp 1.2.6, 关于费马小定理实现中的, 若a^n % b = x, 则: a^2n % b = x^2 % b 的简易证明
;费马小定理: 如果 n 是一个素数, a是小于n 的任意正整数, 那么 a的n次方 与 a 模n同余(除以 n 的余数相同).
; 计算一个数的幂, 对另一个数取模的结果.
(define (fermat-test n)
(define (try-it a)
; 因为 a 小于n, 所以 a/n 的余数就是 a.
(= (expmode a n n) a)
)
(try-it (+ 1 (random (- n 1))))
)
@yanfeng42
yanfeng42 / expmode-better.scm
Last active January 24, 2021 18:32
sicp 1.2.6 费马小定理 核心代码的 尾递归优化版实现 与 实现正确性的证明(with todo)
; 书上的初版实现
(define (expmode base exp m)
(cond ((= exp 0) 1)
((even? exp)
(remainder (square (expmode base (/ exp 2) m)) m))
(else
(remainder (* base (expmode base (- exp 1) m)) m))
)
)