'Can someone point me to examples of multiparadigm (object-functional) programming in F#?

Can someone point me to examples of multiparadigm (object-functional) programming in F#?

I am specifically looking for examples which combine OO & functional programming. There's been a lot of talk about how F# is a hybrid language but I've not been able to find examples which demonstrate the example of multiparadigm programming.

Thanks



Solution 1:[1]

I made a small (600 lines) Tetris clone with F# that is Object Oriented using XNA. The code is old (uses #light) and isn't the prettiest code you will ever see but it's defiantly a mix of OOP and functional. It consists of ten classes. I don't think I pass any first class functions around but it's a good example of the F#'s functional power in programming the small.

  • MyGame - Inherits XNA main game class and is the programs entry point.
  • Board - Keeps track of pieces that are no longer moving and horizontal line completes.
  • UI - The UI only has two states (playing and main menu) handled by bool stateMenu
  • Tetris - Handles game state. Game over and piece collision.
  • Piece - Defines the different Tetris shapes and their movement and drawing.
  • Player - Handles user input.
  • Shape - The base graphic object that maps to primative.
  • Primative - Wraps the Vertex primitive type.

I made a rough class diagram to help. If you have any questions about it feel free to ask in the comment section.

alt text

Solution 2:[2]

The most powerful experience I've had mixing OO (specifically mutation) and functional programming is achieving performance gains by using mutable data-structures internally while enjoying all the benefits of immutability by external users. A great example is an implementation I wrote of an algorithm which yields lexicographical permutations you can find here. The algorithm I use is imperative at it's core (repeated mutation steps of an array) which would suffer if implemented with a functional data structure. By taking an input array, making a read-only copy of it initially so the input is not corrupted, and then yielding read-only copies of it in the sequence expression after the mutation steps of the algorithm are performed, we strike a fine balance between OO and functional techniques. The linked answer references the original C++ implementation as well as benchmarks other purely functional implementation answers. The performance of my mixed OO / functional implementation falls in between the superior performance of the OO C++ solution and the pure functional F# solution.

This strategy of using OO / mutable state internally while keeping pure externally to the caller is used throughout the F# library notably with direct use of IEnumerators in the Seq module.

Another example may be found by comparing memoization using a mutable Dictionary implementation vs. an immutable Map implementation, which Don Syme explores here. The immutable Dictionary implementation is faster but no less pure in usage than the Map implementation.

In conclusion, I think using mutable OO in F# is most powerful for library designers seeking performance gains while keeping everything pure functional for library consumers.

Solution 3:[3]

I don't know any F#, but I can show you an example of the exact language mechanics you're looking for in Scala. Ignore it if this isn't helpful.

class Summation {

    def sum(aLow : Int, aHigh : Int) = {
        (aLow to aHigh).foldLeft(0) { (result, number) => result + number }
    }

}

object Sample {

    def main(args : Array[String]) {
        println(new Summation sum(1, 10))
    }

}

I tried to keep it super simple. Notice that we're declaring a class to sum a range, but the implementation is used with a functional style. In this way, we can abstract the paradigm we used to implement a piece of code.

Solution 4:[4]

I don't know about F#, but most softwares written in Scala are object-functional in nature.

Scala compiler is probably the largest and a state of the art example of an object-functional software. Other notable examples include Akka, Lift, SBT, Kestrel etc. (Googling will find you a lot more object-functional Scala examples.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Glorfindel
Solution 3
Solution 4 missingfaktor