Skip to content

Instantly share code, notes, and snippets.

@an0
an0 / quine.swift
Created Jan 2, 2019
Swift Quine without debugDescription
View quine.swift
let q = Unicode.Scalar(34)!
let s =
"""
print("let q = Unicode.Scalar(34)!")
print("let s =")
print(q, q, q, separator: "")
print(s)
print(q, q, q, separator: "")
print(s)
"""
@an0
an0 / quine.swift
Created Jan 2, 2019
Swift Quine with debugDescription
View quine.swift
let s=";print(\"let s=\"+s.debugDescription+s)";print("let s="+s.debugDescription+s)
@an0
an0 / keybase.md
Created Feb 10, 2017
Keybase Proof
View keybase.md

Keybase proof

I hereby claim:

  • I am an0 on github.
  • I am an0 (https://keybase.io/an0) on keybase.
  • I have a public key ASCrKRZNGiZhsiJnfc9Ey5RkFBxrTSUvp-j2nwz_m3v-SQo

To claim this, I am signing this object:

@an0
an0 / WeakSaferThanStrong.md
Created Nov 4, 2016
When weak is safer than strong
View WeakSaferThanStrong.md

http://clang.llvm.org/docs/AutomaticReferenceCounting.html#self:

The self parameter variable of an Objective-C method is never actually retained by the implementation.

But http://clang.llvm.org/docs/AutomaticReferenceCounting.html#semantics:

For __weak objects, the current pointee is retained and then released at the end of the current full-expression. This must execute atomically with respect to assignments and to the final release of the pointee. For all other objects, the lvalue is loaded with primitive semantics.

The whole scheme makes weak references safer then strong references sometimes:

__block NSArray *strongArray = @[@1, @2];
View AsAny.swift
// Discussion: https://twitter.com/an0/status/644176341986856960
["a": "a"] as? [String: Any] // warning: conditional cast from '[String : Any]' to '[String : Any]' always succeeds
let a: Any = ["a": "a"]
let b = a as? [String: String]
let c = a as? [String: AnyObject]
let d = a as? [String: Any]
if let b = b {
View isKindOfType.swift
public func isKindOfType<T>(type: T.Type, value: Any) -> Bool {
return value is T
}
class A {}
class B: A {}
let clsA = A.self
let b = B()
View flatMap.md

box1_of_e1.flatMap(e1 -> box2_of_e2) -> box1_of_e2

  1. Maps every element of the source box to a target box.
  2. If the target box is not empty, unwraps it and puts its content into the source box.
  3. If the target box is empty, ignores it.

So it filters out empty target boxes. But it doesn't filter out empty elements of the source box. The mapping function f must handle empty source elements.

@an0
an0 / CALayerDrawingAPI.md
Last active Apr 18, 2022
CALayer Drawing API
View CALayerDrawingAPI.md

Call Chain:

CALayer.display() --- if delegate implements displayLayer ---> CALayerDelegate.displayLayer(:)
|                                                               /
|                                                              /
else                                                          /
|                                                            /
|                                                           |
v                                                           v
CALayer.drawInContext(:) ---&gt; CALayerDelegate.drawLayer(:, inContext:) ---&gt; UIView.drawRect(:)