SlideShare uma empresa Scribd logo
1 de 21
Baixar para ler offline
Migration vers le patron
« Collection Pipeline »
en Objective-C
Mathieu Godart — @MonsieurDart
Jeudi 10 septembre 2015 @ CocoaHeads
L’INSPIRATION
• Martin Fowler
• Refactoring with Loops and Collection Pipelines



http://martinfowler.com/articles/refactoring-pipelines.html
• Many use cases in the article
• Here, only one case, translated into ObjC
L’IDÉE SOUS-JACENTE
• Traitement des collections d’objets :
• La boucle est la méthode la plus basique
• Mais ce n’est pas la seule : approche en « pipeline »
[Liste initiale] — fonction —> [Liste filtrée]
• Pas seulement pour la programmation fonctionnelle…
L’IDÉE SOUS-JACENTE
• ObjC (et Swift) possèdent ce qu’il faut pour ça :
• Lambdas : pour nous les blocs
• Bibliothèques : pour nous YOLOKit ou BlocksKit
BRIQUES DE BASE
• Les bibliothèques YOLOKit ou BlocksKit
• Fonctions de filtrage sur collections (NSArray, NSSet…)
• Autres fonctions de manipulation avec des blocs
• Chaînage des appels généralisé
CODE DE DÉPART
- (NSArray *)twitterHandles:(NSArray *)authors

forCompany:(NSString *)company

{
NSMutableArray *result = [NSMutableArray new];
for (MGAuthor *a in authors) {
if ([a.company isEqual:company]) {
var handle = a.twitterHandle;
if (handle != null)

[result addObject:handle];
}
}
return [NSArray arrayWithArray:result];
}
CODE DE DÉPART
- (NSArray *)twitterHandles:(NSArray *)authors

forCompany:(NSString *)company

{
NSMutableArray *result = [NSMutableArray new];
for (MGAuthor *a in authors) {
if ([a.company isEqual:company]) {
var handle = a.twitterHandle;
if (handle != null)

[result addObject:handle];
}
}
return [NSArray arrayWithArray:result];
}
VARIABLE TEMPORAIRE
- (NSArray *)twitterHandles:(NSArray *)authors

forCompany:(NSString *)company

{
NSMutableArray *result = [NSMutableArray new];
NSArray *loopStart = authors;
for (MGAuthor *a in loopStart) {
if ([a.company isEqual:company]) {
var handle = a.twitterHandle;
if (handle != null)

[result addObject:handle];
}
}
return [NSArray arrayWithArray:result];
}
PREMIER FILTRAGE
- (NSArray *)twitterHandles:(NSArray *)authors

forCompany:(NSString *)company

{
NSMutableArray *result = [NSMutableArray new];
NSArray *loopStart = authors
.select(^(MGAuthor *a){ return [a.company isEqual:company];});
for (MGAuthor *a in loopStart) {
if ([a.company isEqualToString:company]) {
var handle = a.twitterHandle;
if (handle != null)

[result addObject:handle];
}
}
return [NSArray arrayWithArray:result];
}
CE DONT ON A BESOIN
- (NSArray *)twitterHandles:(NSArray *)authors

forCompany:(NSString *)company

{
NSMutableArray *result = [NSMutableArray new];
NSArray *loopStart = authors
.select(^(MGAuthor *a){ return [a.company isEqual:company]; })
.map(^(MGAuthor *a){ return a.twitterHandle; });
for (NSString *handle in loopStart) {
var handle = a.twitterHandle;
if (handle != null)

[result addObject:handle];
}
return [NSArray arrayWithArray:result];
}
CODE DE DÉPART
- (NSArray *)twitterHandles:(NSArray *)authors

forCompany:(NSString *)company

{
NSMutableArray *result = [NSMutableArray new];
NSArray *loopStart = authors
.select(^(MGAuthor *a){ return [a.company isEqual:company]; })
.map(^(MGAuthor *a){ return a.twitterHandle; })
.select(^(NSString *h){ return h != null; });
for (NSString *handle in loopStart) {
if (handle != null)

[result addObject:handle];
}
return [NSArray arrayWithArray:result];
}
NETTOYAGE FINAL
- (NSArray *)twitterHandles:(NSArray *)authors

forCompany:(NSString *)company

{
NSMutableArray *result = [NSMutableArray new];
return authors
.select(^(MGAuthor *a){ return [a.company isEqual:company]; })
.map(^(MGAuthor *a){ return a.twitterHandle; })
.select(^(NSString *h){ return h != null; });
for (NSString *handle in loopStart) {

[result addObject:handle];
}
return [NSArray arrayWithArray:result];
}
TA DA !
- (NSArray *)twitterHandles:(NSArray *)authors

