Skip to content

Instantly share code, notes, and snippets.

View NachoSoto's full-sized avatar

NachoSoto NachoSoto

View GitHub Profile
@mayoff
mayoff / convert.swift
Last active June 16, 2021 23:07
How to convert from DispatchData to Data without copying the bytes
import Dispatch
import Foundation
var x = 7
let dd = withUnsafeBytes(of: &x, { DispatchData.init(bytes: $0) })
print(dd as? Data) // Case 1: nil
print(dd as? NSData) // Case 2: nil
print(dd as Any as? Data) // Case 3: nil
print(dd as Any as? NSData) // Case 4: .some
print(dd as Any as? NSData as Data?) // Case 5: .some
@chriseidhof
chriseidhof / scanner.swift
Last active February 6, 2019 14:10
Scanning Sequences
import Foundation
// Alternatives to `Scanner` (before: `NSScanner`).
// A scanner only needs a way to peek and to move to the next token.
protocol ScannerProtocol {
associatedtype Token: Equatable
var peek: Token? { get }
mutating func moveToNextToken()
@lattner
lattner / TaskConcurrencyManifesto.md
Last active March 26, 2024 00:33
Swift Concurrency Manifesto
@macguru
macguru / gist:b7a01e3f0e9ae6f350d5
Last active May 10, 2020 20:58
Interface sizes a regular Universal app must support with iOS 11 when supporting all devices. Starts with iPhone 5 and goes up to iPad Pro 12.9".
COMPACT WIDTH (stacked view)
- 320 x 568 pt
-> iPhone 5, 5s
- 320 x 768 pt
-> iPad 9.7" Split Landscape 2/3 right
- 320 x 834 pt
-> iPad 10.5" Split Landscape 2/3 right
- 320 x 1024 pt
-> iPad 9.7" Split Portrait right
@mgp
mgp / android-development-best-practices.md
Created June 19, 2015 01:51
Android development best practices

Prefer Immutability

Our principal goal as programmers is to reduce complexity. At any point in a program, we can reason about the state of a constant trivially -- its state is the state upon assignment. By contrast, the state of a variable can change endlessly.

With judicious use, immutable objects can lead to quicker execution speed and lower memory usage. The hash value of a String, the query parameters of an immutable URL, and the distance traced by an immutable sequence of points are all immutable as well. We can cache their values for later use instead of recompute them on every access. We can also share an immutable object with clients without requiring those clients to defensively copy it.

An immutable value is safe in many ways. For example, String is immutable and so its hashCode value is immutable. Consequently, it is safe to use as a String as a key in a Map: The lower order bits of that hash will never change, and so an inserted key will never reside in the wrong bucket. An immutab

Rx.Observable.create(o => {
console.log("subscribing");
o.onError(new Error("always fails"));
}).retryWhen(attempts -> {
return attempts.zip(Rx.Observable.range(1, 3), (n, i) => i).flatMap(i => {
console.log("delay retry by " + i + " second(s)");
return Rx.Observable.timer(i * 1000);
});
}).subscribe();
@sohamtriveous
sohamtriveous / build.gradle
Last active February 1, 2017 16:36
A sample RxJava-Android and retrolambda gradle configuration for Android Studio on Mac OS.
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.example.databaseexample"
minSdkVersion 14
targetSdkVersion 22
@airspeedswift
airspeedswift / spelling.swift
Last active July 17, 2019 07:22
Norvig Spellchecker in Swift
/// Translation of [Peter Norvig's spell checker](http://norvig.com/spell-correct.html) into Swift.
/// Sample input corpus [here](http://norvig.com/big.txt)
import Foundation.NSString // purely for IO, most things done with Swift.String
// pythony slicing
postfix operator ..< { }
prefix operator ..< { }
postfix func ..<<I: ForwardIndexType>(lhs: I) -> RangeStart<I> { return RangeStart(start: lhs) }
prefix func ..<<I: ForwardIndexType>(rhs: I) -> RangeEnd<I> { return RangeEnd(end: rhs) }
/*
This is a demonstration of how to implement the Optional type in Swift.
The name 'Maybe' is taken from Haskell, but the two cases 'None' & 'Some'
are the same as Swift's Optional type (Haskell uses 'Nothing' & 'Just').
The Maybe type conforms to NilLiteralConvertible, as does Swift's
Optional type. This allows a Maybe value to be constructed from 'nil'.
One aspect of Swift's Optional type which can't be reproduced is
'implicit Optional wrapping'. Here's an example:
@JaviLorbada
JaviLorbada / FRP iOS Learning resources.md
Last active February 21, 2024 01:16
The best FRP iOS resources.

Videos