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 specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.
Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.
Check out my new course, Complete Guide to AWS Step Functions. In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. Including basic concepts, HTTP and event triggers, activities, callbacks, nested workflows, design patterns and best practices.
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