forCompany:(NSString *)company

{
return authors
.select(^(MGAuthor *a){ return [a.company isEqual:company]; })
.map (^(MGAuthor *a){ return a.twitterHandle; })
.select(^(NSString *h){ return h != null; });
}
TA DA !
- (NSArray *)twitterHandles:(NSArray *)authors

forCompany:(NSString *)company

{
return authors
.select(^(MGAuthor *a){ return [a.company isEqual:company]; })
.map (^(MGAuthor *a){ return a.twitterHandle; })
.select(^(NSString *h){ return h != null; });
}
- (NSArray *)twitterHandles:(NSArray *)authors

forCompany:(NSString *)company

{
return authors
.select(^(MGAuthor *a){ return [a.company isEqual:company]; })
.map (^(MGAuthor *a){ return a.twitterHandle; })
.select(^(NSString *h){ return h != null; });
}
- (NSArray *)twitterHandles:(NSArray *)authors

forCompany:(NSString *)company

{
NSMutableArray *result = [NSMutableArray new];
for (MGAuthor *a in authors) {
if ([a.company isEqual:company]) {
var handle = a.twitterHandle;
if (handle != null) [result addObject:handle];
}
}
return [NSArray arrayWithArray:result];
}
ENCORE PLUS CONCIS EN SWIFT
-(阵列*)的⼿手:(阵列*)作者公司:(字符串*)公司 {
返回作家
。选择(^(作者* A){返回[公司是平等:公司];})
。地图(^(作者* A){返回⼀一个句柄;})
。选择(^(字符串* H){返回H =空;});
}
AUTRE FONCTIONS UTILES
• select, map, slice
• groupBy
• first, any, sort
• flatMap
• …
DU POUR ET DU CONTRE…
• Philosophie ou manière de penser différente
• Lourde barrière à l’entrée, mais peut s’avérer plus claire :
• À l’écriture, à la relecture et à la maintenance
• Favorise les variables immuables (immutable)
• Vous ouvre au fonctionnel… et au map-reduce
DES QUESTIONS ?
DES REMARQUES ?
DES AVIS ?
MERCI LES AMIS…
Mathieu Godart — @MonsieurDart
Jeudi 10 septembre 2015 @ CocoaHeads
Collection pipeline par  Mathieu Godart

Mais conteúdo relacionado

Mais procurados

Java лаб13
Java лаб13Java лаб13
Java лаб13
Enkhee99
 
Linklist through struct
Linklist through structLinklist through struct
Linklist through struct
IBRAR GUL
 
C# Starter L01-Intro and Warm-up
C# Starter L01-Intro and Warm-upC# Starter L01-Intro and Warm-up
C# Starter L01-Intro and Warm-up
Mohammad Shaker
 

Mais procurados (20)

Rubyslava2102
Rubyslava2102Rubyslava2102
Rubyslava2102
 
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
 
Sockets java
Sockets javaSockets java
Sockets java
 
Modern Mobile Web Apps
Modern Mobile Web AppsModern Mobile Web Apps
Modern Mobile Web Apps
 
modern javascript, unobtrusive javascript, jquery
modern javascript, unobtrusive javascript, jquerymodern javascript, unobtrusive javascript, jquery
modern javascript, unobtrusive javascript, jquery
 
Angular 2 не так уж и плох... А если задуматься, то и просто хорош / Алексей ...
Angular 2 не так уж и плох... А если задуматься, то и просто хорош / Алексей ...Angular 2 не так уж и плох... А если задуматься, то и просто хорош / Алексей ...
Angular 2 не так уж и плох... А если задуматься, то и просто хорош / Алексей ...
 
Kruskal algorithm
Kruskal algorithmKruskal algorithm
Kruskal algorithm
 
RxSwift 예제로 감잡기
RxSwift 예제로 감잡기RxSwift 예제로 감잡기
RxSwift 예제로 감잡기
 
Java лаб13
Java лаб13Java лаб13
Java лаб13
 
詳説WebAssembly
詳説WebAssembly詳説WebAssembly
詳説WebAssembly
 
Java Script - Object-Oriented Programming
Java Script - Object-Oriented ProgrammingJava Script - Object-Oriented Programming
Java Script - Object-Oriented Programming
 
Monads
MonadsMonads
Monads
 
Linklist through struct
Linklist through structLinklist through struct
Linklist through struct
 
C/C++とWebAssemblyを利用したライブラリ開発
C/C++とWebAssemblyを利用したライブラリ開発C/C++とWebAssemblyを利用したライブラリ開発
C/C++とWebAssemblyを利用したライブラリ開発
 
