Real-World Functional Programming: With Examples in F# and C#

Author: Tomas Petricek
4.0
All Stack Overflow 7
This Year Stack Overflow 2
This Month Stack Overflow 2

Comments

by anonymous   2018-05-21
As for terms like DiscriminatedUnion... experiment with other languages, particularly functional ones. They tend to guide you towards other techniques. I also highly recommend this book: https://www.amazon.com/Real-World-Functional-Programming-Tomas-Petricek/dp/1933988924
by anonymous   2017-08-20

Since nobody attempted an answer for a day, I'll give it a shot. I understand the problem that you are discussing, but the answer should likely depend on the operating system, language and its runtime library.

The general idea would be that when mouse goes down, you spawn off an asynchronous computation. This computation "blocks" on events, and processes them in its own loop as fetched, doing what you need to do; it exists when the mouse button does up. While this looks like it works like an imperative thread, in reality, when the computation gets "blocked", it just yields control to where it was started from (the main GUI thread invoking the mouse down event, presumably). In such a way, the state is encapsulated into what looks like a lexically local scope of the computation. That magic has to be handled by the runtime of the language, so this is as far as we can get with the answer in a language ans OS-agnostic way.

As for an example of such implementation, check this answer and an article by Tomas Petricek, both discussing your question but in a narrow setting of Windows, .NET and F#. There is a book by the same Perticek, Real World Functional Programming: With Examples in F# and C#, where the same ideas are expressed, surprisingly, in C#, not usually regarded a functional language. Chapter 16, if my memory serves me, is yours.

As for encapsulating the state in an IP/OO language program, you can create an instance of a class in your mouse_down handler. The instance would register itself with the runtime to receive other mouse and UI events, process them, do all the work and, when needed (mouse goes up or the windowing system cancels capture mode), unregisters and destroy itself. Not as elegant as a functional language would allow, but still much better that keeping mutable state in a general UI class.

by anonymous   2017-08-20

Real-World Functional Programming: With Examples in F# and C# [Paperback] Tomas Petricek (Author), Jon Skeet (Author)

Amazon Link

From the Amazon book description:

Real-World Functional Programming is a unique tutorial that explores the functional programming model through the F# and C# languages. The clearly presented ideas and examples teach readers how functional programming differs from other approaches. It explains how ideas look in F#-a functional language-as well as how they can be successfully used to solve programming problems in C#. Readers build on what they know about .NET and learn where a functional approach makes the most sense and how to apply it effectively in those cases.

by anonymous   2017-08-20

There's a pretty good book written on this subject, http://www.amazon.com/Real-World-Functional-Programming-Examples/dp/1933988924.

And in the real world using F# unfortunately isn't an option due to team skills or existing codebases, which is another reason I do love this book, as it has shows many ways to implement F# features in the code you use day to day. And to me at least the vast reduction in state bugs, which take far longer to debug than simple logic errors, is worth the slight reduction in OOP orthodoxy.

For the most part having no static state and operating in a static method only on the parameters given will eliminate side-effects, as you're limiting yourself to pure functions. One point to watch out for though is retrieving data to be acted on or saving data to a database in such a function. Combining OOP and static methods, though, can help here, by having your static methods delegate to lower level objects commands to manipulate state.

Also a great help in enforcing function purity is to keep objects immutable whenever possible. Any object acted on should return a new modified instance, and the original copy discarded.

by anonymous   2017-08-20

Official entry point is here: Microsoft F# Developer Center

For a quick taste, consider trying F# in your browser (Silverlight). (contains interactive tutorial walkthroughs)

Start by watching videos and presentations (BTW, An Introduction to Microsoft F# by Luca Bolognese is still one of the best presentations on the subject). Then read the following two must-read books:

and in addition to that...

Quick Links

  • Visual Studio (F# is standard feature of professional editions of VS2010 and VS2012)
  • Get F# Tools for Visual Studio 2012 Express for Web (free 2012 tools release info)
  • Download the F# November 2010 CTP (free 2010 tools release info)
  • Learn F# via MSDN resources
  • The F# 2.0 Language Specification (PDF) (F# 3.0 spec to appear)
  • MSDN docs for F# (language reference, library reference)
  • F# Programming at Wikibooks (there are lots of samples)
  • F# Samples and Walkthroughs
  • F# 3.0 Sample Pack
  • In-browser tutorials at Try F# (requires Silverlight)
  • The F# Foundation has lots of resources

Community

Q&A

  • F# tags here on StackOverflow
  • F# MSDN forum
  • F# on fpish

Blogging/publishing

  • F# Community Blogs
  • fpound.net aggregator
  • F# on twitter
  • F# on github (fsharpx)
  • F# team blog

Meetup Groups

  • New York City F# User Group
  • Chicago F# Users
  • F# Seattle User Group
  • The San Francisco Bay Area F# User Group
  • F#unctional Londoners Meetup Group
  • The Zurich FSharp Users Meetup Group

Videos

  • F# videos on Channel9
  • F# "How do I…?" videos on MSDN

Other Books

Tools needed

Visual Studio:

  • F# is built in to professional editions of VS2010 and VS2012.
  • F# is available as a add-on component to Visual Studio 2012 Express for Web; get the tools here.
  • Alternatively you can also install the VS2010 integrated shell (free download). Then install the CTP MSI, for a free VS2010 version of F# 2.0.

MonoDevelop:

  • You can find lots of information about using F# within MonoDevelop here. The F# compiler and fsi.exe are now part of the Mono distribution.