Skip to content

Instantly share code, notes, and snippets.

View dani-mp's full-sized avatar

Daniel Martín dani-mp

View GitHub Profile
@dani-mp
dani-mp / ReactNavigation4+Context.js
Last active February 25, 2020 13:33
React Navigation (v1/2/3/4) Navigator wrapped in a React Context Provider
// So we have a context...
const Context = React.createContext();
class Provider extends React.Component {
addTodo = todo => this.setState(({ todos }) => ({ todos: [...todos, todo] }));
state = {
todos: [],
addTodo: this.addTodo
@dani-mp
dani-mp / AsyncReSwift.swift
Last active November 14, 2017 08:52
Swift Playground showcasing the use of ReSwift middleware to perform asynchronous operations, in contrast of other possible approaches.
import UIKit
import PlaygroundSupport
import ReSwift
// API
protocol API {
func loadUsers(completion: @escaping (([User]) -> Void))
}
@dani-mp
dani-mp / ProducerConsumer.swift
Created March 6, 2017 20:13
Producer-consumer written in Swift
// Note you can copy this code andn paste it in a Xcode Playground
// Simulates working and introduces some randomness
func randomSleep() {
sleep(arc4random_uniform(2))
}
// Item to be produced/consumed
struct Item {
let id: Int
@dani-mp
dani-mp / ReduceExample.swift
Created March 5, 2017 21:48
Reduce example
func arrayToTuple(array: [Int]) -> (Int, Int, Int) {
return array.reduce((0, 0, 0)) { temp, item in
switch item {
case 0..<10: return (temp.0 + 1, temp.1, temp.2)
case 10..<100: return (temp.0, temp.1 + 1, temp.2)
default:
return item > 100 ? (temp.0, temp.1, temp.2 + 1) : temp
}
}
}
@dani-mp
dani-mp / gist:fdb81a8e28c1b414b85d
Last active August 29, 2015 14:21
AppDelegate to run unit tests faster in Swift.
1. Comment out @UIApplicationMain in your AppDelegate.
2. Create main.swift file with the following code:
import Foundation
import UIKit
let isRunningTests = NSClassFromString("XCTestCase") != nil
if isRunningTests {