Skip to content

Instantly share code, notes, and snippets.

View RPallas92's full-sized avatar

Ricardo Pallas RPallas92

View GitHub Profile
protocol BaseContext {
var cloudKitApiKey: String {get}
var todosDataSource: TodosDataSource {get}
}
class AppContext: BaseContext {
var cloudKitApiKey = "sjsfAJ&76sdgjhs3434JHS3"
var todosDataSource: TodosDataSource = TodosCloudKitDataSource()
}
class TodosListContext: AppContext {
class CounterViewController: UIViewController {
@IBOutlet weak var label: UILabel?
@IBOutlet weak var minus: UIButton?
@IBOutlet weak var plus: UIButton?
override func viewDidLoad() {
super.viewDidLoad()
typealias State = Int
enum Event {
case addTodo(Todo)
case toggleTodo(Todo)
case setVisibilityFilter(VisibilityFilter)
case storeTodos()
case todosStored(Bool)
}
struct State {
var todos: [Todo]
func loginWithFacebook() -> AsyncResult<Context, String, GenericError> {
return AsyncResult.unfoldTT{ context, continuation in
let loginManager = LoginManager()
loginManager.logIn([ .publicProfile ], viewController: self) { loginResult in
switch loginResult {
case .Failed(let error):
continuation(Result.failure(error))
case .Cancelled:
continuation(Result.failure(LoginCancelledError()))
case .Success(let grantedPermissions, let declinedPermissions, let accessToken):
func reduce(state: State, event: Event) -> State {
switch event {
case .addTodo(let todo):
var newState = state
newState.todos = state.todos + [todo]
return newState
case .toggleTodo(let todo):
var newState = state
newState.todos = state.todos.map { t in
var newTodo = t
enum Event {
case addTodo(Todo)
case toggleTodo(Todo)
case setVisibilityFilter(VisibilityFilter)
}
struct Todo {
var text: String
var completed: Bool
}
enum VisibilityFilter {
case showCompleted
case showAll
}
@RPallas92
RPallas92 / MonadTest.swift
Created November 22, 2017 22:34
facile-it/Monads test
import Monads
func test() {
typealias DeferredResult<T> = Deferred<Result<T, String>>
typealias AsyncResult<T> = Effect<DeferredResult<T>>
struct User {
var name: String
var dateOfBirth: String
}
@RPallas92
RPallas92 / dataTransform.js
Created November 15, 2017 07:06 — forked from davidchambers/dataTransform.js
Data mapping with sanctuary
'use strict';
const R = require('ramda');
const S = require('sanctuary');
const data = {
id: 1,
orderName: 'Order from spain',
teamName: 'Portland penguins',
//You can react to the validation result value, either it's a success or a failure
let success: Validation<String, Int> = Validation.Success(1)
switch(success){
case .Success(let value):
print(value)
case .Failure(let error):
print(error)
}
// ==> Print(1)