F#: Using a Function’s Parameter as an Interface

Datetime:2016-08-22 22:02:39          Topic: F#           Share

Intro

I have been struggling to adopt a functional mindset while learning F#. Thus, I continue to spin my wheels.

It’s gotten so bad that one member on StackOverflow left the following message :

Scott, you’re sometimes asking questions that have straightforward answers in relevant definitions. It might make your programming more efficient to spend some extra time on learning the exact definitions and functionality of used features, so that you can pin down the source of issues with more confidence. When it’s perfectly clear how functions, parameters, tuples, and pipes work, the oddness of ||> here becomes much more prominent.

The person who left that feedback was right. Unfortunately, I tend to learn from my failures. Thus, I will continue my campaign with massive and consistent action to learn this beast of a language by any means necessary.

Recently, I have been struggling to understand how to leverage functions as interfaces to other functions. Specifically, I struggled to figure out how to invoke a function from a parameter of another function whose value is a function.

OMG! Just making that statement makes my head hurt.

Implementing an Interface as a Function

I have identified two techniques for exposing an interface through a function. The first technique is to specify the function signature for the parameter of the function that’s to serve as an interface. The other technique is embarrassingly simple. It’s just providing a single abstract parameter.

Provide a Function Signature as an Argument

Let’s expose a parameter for a function that will serve as an interface for invoking some function with a specific signature.

Here’s an example:

let executeStrategy1 someFunc:int -> int -> int = 
    someFunc

The above function exposes a parameter as a function signature. Thus, we can leverage this parameter within the function as a function value by invoking the someFunc parameter of the executeStrategy1 function.

Provide an abstract parameter that’s really an implicit function

We can also just provide an arbitrary parameter to the function that will serve as an interface. Specifically, instead of writing a function signature as a parameter, we can just provide a simple parameter that will be inferred as a function value.

Here’s an example:

let executeStrategy2 someFunc =
    someFunc

Holy Crap! That was just stupid simple!

Let’s do a replay:

let executeStrategy2 someFunc =
    someFunc

Oh my lord… Did you see how simple that was?In the code snippet above, we just provided an arbitrary parameter name as the function to be executed and invoked it by just referencing the parameter at the end of our function.

Using an Interface

Here’s an example of how we could leverage an interface to a function:

let add a b = 
    a + b

let result1 = executeStrategy1 add 1 2;;
let result2 = executeStrategy2 add 1 2;;

The result looks like this:

val executeStrategy1 : someFunc:(int -> int -> int) -> (int -> int -> int)
val executeStrategy2 : f:'a -> 'a
val add : a:int -> b:int -> int
val result1 : int = 3

>

val result2 : int = 3

Conclusion

In conclusion, I have been struggling to understand how to leverage functions as interfaces to other functions. Specifically, I struggled to figure out how to invoke a function from a parameter of another function whose value is a function. Thus, I attempted to demonstrate how to expose a function’s parameter as an interface.





About List