Skip to content

Instantly share code, notes, and snippets.

View RPallas92's full-sized avatar

Ricardo Pallas RPallas92

View GitHub Profile

Business Models

Advertising

Models Examples
Display ads Yahoo!
Search ads Google
//Check if the password is long enough
func isPasswordLongEnough(_ password:String) -> Validation<[String], String> {
if password.characters.count < 8 {
return Validation.Failure(["Password must have more than 8 characters."])
} else {
return Validation.Success(password)
}
}
func validatePassword(username: String, password:String) -> [String]{
var errors:[String] = []
if password.characters.count < 8 {
errors.append("Password must have more than 8 characters.")
}
if (password.range(of:"[\\W]", options: .regularExpression) == nil){
errors.append("Password must contain a special character.")
//Validate min length
func minLength(_ value:String, min:Int, fieldName:String) -> Validation<[String], String>{
if(value.characters.count < min){
return Validation.Failure(["\(fieldName) must have more than \(min) characters"])
} else {
return Validation.Success(value)
}
}
//Validate match a regular expression
//Validate min length
func minLength(_ value:String, min:Int, fieldName:String) -> Validation<[String], String>{
if(value.characters.count < min){
return Validation.Failure(["\(fieldName) must have more than \(min) characters"])
} else {
return Validation.Success(value)
}
}
//Validate match a regular expression
//The fmap function is only applied on Success values.
let success: Validation<String, Int> = Validation.Success(1)
success.fmap{ $0 + 1 }
// ==> Validation.Success(2)
let failure: Validation<String, Int> = Validation.Failure("error")
failure.fmap{$0 + 1}
// ==> Validation.Failure("error")
//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)
@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',
@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
}
struct Todo {
var text: String
var completed: Bool
}
enum VisibilityFilter {
case showCompleted
case showAll
}