Contents tagged with f#

Synchronously Await an Async Workflow Using Observable.guard

Oct 15 2011No Comments by stanley

The other day, I couldn't figure out how to Synchronously wait for an Async workflow to complete. I was pretty sure I needed to use Observable.guard, I just couldn't wrap my head around how to use it. This morning I was on a mission to figure it out. [fsharp] open System open FSharp.Control let event = new Event<_>() let createObservableByExecutingAsync asyncToExecute = event.Publish |> Observable.guard(fun _ -> let asyncOperation = async { …

Async Workflow, f#, fsharpx Read more

Rate Limiting a Function with Async Workflows

Oct 14 2011No Comments by stanley

So I am trying to deal with an API that says I cannot make more than 4 requests per second. This is a really good use for F# since it has such great support for Asynchronous Workflows. I took a little inspiration from this post by Luca Bolognese. [fsharp] module RateLimiting open System open FSharp.Control type RateLimitedMessage<'a,'b> = {payload: 'a ; response: ('b -> Async<unit>) option} type RateLimitedAgent<'a,'b>( operation: 'a -& …

Async Workflow, f#, fsharpx Read more

Project Euler in F# - Problems 4 - 6

Sep 04 2011No Comments by stanley

Problem 4 Find the largest palindrome that results from the multiplication of two numbers under 1000 I feel like I have a strange obsession with the Seq.unfold function. That function does most of the heavy lifting here. Although the wise Euler pointed out to me that I can avoid duplicate multiplication factors, and keep track of the largest palindrome so far to avoid unnecessary `isPalindrome` checks. [fsharp] let problem4 () = let isPalindrome (value: string) = match value. …

f#, project euler Read more

Project Euler in F# - Problems 1 - 3

Sep 02 2011No Comments by stanley

Problem 1 Find the sum of all the multiples of 3 or 5 below 1000. Brute force is the first thing that comes to mind [fsharp] let problem1 () = {1 .. 999} |> Seq.filter (fun x -> (x % 3 = 0) || (x % 5 = 0)) |> Seq.sum [/fsharp] But the wise Euler shows Math to be a better tactic, as his answer is unquestionably faster. [fsharp] let problem1_correct () = let target = 999 let sumDivisibleBy value = let p = target / value value * (p * (p + 1)) / 2 …

f#, project euler Read more

Project Euler in F#

Sep 02 2011No Comments by stanley

While job hunting this time around, a recruiter pointed me to a website called Project Euler, as a great place to practice. Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems. The problems always seem overly complicated, and somehow ask …

f#, project euler Read more