You can become a serverless blackbelt. Enrol in my course Learn you some Lambda best practice for great good! and learn best practices for performance, cost, security, resilience, observability and scalability. By the end of this course, you should be able to make informed decisions on which AWS service to use with Lambda and how to build highly scalable, resilient and cost efficient serverless applications.
Find the greatest product of five consecutive digits in the 1000-digit number.
open System.IO let numbers = File.ReadAllLines(@"C:\TEMP\euler8.txt") |> Seq.concat |> Seq.map (fun c -> int32(c.ToString())) let CalcProduct numbers = numbers |> Seq.fold (fun acc n -> acc * n) 1 let maxProduct = numbers |> Seq.windowed(5) |> Seq.map (fun n -> CalcProduct n) |> Seq.max
To get the 1000 digits into the program, I copied and pasted the digits into a text file and saved it to C:\TEMP\euler8.txt.
Here you see an example of how you can use the File.ReadAllLines static method to read the contents of a text into a string array in F#. However, a string array doesn’t really help us here, so I used the Seq.concat function to merge them into a char array because mapping each char into the integer it represents.
Two things to note here:
1) why does Seq.concat return a char array as opposed to a string?
Because a string can be treated as a char in the same way that a Dictionary<T, V> can be treated as an IEnumerable<KeyValuePair<T, V> > and iterated as such when passed into a method which takes a IEnumerable<KeyValuePair<T, V> > as argument.
Looking at the signature of the Seq.concat function, it takes a seq<‘T list> (a sequence of lists of T, i.e. IEnumerable<IEnumerable<T> >) as argument and therefore the string is interpreted as IEnumerable<IEnumerable<char> > and concatenated into IEnumerable<char>.
The same is applied to all the List/Array/Seq functions when used on a string.
2) why is ToString() needed when casting a char to int32?
Because int32(char) will give you the unicode value of that char! For example,
int32('c') // this is legal and returns 99 int32("c") // this is illegal int32('1') // this returns 49 int32("1") // this returns 1
Moving on, the next line defines a helper function which takes a list of numbers and multiply them together, i.e [x; y; z] => x * y * z. I have done this with the Seq.fold function which applies a function to each element of the list whilst keeping an accumulator throughout, similar to the Enumerable.Aggregate method in Linq. In this particular case, the accumulator starts off at 1, and iteratively multiplied by the elements in the list:
let CalcProduct numbers = numbers |> Seq.fold (fun acc n -> acc * n) 1
It’s worth noting that this function will return 1 if used on an empty array which is not the correct behaviour, but for the purpose of solving the problem at hand it can be safely assumed that this will never happen.
Finally, in the last part of the solution, you will notice yet another new function Seq.windowed, which iterates through the elements in the list yielding a sliding windows of 5 elements:
numbers |> Seq.windowed(5);; val it : seq<int32 > = seq [[|7; 3; 1; 6; 7|]; [|3; 1; 6; 7; 1|]; [|1; 6; 7; 1; 7|]; [|6; 7; 1; 7; 6|]; ...]
I then calculate the product for each of the 5 digit arrays and find the greatest product:
|> Seq.map (fun n -> CalcProduct n) |> Seq.max
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 podcast Real-World Serverless where I talk with engineers who are building amazing things with serverless technologies and discuss the real-world use cases and challenges they face. If you’re interested in what people are actually doing with serverless and what it’s really like to be working with serverless day-to-day, then this is the podcast for you.
Check out my new course, Learn you some Lambda best practice for great good! In this course, you will learn best practices for working with AWS Lambda in terms of performance, cost, security, scalability, resilience and observability. We will also cover latest features from re:Invent 2019 such as Provisioned Concurrency and Lambda Destinations. Enrol now and start learning!
Check out my video 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. There is something for everyone from beginners to more advanced users looking for design patterns and best practices. Enrol now and start learning!
Are you working with Serverless and looking for expert training to level-up your skills? Or are you looking for a solid foundation to start from? Look no further, register for my Production-Ready Serverless workshop to learn how to build production-grade Serverless applications!
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 wrong about serverless and vendor lock-in
- You are thinking about serverless costs all wrong
- Just how expensive is the full AWS SDK?
- Many faced threats to Serverless security
- We can do better than percentile latencies
- 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
- Top 10 Serverless framework best practices