Skip to content

Instantly share code, notes, and snippets.

Avatar
😵
拖延癌末期,不爱回复

Kem Chen kemchenj

😵
拖延癌末期,不爱回复
View GitHub Profile
View summary.md
View sessions.js
var sessions = []
var sessionsNodes = document.querySelectorAll("#sessions section.video-container")
for (var i=0;i<sessionsNodes.length;i+=1) {
var section = sessionsNodes[i]
var date = section.querySelector("section.sticky section section section span span").textContent
var sessionsInDateNodes = section.querySelectorAll("ul.videos li.video div.grid div.row")
var sessionsInDate = []
View index.swift
struct SampleCollection<C: RandomAccessCollection>: RandomAccessCollection {
let storage: C
let sampleInterval: Int
var startIndex: C.Index { storage.startIndex }
var endIndex: C.Index { storage.endIndex }
func index(before i: C.Index) -> C.Index {
if i == endIndex {
return storage.index(endIndex, offsetBy: -storage.count.remainderReportingOverflow(dividingBy: sampleInterval).partialValue)
} else {
@kemchenj
kemchenj / difference.md
Last active Nov 24, 2019
Swift 与 Objective-C
View difference.md

Objective-C 的核心是消息机制,当年想要解答的问题是“什么是面向对象”,其实我觉得 Objective-C 是更加纯粹和理想化的面向对象,而 C 是它实用主义的一面。

消息机制带来的是低耦合,极度灵活的的函数调用,可以很便捷地 hack 掉 SDK 的内部实现,亦或者是与其它语言的进行桥接,所以 OC 社区讨论的代码技巧都是围绕着消息机制,各种 Swizzle,各种语言的桥接等等。

但我们的代码里对内的逻辑天生就足够内聚,并不需要这样的灵活性,它更适合去处理对外的,低耦合的场景,例如 rpc,微内核,线程通信;而且消息机制的面向对象本质上是面向继承,不容易组合。

Swift 与 OC 的差异代表着这些年编译器和类型理论的进步,在类型论的基础上搭建范型,Nullability,Mutability,错误处理,还有以后可能会有的 async/await,都是在把之前代码逻辑里的东西抽象到语言层面进行处理,并且显式地声明出来,Swift 标准库的核心思路也是使用类型,描述对象。

所以我们的函数签名变得越来越长,难读懂却易使用,Swift 的程序员喜欢面向类型去做文章,strong-type / type-safe 是很多开源库最喜欢使用的词,Swift 程序员会更习惯基于已有的 interface 去做 extension。

@kemchenj
kemchenj / gitment2gitalk.rb
Created Aug 11, 2019
Gitment 迁移到 Gitalk 的脚本,处理 label 的重命名
View gitment2gitalk.rb
require 'openssl'
require 'json'
require 'net/http'
require 'cgi'
require 'digest'
repo = "kemchenj.github.io"
owner = "kemchenj"
account = "account"
password = "password"
View CodeTextField.swift
import UIKit
class CodeTextField: UITextField, UITextFieldDelegate {
let codeLength: Int
var characterSize: CGSize
var characterSpacing: CGFloat
let textPreprocess: (String) -> String
let validCharacterSet: CharacterSet
View CustomArray.swift
import Foundation
struct CustomArray<Element>: MutableCollection, RangeReplaceableCollection, RandomAccessCollection {
typealias Index = Int
typealias SubSequence = CustomArray<Element>
private var storage = NSMutableArray()
var startIndex: Int {
View DimBackgroundVC+PresentAnimator.swift
//
// DimBackgroundVC+Animator.swift
// guru_iOS
//
// Created by kemchenj on 2018/10/4.
// Copyright © 2018 gelonghui. All rights reserved.
//
import UIKit
View json2mson.rb
require 'json'
def base_type?(element)
element.is_a?(Numeric) || element.is_a?(String) || element.is_a?(TrueClass) || element.is_a?(FalseClass)
end
def base_type_array?(arr)
base_type?(arr.first)
end
View TicTacToe.c
//
// main.c
// jkasdfj
//
// Created by kemchenj on 2018/7/13.
// Copyright © 2018 kemchenj. All rights reserved.
//
#include <stdio.h>