Functional-style
control flow in F#
Lincoln Atkinson
7/17/2013
Agenda
• Motivation/goals
• Idiomatic control-flow mechanisms in F#
• Pattern matching/Active patterns
• Loops via recursi...
Motivation
• Most of us learn imperative-style control flow first
• F# allows for a fair bit of imperative style, but this...
FSM
FSM
(F# Spaghetti Monster)
Goals
• Help you have a better understanding of how to use common
functional-style tools via F#, and how they compare to i...
Pattern Matching
• Powerful mechanism to partition data into distinct cases
• Encompasses capabilities of “switch” + “if/e...
(examples)
Active Patterns
• Allow for custom partitioning of data, beyond native pattern
matching capabilities
• Enhance readability...
(examples)
Recursive loops
• F# supports basic imperative loops
• „for‟ with constant iterators
• „foreach‟ over collections
• „while...
Recursive loops - cont
• Advantages of recursive loops
• More general approach – all loop types implemented essentially th...
Recursive loops - cont
• General approach
• Identify the stopping condition(s) of the loop, and what data affect
it(them)....
(examples)
Higher-order functions
• Any function that either
• Accepts another function as an argument, or
• Returns a function as ou...
Higher-order functions - cont
• Key higher-order functions to know for collection processing
• map – transform a collectio...
(examples)
Takeaways
• Pattern matching can greatly streamline code, reduce spaghetti
nested if-blocks, and simplify branching
• Acti...
Questions?
Upcoming SlideShare
Loading in …5
×

Functional-style control flow in F#

747 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
747
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Functional-style control flow in F#

  1. 1. Functional-style control flow in F# Lincoln Atkinson 7/17/2013
  2. 2. Agenda • Motivation/goals • Idiomatic control-flow mechanisms in F# • Pattern matching/Active patterns • Loops via recursion • Higher-order functions for collection processing • Examples
  3. 3. Motivation • Most of us learn imperative-style control flow first • F# allows for a fair bit of imperative style, but this is not always the best approach • “Translate your C# projects to F#” is advice often given to those looking to learn F# • Not bad advice, but naïve translations might leave a bad impression • These patterns usually require mutable state, extra local variables, nested scopes…
  4. 4. FSM
  5. 5. FSM (F# Spaghetti Monster)
  6. 6. Goals • Help you have a better understanding of how to use common functional-style tools via F#, and how they compare to imperative- style tools • Show you at least 1 thing about F# you hadn‟t seen before, didn‟t know before, or hadn‟t thought about before Non-Goals • Teach you intro F# • Convince you that functional-style is the only cool thing to do, or that imperative code is “bad”
  7. 7. Pattern Matching • Powerful mechanism to partition data into distinct cases • Encompasses capabilities of “switch” + “if/else” + more, all in one language construct • Can pattern match against: • Constant literal values • Data structure (for common F# types) • Types • Arbitrary tests • Combinations of above • Matching and capturing are done in a single step
  8. 8. (examples)
  9. 9. Active Patterns • Allow for custom partitioning of data, beyond native pattern matching capabilities • Enhance readability by keeping partitioning implementation details separate from usage • Perfect for when the logic to determine cases is complex enough that ideas become obscured by the code. “This looks complex, but it‟s really just a few fundamental cases.”
  10. 10. (examples)
  11. 11. Recursive loops • F# supports basic imperative loops • „for‟ with constant iterators • „foreach‟ over collections • „while‟ • Limitations • Loop bodies must return „unit‟, so mutation required in order to obtain computed data • No „break‟ statement (yet)
  12. 12. Recursive loops - cont • Advantages of recursive loops • More general approach – all loop types implemented essentially the same via recursion • No mutation required • Arbitrary short-circuiting (i.e. „break) is simple to implement • Can return any data you want • Disadvantages of recursive loops • Harder to grok at first • Slower
  13. 13. Recursive loops - cont • General approach • Identify the stopping condition(s) of the loop, and what data affect it(them). • Identify the data that are processed or mutated in each iteration • Define a recursive function which accepts arguments capturing each of the above • In recursive function body • Check stopping conditions and return if needed • Otherwise compute data for next iteration and pass recursively
  14. 14. (examples)
  15. 15. Higher-order functions • Any function that either • Accepts another function as an argument, or • Returns a function as output • Particularly useful for transforming and processing collections • Chaining (pipelining) such functions together is a common strategy
  16. 16. Higher-order functions - cont • Key higher-order functions to know for collection processing • map – transform a collection to a new one by applying a function to each element of the original • fold – transform a collection into a single result by successively applying a function to each element, threading an accumulator through the calculation • filter – transform a collection into a new one by applying a Boolean function to each element of the original and keeping only those which cause the function to return true • iter – apply a given function to each element of a collection, returning unit.
  17. 17. (examples)
  18. 18. Takeaways • Pattern matching can greatly streamline code, reduce spaghetti nested if-blocks, and simplify branching • Active patterns can be used to enhance expressivity, readability, and modularity of complex partitioning logic • Recursion is a powerful, general approach to looping • Higher-order functions provide clean, pre-packaged engines for common actions
  19. 19. Questions?

×