C# Starter L01-Intro and Warm-up
C# Starter L01-Intro and Warm-upC# Starter L01-Intro and Warm-up
C# Starter L01-Intro and Warm-up
 
HTML5を使ったウェブアプリケーションの高速化
HTML5を使ったウェブアプリケーションの高速化HTML5を使ったウェブアプリケーションの高速化
HTML5を使ったウェブアプリケーションの高速化
 
Dart != JavaScript
Dart != JavaScriptDart != JavaScript
Dart != JavaScript
 
Dart und JavaScript
Dart und JavaScriptDart und JavaScript
Dart und JavaScript
 
JS programowanie obiektowe
JS  programowanie obiektoweJS  programowanie obiektowe
JS programowanie obiektowe
 
Menguak Misteri Module Bundler
Menguak Misteri Module BundlerMenguak Misteri Module Bundler
Menguak Misteri Module Bundler
 

Destaque

The Impact of Code Review Coverage and Participation on Software Quality
The Impact of Code Review Coverage and Participation on Software QualityThe Impact of Code Review Coverage and Participation on Software Quality
The Impact of Code Review Coverage and Participation on Software Quality
Shane McIntosh
 
Code Coverage Revised : EclEmma on JaCoCo
Code Coverage Revised : EclEmma on JaCoCoCode Coverage Revised : EclEmma on JaCoCo
Code Coverage Revised : EclEmma on JaCoCo
Evgeny Mandrikov
 
Code Coverage in Theory and in practice form the DO178B perspective
Code Coverage in Theory and in practice form the DO178B perspective   Code Coverage in Theory and in practice form the DO178B perspective
Code Coverage in Theory and in practice form the DO178B perspective
Engineering Software Lab
 

Destaque (16)

Couverture de code
Couverture de codeCouverture de code
Couverture de code
 
Code Coverage Web Application
Code Coverage Web ApplicationCode Coverage Web Application
Code Coverage Web Application
 
Java code coverage with JCov. Implementation details and use cases.
Java code coverage with JCov. Implementation details and use cases.Java code coverage with JCov. Implementation details and use cases.
Java code coverage with JCov. Implementation details and use cases.
 
Fight your technical debt with Jenkins, Jacoco and Sonar
Fight your technical debt with Jenkins, Jacoco and SonarFight your technical debt with Jenkins, Jacoco and Sonar
Fight your technical debt with Jenkins, Jacoco and Sonar
 
The Impact of Code Review Coverage and Participation on Software Quality
The Impact of Code Review Coverage and Participation on Software QualityThe Impact of Code Review Coverage and Participation on Software Quality
The Impact of Code Review Coverage and Participation on Software Quality
 
The Nightmare Fuzzing Suite and Blind Code Coverage Fuzzer
The Nightmare Fuzzing Suite and Blind Code Coverage FuzzerThe Nightmare Fuzzing Suite and Blind Code Coverage Fuzzer
The Nightmare Fuzzing Suite and Blind Code Coverage Fuzzer
 
Code Coverage Revised : EclEmma on JaCoCo
Code Coverage Revised : EclEmma on JaCoCoCode Coverage Revised : EclEmma on JaCoCo
Code Coverage Revised : EclEmma on JaCoCo
 
Code Coverage in Theory and in practice form the DO178B perspective
Code Coverage in Theory and in practice form the DO178B perspective   Code Coverage in Theory and in practice form the DO178B perspective
Code Coverage in Theory and in practice form the DO178B perspective
 
Code coverage & tools
Code coverage & toolsCode coverage & tools
Code coverage & tools
 
Code coverage analysis in testing
Code coverage analysis in testingCode coverage analysis in testing
Code coverage analysis in testing
 
Code coverage
Code coverageCode coverage
Code coverage
 
Code Coverage
Code CoverageCode Coverage
Code Coverage
 
Code coverage
Code coverageCode coverage
Code coverage
 
Top 5 Code Coverage Tools in DevOps
Top 5 Code Coverage Tools in DevOpsTop 5 Code Coverage Tools in DevOps
Top 5 Code Coverage Tools in DevOps
 
Code coverage for automation scripts
Code coverage for automation scriptsCode coverage for automation scripts
Code coverage for automation scripts
 
Ja coco ignite
Ja coco igniteJa coco ignite
Ja coco ignite
 

Mais de CocoaHeads France

Mais de CocoaHeads France (20)

Mutation testing for a safer Future
Mutation testing for a safer FutureMutation testing for a safer Future
Mutation testing for a safer Future
 
iOS App Group for Debugging
iOS App Group for DebuggingiOS App Group for Debugging
iOS App Group for Debugging
 
