Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

F# for Scala developers

1.868 visualizaciones

Publicado el

Introduction to F# oriented to Scala developers

Publicado en: Software
  • Sé el primero en comentar

F# for Scala developers

  1. 1. F# for Scala Developers Walking into the dark side meets and Madrid Meetup groupsScala F#
  2. 2. How much do Scala and F# look alike? Bring (non-strict) Functional Programming to Java and .NET Full compatibility with their host platforms Built-in functional libraries Static safety with type inference Mostly expression based (side-effects also allowed) Open source projects with vibrant communities
  3. 3. How much do Scala and F# differ? Scala Embraces both Object Oriented and Functional Programming Designed not to scare OOP developers: curly-brace Very powerful and flexible syntax Very rich class system Language team works separately from Java team
  4. 4. How much do Scala and F# differ? F# Multi-paradigm but functional-first Inherited from Ocaml: indentation sensitive Less flexible syntax, more focused on consistency Three flavors: project (.fs), script (.fsx) and signature (.fsi) files Language team works together (more or less) with .NET team Functional features like generics and tail-call instructions are native to the platform
  5. 5. Let's see some code examples...
  6. 6. Constants, Variables and null Scala 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
  7. 7. F# 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
  8. 8. Imperative loops Scala 1: 2: 3: 4: 5: 6: 7: 8: 9:
  9. 9. F# As in Scala, break and continue are missing from the language. Recursion or stream functions are preferred. 1: 2: 3: 4: 5: 6: 7:
  10. 10. Functions Scala 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
  11. 11. Scala Memoize Pattern 1: 2: 3: 4: 5: 6: 7: 8: 9: 10:
  12. 12. F# 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
  13. 13. F# As seen above, functions in F# are usually contained in modules (curried by default) Optional and rest parameters are only accepted in non-curried class methods 1: 2: 3: 4: 5: 6: 7: 8: 9: 10:
  14. 14. Classes Scala Classes are very powerful in Scala and different from F#: Singleton objects Traits and abstract types Compound types and mixins
  15. 15. F# F# doesn't focus on classes Their main purpose is compatibility with .NET Base Class Library Mostly same functionality as C# with different Syntax and some additional features (like primary constructors) Interfaces are just abstract classes without default method implementations No mixins, only multiple interface implementation is possible (extension methods are allowed) Object Expressions allow dynamic implementation of interfaces
  16. 16. Abstract classes, interfaces and structs (value classes) 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
  17. 17. 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
  18. 18. Object expressions 1: 2: 3: 4: 5: 6: 7: 8: 9:
  19. 19. Tuples and Records In F#, tuples, records (lightweight classes) and discriminated unions (ADT) are usually preferred, with logic separated in module functions.
  20. 20. Tuples in Scala 1: 2: 3: 4: 5: Tuples in F# 1: 2: 3: 4: 5:
  21. 21. F# Records Named tuples or lightweight classes, if you must 1: 2: 3: 4: 5: 6: 7: 8: 9: We can reach a similar effect in Scala marking constructor parameters as fields 1: 2:
  22. 22. Algebraic Data Types and Pattern Matching Scala 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
  23. 23. F# 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
  24. 24. F# (a more contrived example) 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
  25. 25. Scala Extractor Objects 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
  26. 26. F# Exhaustive Active Patterns 1: 2: 3: 4: 5: F# Partial Active Pattern 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
  27. 27. F# Parameterized Active Patterns Specializing the pattern by passing extra parameters 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
  28. 28. Kitty Break
  29. 29. Generics F# generics are very similar to Scala, with a few diferences: Automatic Generalization If the function has no dependency on the specific type of a parameter, the type is inferred to be generic Statically Resolved Type Parameters Type parameter replaced with actual types at compile time instead of at run time No generics of generics Generics are native to .NET platform (no erasures) but on the other hand are more limited (no type classes)
  30. 30. 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
  31. 31. Instead, _map_ must be implemented for each type (or interface) No generics of generics Type classes like Functor are not allowed 1: 1: 2: 3:
  32. 32. Collections F# built-in functions and operators and focus only on a few collection types: Immutable? Feature Scala list Yes Linked list List seq Yes Lazy evaluation Iterable/Stream array No Random access Array map Yes Indexed access Map set Yes Unique items Set
  33. 33. Fluent APIs Scala 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
  34. 34. F# 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
  35. 35. F# Comprehensions F# allows comprensions similar to those in Haskell or Python 1: 2: 3: 4: 5: In Scala we would just use functions 1:
  36. 36. Observables F# core library also includes support for Functional Reactive Programming 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
  37. 37. More info about F# collections at Scott Wlaschin's site Check also about the performance of F# collection types Phillip Trelford's presentation
  38. 38. Scala Comprehensions and F# Computation Expressions In Scala, any type implementing filterWith, map and flatMap can be used with for comprehensions. This allows, for example, dealing with async operations in a monadic way. 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
  39. 39. In F#, this can be done using computation expressions 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: Computation expressions convert language constructs like let, use, do, for or try in for continuation passing style operations syntactic sugar
  40. 40. F# core has Asynchronous Workflows built-in 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: Async<'T> is lazy, it will only start running after calling Async.Start or Async.RunSynchronously
  41. 41. Custom computation expressions 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: Besides Bind and Return, there are wich can be implemented by custom computations expressions like Zero, Yield, Combine, For, While or Try other methods
  42. 42. Query expressions Query expressions provide support for LINQ in F# 1: 2: 3: 4: 5: 6: They express transformations on a data source wich can be translated to another language, usually SQL 1: 2: 3:
  43. 43. And now for a couple of unique F# features...
  44. 44. Units of Measure 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: Measure annotations disappear after compilation and thus they have no performance penalty (Cannot be retrieved by Reflection though)
  45. 45. Type Providers Static types generated dynamically
  46. 46. JSON
  47. 47. World Bank API REST Watch to know more about type providersthis presentation Type providers can also be emulated with Scala macros
  48. 48. Flagship Projects Scala F# Web , Actors Big Data , Visualization Play Lift Suave Akka Akka.net Spark Mbrace Prajna Zeppelin FsLab More at the F# space for incubating open community projects
  49. 49. Other platforms Scala Scala.js Android F# JS/HTML5: andFunScript WebSharper Mobile platforms with Xamarin
  50. 50. Want more F#? Get started with F# foundation F# for Fun and Profit F# Weekly Ionide These slides were made with and no kittens were harmed in the process FSReveal
  51. 51. And here comes the unasked-for advice! Remember to focus away and when staring at the screen for a long time blink regularly Ping me! @alfonsogcnunez ,Github SlideShare

×