Advertisement
Advertisement

More Related Content

Advertisement

Recently uploaded(20)

Advertisement

Data Pipelines in Swift

  1. Foward'|>'Pipe h"p://github.com/jarsen/Pipes
  2. Method'Chaining [1,2,3,4,5].filter(isEven).map({$0 * 3}).reduce(0, combine: +) // 18
  3. Method'Chaining Array(1...5) .filter(isEven) .map({$0 * 3}) .reduce(0, combine: +) // 18
  4. Func%on!Chaining reduce(map(filter(1...5, isEven), {$0 * 3}), 0, +) // 18
  5. "Hello" |> println • Meet%the%Forward%Pipe%Operator%|> • Popular%in%languages%such%as%F#%and%Elixir. • Simply%applies%le>%hand%side%of%expression%as%first%argument%of% funcAon%on%the%right
  6. The$Forward$Pipe$Operator 2 |> increment // 3 5 |> (divide, 10) // 0.5 10 |> (divide, 5) // 2
  7. Data$Pipelines // the result of each step becomes first arg in the next 1...5 |> (filter, isEven) // [2, 4] |> (map, {$0 * 3}) // [6, 12] |> (reduce, 0, +) // 18
  8. Data$Pipelines students // filter to students with last name L* |> (filter, { $0.lastName.hasPrefix("L") } // get list of all their first names |> (map, Student.firstName) // make all the names uppercase |> (map, String.uppercaseString)
  9. Op#onal<T>+Method+Chaining let animalNoiseMap = ["cow":"moo", "dog":"woof", "cat":"meow"] animalNoiseMap["dog"]?.uppercaseString // .Some("WOOF") animalNoiseMap["fox"]?.uppercaseString // .None
  10. Op#onal<T>+Func%on+Chaining let animalNoiseMap = ["cow":"moo", "dog":"woof", "cat":"meow"] map(animalNoiseMap["dog"]) { count(filter($0, isVowel)) } // .Some(2) map(animalNoiseMap["fox"]) { count(filter($0, isVowel)) } // .None
  11. Op#onal<T>+Pipeline animalNoiseMap["dog"] // .Some("woof") |> (filter, isVowel) // .Some(["o", "o"]) |> count // .Some(2)
  12. Op#onal<T>+Pipeline animalNoiseMap["fox"] // .None |> (filter, isVowel) // .None |> count // .None
  13. Op#onal<T>+Pipeline [2, 4, 6, 8, 10] |> (map, increment) // [3, 5, 7, 9, 11] |> (find, 7) // .Some(2) |> isEven // .Some(true)
  14. Op#onal<T>+Pipeline [2, 4, 6, 8, 10] |> (map, increment) // [3, 5, 7, 9, 11] |> (find, 6) // .None |> isEven // .None
  15. Result<T>*Pipeline! // uses https://github.com/antitypical/Result func escapeInput(string: String) -> String { ... } func readFile(fileName: String) -> Result<String> { ... } func processText(string: String) -> String { ... } let processedText = inputFileName |> escapeInput |> readFile |> processText
  16. Experimental,Forward,Pipes |>> // apply as second argument |>>> // apply as third argument |< // apply as last argument
  17. Benefits • pipelines(are(about(composi'on,(encouraging(small,(pure,( reusable(func3ons • fewer("temporary"(variables(intended(for(use(on(next(line • data(pipeline(reads(like(a(nice(list • code(for(handling(values,(Optional<T>(and(Result<T>(is(the( same:(avoid(if-lets(and(switches(and(without(knowledge(of( monad(kung>fu
  18. Non$Muta)ng+Helpers Pure%versions%of%everything%in: • ExtensibleCollectionType • RangeReplaceableCollectionType • MutableCollectionType • Mutable)dic-onary)stuff
  19. Currying // curry the first argument at the end func map<S: SequenceType, T>(f: S.Generator.Element->T)(seq: S) -> [T] {...} 1...5 |> filter(isEven) // [2, 4] |> map({$0 * 3}) // [6, 12] |> reduce(0, +) // 18
  20. Non$Muta)ng+Helpers Pipes&contains&curried&versions&of: • map,#filter,#reduce • ExtensibleCollectionType • RangeReplaceableCollectionType • MutableCollectionType • Mutable#dic.onary#stuff
  21. h"p://github.com/jarsen/Pipes Feedback(/(Pull(Requests(much(appreciated @jarsen(on(twi-er
Advertisement