Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use F# in Azure Functions

What is the best way to use F# in Azure Functions at the moment? I want my function to have both input and output bindings (e.g. to queues or event hubs)

What I found so far:

  • Queue triggered FSX - but it has no output bindings and is reported to be slow
  • Compile F# code to exe file and run Function as external executable - but again the output queue binding isn't clear to me, I guess the exe file would have to send the item explicitly

Is there a way to take an F# function with inputs and output and host it as a Azure Function directly? Something similar to C# Run method?

Ideally, inputs and outputs should be strongly typed: an object, record or discriminated union.

like image 303
Mikhail Shilkov Avatar asked Apr 09 '16 08:04

Mikhail Shilkov


People also ask

What is f {} in Python?

“F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code, an f-string is a literal string, prefixed with f , which contains expressions inside braces.

How do you use string F?

To create an f-string, prefix the string with the letter “ f ”. The string itself can be formatted in much the same way that you would with str. format(). F-strings provide a concise and convenient way to embed python expressions inside string literals for formatting.

What is %d and %f in Python?

For example, "print %d" % (3.78) # This would output 3 num1 = 5 num2 = 10 "%d + %d is equal to %d" % (num1, num2, num1 + num2) # This would output # 5 + 10 is equal to 15. The %f formatter is used to input float values, or numbers with values after the decimal place.

How do you use %s in Python?

The %s operator is put where the string is to be specified. The number of values you want to append to a string should be equivalent to the number specified in parentheses after the % operator at the end of the string value.


2 Answers

The templates are only meant to be starting points - you can add additional input/output bindings to them easily in the portal "Integrate" tab. For example, if you add a new Blob output named result and bound to blob path "test-output/%rand-guid%", you can write script like the below that writes a blob:

open System
open System.IO

let inputPath = Environment.GetEnvironmentVariable("input")
let input = File.ReadAllText(inputPath)
let message = sprintf "F# script processed queue message '%s'" input
System.Console.Out.WriteLine(message)

let resultPath = Environment.GetEnvironmentVariable("result")
File.WriteAllText(resultPath, input);

Regarding more strongly typed "first class" support for F#, as I mentioned in the forum post you linked to, we're working on it :) For now, F# is in the bucket with all the other out of proc script types, where the communication mechanism in and out of the binding pipeline is via environment variables as you can see above.

like image 199
mathewc Avatar answered Nov 02 '22 02:11

mathewc


Now F# is native!

Thanks to the excellent work by the F# team including Don Syme and Tomas Petricek, we're happy to announce we finally support F# in a first-class fashion in Azure Functions.

https://blogs.msdn.microsoft.com/appserviceteam/2016/09/01/azure-functions-0-5-release-august-portal-update/

like image 21
Johan Karlsson Avatar answered Nov 02 '22 01:11

Johan Karlsson