Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fun with F# while building my first OSS F# app...

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for Koen Metsu Koen Metsu
September 23, 2015

Fun with F# while building my first OSS F# application

Avatar for Koen Metsu

Koen Metsu

September 23, 2015
Tweet

More Decks by Koen Metsu

Other Decks in Programming

Transcript

  1. SOME COOL STUFF I LIKED IN F# WHILE BUILDING A

    PET PROJECT IN MY SPARE TIME KOEN METSU
  2. WHAT'S MY PET PROJECT ABOUT? Image archiver Uses ExifTool (perl)

    to extract EXIF|XMP|... data Archives based on date taken Muffin.Pictures.Archiver on GitHub
  3. EASY TO CREATE WRAPPER TYPES Let's say you want a

    wrapper around a string in C# public class DirectoryPath { private readonly string _path; public DirectoryPath(string path) { _path = path; } public string Path { get { return _path; } } public static implicit operator string(DirectoryPath directoryPath) { return directoryPath.Path; } }
  4. EASY TO CREATE WRAPPER TYPES Let's say you want a

    wrapper around a string in F# 1 : 2 : 3 : 4 : 5 : t y p e D i r e c t o r y P a t h = s t r i n g t y p e F i l e P a t h = s t r i n g t y p e T i m e T a k e n = D a t e T i m e O f f s e t
  5. EASY TO CREATE DATA TYPES (your plain old POCO) 1

    : 2 : 3 : 4 : 5 : 6 : 7 : 8 : t y p e M o v e R e q u e s t = { S o u r c e : F i l e P a t h ; D e s t i n a t i o n : F i l e P a t h } t y p e F a i l e d M o v e = { R e q u e s t : M o v e R e q u e s t ; M e s s a g e : s t r i n g } t y p e F i l e = { F u l l P a t h : F i l e P a t h ; N a m e : s t r i n g }
  6. EASY TO EXTEND TYPES 1 : 2 : 3 :

    4 : 5 : 6 : t y p e P i c t u r e = { F i l e : F i l e ; T a k e n O n : T i m e T a k e n } w i t h m e m b e r t h i s . f o r m a t T a k e n O n = s p r i n t f " % i - % 0 2 i " t h i s . T a k e n O n . Y e a r t h i s . T a k e n O n . M o n t h
  7. EASY TO CREATE UNION TYPES (kind of like enums but

    way cooler) 1 : 2 : 3 : 4 : 5 : t y p e F a i l u r e = | B y t e s D i d N o t M a t c h o f M o v e R e q u e s t | C o u l d N o t C o p y F i l e o f F a i l e d M o v e | C o u l d N o t D e l e t e S o u r c e o f F a i l e d M o v e | O h D e a r G o d I t A l l B l e w U p
  8. TYPE SAFETY: PATTERN MATCHING 1 : 2 : 3 :

    4 : 5 : 6 : 7 : 8 : 9 : 1 0 : l e t f o r m a t F a i l u r e f a i l u r e = m a t c h f a i l u r e w i t h | B y t e s D i d N o t M a t c h r e q u e s t - > " R e a s o n : B y t e s d i d n o t m a t c h " | C o u l d N o t C o p y F i l e m o v e - > s p r i n t f " R e a s o n : C o u l d n o t c o p y % A " m o v e | C o u l d N o t D e l e t e S o u r c e m o v e - > " R e a s o n : C o u l d n o t d e l e t e s o u r c e " | O h D e a r G o d I t A l l B l e w U p - > " Y o u h a n d l e i t f r o m h e r e "
  9. TYPE SAFETY: STRING FORMATTING 1 : 2 : 3 :

    4 : 5 : 6 : l e t r e p o r t n u m b e r O f S u c c e s s e s a u t h o r = s p r i n t f " % i s u c c e s s e s , g o % s ! " n u m b e r O f S u c c e s s e s a u t h o r l e t r e p o r t e d = r e p o r t 1 0 0 0 0 0 0 " K o e n " "1000000 successes, go Koen!"
  10. TYPE SAFETY: INFERENCE 1 : 2 : 3 : 4

    : 5 : 6 : 7 : l e t m a p S o m e O r N o n e f t h i n g s = m a t c h t h i n g s w i t h | x : : x s - > t h i n g s | > S e q . m a p f | > S o m e | _ - > N o n e l e t m a p p e d = m a p S o m e O r N o n e ( f u n x - > x + 2 0 ) [ 0 ; 5 ; l e t n o t M a p p e d = m a p S o m e O r N o n e ( f u n x - > x + 2 0 ) [ ] Some (seq [20; 25; 30]) <null>
  11. EASILY PIPELINE OPERATIONS 1 : 2 : 3 : 4

    : 5 : 6 : 7 : m o v e R e q u e s t s | > L i s t . c h o o s e i s S u c c e s s | > m o v e I n P a r a l l e l m o v e | > t e e ( f u n _ - > w a t c h . S t o p ( ) ) | > c r e a t e R e p o r t m o v e R e q u e s t s | > t e e r e p o r t T o C o n s o l e | > r e p o r t T o M a i l I f N e c e s s a r y a r g u m e n t s
  12. COMPOSE OPERATIONS WITH ROP 1 : 2 : 3 :

    4 : l e t m o v e m o v e W i t h F s c o m p a r e F i l e s c l e a n U p = m o v e W i t h F s > = > c o m p a r e F i l e s > = > c l e a n U p source
  13. F# TYPE PROVIDERS 1 : 2 : 3 : 4

    : 5 : 6 : 7 : 8 : 9 : 1 0 : o p e n F S h a r p . D a t a t y p e T a g s = J s o n P r o v i d e r < " e x a m p l e . j s o n " > l e t t a g s = T a g s . L o a d ( " s o m e F i l e . j s o n " ) l e t p i c t u r e = t a g s | > S e q . f i n d ( f u n p i c - > p i c . F i l e N a m e = " b l a " ) l e t a p e r t u r e = p i c t u r e . A p e r t u r e example json
  14. .NET INTEROPERABILITY 1 : 2 : 3 : 4 :

    5 : 6 : 7 : o p e n S y s t e m . D i a g n o s t i c s l e t p r o c e s s S t a r t I n f o = n e w P r o c e s s S t a r t I n f o ( ) p r o c e s s S t a r t I n f o . F i l e N a m e < - " k i l l d e s t r o y . e x e " p r o c e s s S t a r t I n f o . A r g u m e n t s < - s p r i n t f " / r / y / c / b / a / r % i " 1 2 3 4 P r o c e s s . S t a r t ( p r o c e s s S t a r t I n f o )
  15. EASY ASYNC 1 : 2 : 3 : 4 :

    5 : 6 : 7 : 8 : l e t r u n A s y n c m o v e = l e t a s y n c M o v e r e q u e s t = a s y n c { r e t u r n m o v e r e q u e s t } L i s t . m a p a s y n c M o v e > > A s y n c . P a r a l l e l > > A s y n c . R u n S y n c h r o n o u s l y > > L i s t . o f A r r a y
  16. FUNCTIONS AS INTERFACES 1 : 2 : 3 : 4

    : 5 : 6 : 7 : 8 : 9 : l e t c o m p a r e F i l e s c o m p a r e x y = i f c o m p a r e x y t h e n s p r i n t f " W o w ! % s I S % s ! " x y e l s e " A w w w . . . " l e t b a d S t r a t e g y x y = x < > y l e t a r e E q u a l = c o m p a r e F i l e s b a d S t r a t e g y " T i m e " " M o n e y "Wow! Time IS Money!"
  17. EASY STUBS! 1 : 2 : 3 : 4 :

    5 : 6 : 7 : 8 : o p e n S w e n s e n . U n q u o t e l e t ` ` V e r i f y A w w w . . . w h e n t h i n g s d o n ' t m a t c h u p ` ` ( ) l e t s t u b C o m p a r e r x y = f a l s e l e t c o m p a r i s o n = c o m p a r e F i l e s s t u b C o m p a r e r " T i m e " " M o n e y " t e s t < @ " A w w w . . . " = c o m p a r i s o n @ >
  18. PARTIAL APPLICATION 1 : 2 : 3 : 4 :

    5 : 6 : l e t n o w = D a t e T i m e O f f s e t . U t c N o w l e t i s O l d e r T h a n 1 M o n t h = i s O l d e r T h a n X M o n t h s n o w 1 l e t f i r s t O l d D a t e = [ 0 . 0 . . 8 . 0 . . 1 0 0 . 0 ] | > S e q . m a p ( f u n i - > n o w . A d d D a y s ( - i ) ) | > S e q . f i n d i s O l d e r T h a n 1 M o n t h 22/08/2015 13:04:32 +00:00