Asynchronous swift
Asynchronous swiftAsynchronous swift
Asynchronous swift
 
Visual accessibility in iOS11
Visual accessibility in iOS11Visual accessibility in iOS11
Visual accessibility in iOS11
 
My script - One year of CocoaHeads
My script - One year of CocoaHeadsMy script - One year of CocoaHeads
My script - One year of CocoaHeads
 
Ui testing dealing with push notifications
Ui testing dealing with push notificationsUi testing dealing with push notifications
Ui testing dealing with push notifications
 
CONTINUOUS DELIVERY WITH FASTLANE
CONTINUOUS DELIVERY WITH FASTLANECONTINUOUS DELIVERY WITH FASTLANE
CONTINUOUS DELIVERY WITH FASTLANE
 
L'intégration continue avec Bitrise
L'intégration continue avec BitriseL'intégration continue avec Bitrise
L'intégration continue avec Bitrise
 
Super combinators
Super combinatorsSuper combinators
Super combinators
 
Design like a developer
Design like a developerDesign like a developer
Design like a developer
 
Handle the error
Handle the errorHandle the error
Handle the error
 
Quoi de neuf dans iOS 10.3
Quoi de neuf dans iOS 10.3Quoi de neuf dans iOS 10.3
Quoi de neuf dans iOS 10.3
 
IoT Best practices
 IoT Best practices IoT Best practices
IoT Best practices
 
SwiftyGPIO
SwiftyGPIOSwiftyGPIO
SwiftyGPIO
 
Présentation de HomeKit
Présentation de HomeKitPrésentation de HomeKit
Présentation de HomeKit
 
Programme MFI retour d'expérience
Programme MFI retour d'expérienceProgramme MFI retour d'expérience
Programme MFI retour d'expérience
 
How to communicate with Smart things?
How to communicate with Smart things?How to communicate with Smart things?
How to communicate with Smart things?
 
Build a lego app with CocoaPods
Build a lego app with CocoaPodsBuild a lego app with CocoaPods
Build a lego app with CocoaPods
 
Let's migrate to Swift 3.0
Let's migrate to Swift 3.0Let's migrate to Swift 3.0
Let's migrate to Swift 3.0
 
Project Entourage
Project EntourageProject Entourage
Project Entourage
 

