Normally, with languages like C# or Java, there is quite a bit of overhead in creating a quick and dirty program to do small tasks like bulk renaming files in a director, etc. This is the spot that is filled by scripting languages like Python, Perl and Ruby, but did you know that you can write F# code in a scripting style too?
If you have F# installed, it will register the .fsx extension as F# script files. From within Windows Explorer you can execute it directly by selecting the “Run with F# Interactive” context menu option:
Executing a F# script file this way would send the file to the console version of F# interactive, fsi.exe.
You can also double click on the file to edit it inside Visual Studio, whilst writing F# as a script you have access to a number of compiler directives such as __SOURCE_DIRECTORY__ and __SOURCE_FILE__ which returns the current directory and filename respectively.
As a simple example, here’s a script I put together in 5 minutes that identifies image files in the current directory and sub-directories and renames them to be title cased, e.g. “my_image.jpg” will become “My_Image.jpg”.
Executing this script gives the following output:
Using F# in this fashion has the obvious benefit that you don’t have to learn another language just so that you can hack some scripts together to perform simple tasks for you. Build automation tools is one area this can be very useful. Inspired by the Rake build tool of the Ruby world – which allows you to write your build script in Ruby – we now have a Fake build tool that lets us do the same with F#.
Another benefit of using F# as a scripting language is performance. Scripting languages typically sacrifices execution speed for the ease of modifying and deploying. F# on the other hand, is not interpreted and is always compiled first so it can be faster than pure scripting languages.
I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.
Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.
- Lambda optimization tip – enable HTTP keep-alive
- You are thinking about serverless costs all wrong
- Many faced threats to Serverless security
- We can do better than percentile latencies
- I’m afraid you’re thinking about AWS Lambda cold starts all wrong
- Yubl’s road to Serverless
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- AWS Lambda – compare coldstart time with different languages, memory and code sizes
- Guys, we’re doing pagination wrong