Collection pipeline par Mathieu Godart

  • 1. Migration vers le patron « Collection Pipeline » en Objective-C Mathieu Godart — @MonsieurDart Jeudi 10 septembre 2015 @ CocoaHeads
  • 2. L’INSPIRATION • Martin Fowler • Refactoring with Loops and Collection Pipelines
 
 http://martinfowler.com/articles/refactoring-pipelines.html • Many use cases in the article • Here, only one case, translated into ObjC
  • 3. L’IDÉE SOUS-JACENTE • Traitement des collections d’objets : • La boucle est la méthode la plus basique • Mais ce n’est pas la seule : approche en « pipeline » [Liste initiale] — fonction —> [Liste filtrée] • Pas seulement pour la programmation fonctionnelle…
  • 4. L’IDÉE SOUS-JACENTE • ObjC (et Swift) possèdent ce qu’il faut pour ça : • Lambdas : pour nous les blocs • Bibliothèques : pour nous YOLOKit ou BlocksKit
  • 5. BRIQUES DE BASE • Les bibliothèques YOLOKit ou BlocksKit • Fonctions de filtrage sur collections (NSArray, NSSet…) • Autres fonctions de manipulation avec des blocs • Chaînage des appels généralisé
  • 6. CODE DE DÉPART - (NSArray *)twitterHandles:(NSArray *)authors
 forCompany:(NSString *)company
 { NSMutableArray *result = [NSMutableArray new]; for (MGAuthor *a in authors) { if ([a.company isEqual:company]) { var handle = a.twitterHandle; if (handle != null)
 [result addObject:handle]; } } return [NSArray arrayWithArray:result]; }
  • 7. CODE DE DÉPART - (NSArray *)twitterHandles:(NSArray *)authors
 forCompany:(NSString *)company
 { NSMutableArray *result = [NSMutableArray new]; for (MGAuthor *a in authors) { if ([a.company isEqual:company]) { var handle = a.twitterHandle; if (handle != null)
 [result addObject:handle]; } } return [NSArray arrayWithArray:result]; }
  • 8. VARIABLE TEMPORAIRE - (NSArray *)twitterHandles:(NSArray *)authors
 forCompany:(NSString *)company
 { NSMutableArray *result = [NSMutableArray new]; NSArray *loopStart = authors; for (MGAuthor *a in loopStart) { if ([a.company isEqual:company]) { var handle = a.twitterHandle; if (handle != null)
 [result addObject:handle]; } } return [NSArray arrayWithArray:result]; }
  • 9. PREMIER FILTRAGE - (NSArray *)twitterHandles:(NSArray *)authors
 forCompany:(NSString *)company
 { NSMutableArray *result = [NSMutableArray new]; NSArray *loopStart = authors .select(^(MGAuthor *a){ return [a.company isEqual:company];}); for (MGAuthor *a in loopStart) { if ([a.company isEqualToString:company]) { var handle = a.twitterHandle; if (handle != null)
 [result addObject:handle]; } } return [NSArray arrayWithArray:result]; }
  • 10. CE DONT ON A BESOIN - (NSArray *)twitterHandles:(NSArray *)authors
 forCompany:(NSString *)company
 { NSMutableArray *result = [NSMutableArray new]; NSArray *loopStart = authors .select(^(MGAuthor *a){ return [a.company isEqual:company]; }) .map(^(MGAuthor *a){ return a.twitterHandle; }); for (NSString *handle in loopStart) { var handle = a.twitterHandle; if (handle != null)
 [result addObject:handle]; } return [NSArray arrayWithArray:result]; }
  • 11. CODE DE DÉPART - (NSArray *)twitterHandles:(NSArray *)authors
 forCompany:(NSString *)company
 { NSMutableArray *result = [NSMutableArray new]; NSArray *loopStart = authors .select(^(MGAuthor *a){ return [a.company isEqual:company]; }) .map(^(MGAuthor *a){ return a.twitterHandle; }) .select(^(NSString *h){ return h != null; }); for (NSString *handle in loopStart) { if (handle != null)
 [result addObject:handle]; } return [NSArray arrayWithArray:result]; }
  • 12. NETTOYAGE FINAL - (NSArray *)twitterHandles:(NSArray *)authors
 forCompany:(NSString *)company
 { NSMutableArray *result = [NSMutableArray new]; return authors .select(^(MGAuthor *a){ return [a.company isEqual:company]; }) .map(^(MGAuthor *a){ return a.twitterHandle; }) .select(^(NSString *h){ return h != null; }); for (NSString *handle in loopStart) {
 [result addObject:handle]; } return [NSArray arrayWithArray:result]; }
  • 13. TA DA ! - (NSArray *)twitterHandles:(NSArray *)authors
 forCompany:(NSString *)company
 { return authors .select(^(MGAuthor *a){ return [a.company isEqual:company]; }) .map (^(MGAuthor *a){ return a.twitterHandle; }) .select(^(NSString *h){ return h != null; }); }
  • 14. TA DA ! - (NSArray *)twitterHandles:(NSArray *)authors
 forCompany:(NSString *)company
 { return authors .select(^(MGAuthor *a){ return [a.company isEqual:company]; }) .map (^(MGAuthor *a){ return a.twitterHandle; }) .select(^(NSString *h){ return h != null; }); }
  • 15. - (NSArray *)twitterHandles:(NSArray *)authors
 forCompany:(NSString *)company
 { return authors .select(^(MGAuthor *a){ return [a.company isEqual:company]; }) .map (^(MGAuthor *a){ return a.twitterHandle; }) .select(^(NSString *h){ return h != null; }); } - (NSArray *)twitterHandles:(NSArray *)authors
 forCompany:(NSString *)company
 { NSMutableArray *result = [NSMutableArray new]; for (MGAuthor *a in authors) { if ([a.company isEqual:company]) { var handle = a.twitterHandle; if (handle != null) [result addObject:handle]; } } return [NSArray arrayWithArray:result]; }
  • 16. ENCORE PLUS CONCIS EN SWIFT -(阵列*)的⼿手:(阵列*)作者公司:(字符串*)公司 { 返回作家 。选择(^(作者* A){返回[公司是平等:公司];}) 。地图(^(作者* A){返回⼀一个句柄;}) 。选择(^(字符串* H){返回H =空;}); }
  • 17. AUTRE FONCTIONS UTILES • select, map, slice • groupBy • first, any, sort • flatMap • …
  • 18. DU POUR ET DU CONTRE… • Philosophie ou manière de penser différente • Lourde barrière à l’entrée, mais peut s’avérer plus claire : • À l’écriture, à la relecture et à la maintenance • Favorise les variables immuables (immutable) • Vous ouvre au fonctionnel… et au map-reduce
  • 19. DES QUESTIONS ? DES REMARQUES ? DES AVIS ?
  • 20. MERCI LES AMIS… Mathieu Godart — @MonsieurDart Jeudi 10 septembre 2015 @ CocoaHeads