F# compose pattern matched function - f#

I have these types:
type ShouldRetry = ShouldRetry of (RetryCount * LastException -> bool * RetryDelay)
and RetryCount = int
and LastException = exn
and RetryDelay = TimeSpan
type RetryPolicy = RetryPolicy of ShouldRetry
Now I want composability of the retries; something like this:
let serverOverloaded = [| exnRetry<TimeoutException>;
exnRetry<ServerBusyException> |]
|> Array.map (fun fn -> fn (TimeSpan.FromSeconds(4.0)))
let badNetwork = [||] // etc
let compose p1 p2 =
// http://fssnip.net/7h
RetryPolicy(ShouldRetry( (fun (c,e) ->
let RetryPolicy(ShouldRetry(fn)) = p1
let RetryPolicy(ShouldRetry(fn')) = p2
let (cont, delay) = fn c,e
if cont then cont, delay
else
let (cont', delay') = fn' c,e
cont', delay') ))
let finalPolicy = serverOverloaded |> Array.scan compose (RetryPolicies.NoRetry())
But I'm getting compiler errors on fn, delay and fn', saying "The value or constructor 'fn' is not defined".

I can see two problems in your compose function.
When decomposing p1 and p2, the pattern needs to be wrapped in parentheses (otherwise, the compiler interprets the code as a definition of RetryPolicy function, instead of pattern matching):
let (RetryPolicy(ShouldRetry(fn))) = p1
let (RetryPolicy(ShouldRetry(fn'))) = p2
When calling fn' a bit later, you need to pass it the arguments in a tuple (otherwise, the compiler thinks that you're calling fn' with just a single argument c and then building a tuple):
let (cont', delay') = fn' (c,e)
I didn't check (or tried to run) the whole example, so I don't know if the rest of the code does what you want.

Related

How to make a lazy computational workflow?

I'm trying to write a computational workflow which would allow a computation which can produce side effects like log or sleep and a return value
A usage example would be something like this
let add x y =
compute {
do! log (sprintf "add: x = %d, y= %d" x y)
do! sleep 1000
let r = x + y
do! log (sprintf "add: result= %d" r)
return r
}
...
let result = run (add 100 1000)
and I would like the side effects to be produced when executeComputation is called.
My attempt is
type Effect =
| Log of string
| Sleep of int
type Computation<'t> = Computation of Lazy<'t * Effect list>
let private bind (u : 'u, effs : Effect list)
(f : 'u -> 'v * Effect list)
: ('v * Effect list) =
let v, newEffs = f u
let allEffects = List.append effs newEffs
v, allEffects
type ComputeBuilder() =
member this.Zero() = lazy ((), [])
member this.Return(x) = x, []
member this.ReturnFrom(Computation f) = f.Force()
member this.Bind(x, f) = bind x f
member this.Delay(funcToDelay) = funcToDelay
member this.Run(funcToRun) = Computation (lazy funcToRun())
let compute = new ComputeBuilder()
let log msg = (), [Log msg]
let sleep ms = (), [Sleep ms]
let run (Computation x) = x.Force()
...but the compiler complains about the let! lines in the following code:
let x =
compute {
let! a = add 10 20
let! b = add 11 2000
return a + b
}
Error FS0001: This expression was expected to have type
'a * Effect list
but here has type
Computation<'b> (FS0001)
Any suggestions?
The main thing that is not right with your definition is that some of the members of the computation builder use your Computation<'T> type and some of the other members use directly a pair of value and list of effects.
To make it type check, you need to be consistent. The following version uses Computation<'T> everywhere - have a look at the type signature of Bind, for example:
let private bind (Computation c) f : Computation<_> =
Computation(Lazy.Create(fun () ->
let u, effs = c.Value
let (Computation(c2)) = f u
let v, newEffs = c2.Value
let allEffects = List.append effs newEffs
v, allEffects))
type ComputeBuilder() =
member this.Zero() = Computation(lazy ((), []))
member this.Return(x) = Computation(lazy (x, []))
member this.ReturnFrom(c) = c
member this.Bind(x, f) = bind x f
member this.Delay(funcToDelay:_ -> Computation<_>) =
Computation(Lazy.Create(fun () ->
let (Computation(r)) = funcToDelay()
r.Value))

How can I interpret property based test code?

How can I interpret property based test code?
I'm struggling to translate the instructions on the following snippet:
let myProperty = Prop.forAll fiveAndThrees <| fun number ->
let actual = transform number
let expected = "jbdhjsdhjdsjhsdglsdjlljh"
expected = actual
Check.QuickThrowOnFailure myProperty
Specifically, I'm struggling with the backwards pipeline operator (i.e. "<|").
Here's the test:
[<Fact>]
let ``FizzBuzz.transform returns FizzBuzz`` () =
let fiveAndThrees = Arb.generate<int> |> Gen.map ((*) (3 * 5))
|> Arb.fromGen
let myProperty = Prop.forAll fiveAndThrees <| fun number ->
let actual = transform number
let expected = "jbdhjsdhjdsjhsdglsdjlljh"
expected = actual
Check.QuickThrowOnFailure myProperty
Can someone please guide me step by step on how this code works?
Could this be rewritten using the forward pipe operator (i.e. "|>")?
This answer only covers why <| is used instead of |>.
Here are 5 examples that work toward making use of <| with a large function. The 6th example is to show how the code looks using |> instead of <|. The point is that with the 6th example using |> you have to look into the code to find the primary function funThatNeedsListAndFunc but with <| the primary function funThatNeedsListAndFunc is obvious. So you typically see <| when the last parameter is a function and you want to pass in the function after the primary function for easier comprehension. That's all; don't read more into than that.
After reading the Mark's blog I also learned that <| is useful to remove ( ) around fun. An example using ( ) is given as example 7.
let funThatNeedsListAndFunc list func =
func list
let func = List.sum
let list = Seq.toList { 0 .. 5}
let result1 = funThatNeedsListAndFunc list func
printfn "result1: %A" result1
let result2 = funThatNeedsListAndFunc list <| func
printfn "result2: %A" result2
let result3 = funThatNeedsListAndFunc list <| List.sum
printfn "result3: %A" result3
let result4 = funThatNeedsListAndFunc list <|
fun (list : 'a list) -> List.sum list
printfn "result4: %A" result4
let result5 = funThatNeedsListAndFunc list <|
fun (list : 'a list) ->
// This will be a long function
let a = 1
let b = 2
let c = a * b
let result = List.sum list
let d = "more useless lines"
let (e,f,g) = ("a", 15, 3.0)
result
printfn "result5: %A" result5
.
let result6 =
fun (list : 'a list) ->
// This will be a long function
let a = 1
let b = 2
let c = a * b
let result = List.sum list
let d = "more useless lines"
let (e,f,g) = ("a", 15, 3.0)
result
|> funThatNeedsListAndFunc list
printfn "result6: %A" result6
.
let result7 =
funThatNeedsListAndFunc list (fun (list : 'a list) ->
// This will be a long function
let a = 1
let b = 2
let c = a * b
let result = List.sum list
let d = "more useless lines"
let (e,f,g) = ("a", 15, 3.0)
result)
printfn "result7: %A" result7

The mutable variable 'x' is used in an invalid way. Mutable variables cannot be captured by closures

I have a couple of books that I am going by, but as I am working on my F# problems, I find some difficulties in syntax here. If anyone thinks I should not be asking these questions here and have another book recommendation on a budget, please let me know.
Here is the code that reproduces the problem I am having with my project
[<EntryPoint>]
let main argv =
let mutable x = 0
let somefuncthattakesfunc v = ignore
let c() =
let y = x
ignore
somefuncthattakesfunc (fun () -> (x <- 1))
Console.ReadKey()
0 // return an integer exit code
I am getting the following compile error
The mutable variable 'x' is used in an invalid way. Mutable variables cannot be captured by closures. Consider eliminating this use of mutation or using a heap-allocated mutable reference cell via 'ref' and '!'.
Any clue ?
As the error explains, you can't close over mutable variables, which you are doing in:
let y = x
and
(fun () -> x = 1)
It suggests you use a ref instead if you need mutation:
let x = ref 0
let somefuncthattakesfunc v = ignore
let c() =
let y = !x
ignore
somefuncthattakesfunc (fun () -> x := 1)
As the error message says, mutable variables cannot be captured by closures, use a reference cell instead:
let main argv =
let x = ref 0
let somefuncthattakesfunc v = ignore
let c() =
let y = !x
ignore
somefuncthattakesfunc (fun () -> x := 1)
Console.ReadKey()
0 // return an integer exit code
Also see this answer.

Wrangling TryWith in Computation expressions

(Having failed to 'grok' FParsec, I followed the advice I read somewhere and started trying to write a little parser myself. Somehow I spotted what looked like a chance to try and monadify it, and now I have N problems...)
This is my 'Result' type (simplified)
type Result<'a> =
| Success of 'a
| Failure of string
Here's the computation expression builder
type ResultBuilder() =
member m.Return a = Success(a)
member m.Bind(r,fn) =
match r with
| Success(a) -> fn a
| Failure(m) -> Failure(m)
In this first example, everything works (compiles) as expected:
module Parser =
let res = ResultBuilder()
let Combine p1 p2 fn =
fun a -> res { let! x = p1 a
let! y = p2 a
return fn(x,y) }
My problem is here: I'd like to be able to catch any failure in the 'combining' function and return a failure, but it says that I should define a 'Zero'.
let Combine2 p1 p2 fn =
fun a -> res { let! x = p1 a
let! y = p2 a
try
return fn(x,y)
with
| ex -> Failure(ex.Message) }
Having no idea what I should return in a Zero, I just threw in member m.Zero() = Failure("hello world"), and it now says I need TryWith.
So:
member m.TryWith(r,fn) =
try
r()
with
| ex -> fn ex
And now it wants Delay, so member m.Delay f = (fun () -> f()).
At which point it says (on the ex -> Failure), This expression should have type 'unit', but has type 'Result<'a>', and I throw up my arms and turn to you guys...
Link for playing: http://dotnetfiddle.net/Ho1sGS
The with block should also return a result from the computation expression. Since you want to return Result.Failure you need to define the member m.ReturnFrom a = a and use it to return the Failure from the with block. In the try block you should also specify that fn returns Success if it doesn't throw.
let Combine2 p1 p2 fn =
fun a -> res { let! x = p1 a
let! y = p2 a
return!
try
Success(fn(x,y))
with
| ex -> Failure(ex.Message)
}
Update:
The original implementation was showing a warning, not an error. The expression in the with block was not used since you returned from the try block, so you could simply add |> ignore. In that case if fn throws then the return value is m.Zero() and the only difference is that you would get "hello world" instead of ex.Message. Illustrated with an example below. Full script here: http://dotnetfiddle.net/mFbeZg
Original implementation with |> ignore to mute the warning:
let Combine3 p1 p2 fn =
fun a -> res { let! x = p1 a
let! y = p2 a
try
return fn(x,y)
with
| ex -> Failure(ex.Message) |> ignore // no warning
}
Run it:
let comb2 a =
let p1' x = Success(x)
let p2' y = Success(y)
let fn' (x,y) = 1/0 // div by zero
let func = Parser.Combine2 p1' p2' fn' a
func()
let comb3 a =
let p1' x = Success(x)
let p2' y = Success(y)
let fn' (x,y) = 1/0 // div by zero
let func = Parser.Combine3 p1' p2' fn' a
func()
let test2 = comb2 1
let test3 = comb3 1
Result:
val test2 : Result<int> = Failure "Attempted to divide by zero."
val test3 : Result<int> = Failure "hello world"
If you want to support try ... with inside a computation builder, you need to add TryWith (as you tried) as well as a few other members including Delay and Run (depending on how you want to implement Delay). In order to be able to return failure, you also need to support return! by adding ReturnFrom:
type ResultBuilder() =
member m.Return a = Success(a)
member m.Bind(r,fn) =
match r with
| Success(a) -> fn a
| Failure(m) -> Failure(m)
member m.TryWith(r,fn) =
try r() with ex -> fn ex
member m.Delay(f) = f
member m.Run(f) = f()
member m.ReturnFrom(r) = r
Now you can do the following:
let Combine2 p1 p2 fn = fun a -> res {
let! x = p1 a
let! y = p2 a
try
return fn(x,y)
with ex ->
return! Failure(ex.Message) }
The trick is that the normal branch uses just return (representing success), but the exception handler uses return! to return an explicitly created result using Failure.
That said, if you are interested in parsers, then you need to use a different type - what you are describing here is more like the option (or Maybe) monad. To implement parser combinators you need a type that represents a parser rather than result of a parser. See for example this article.

how to unwrap union value in list in f#

You know that to unwrap a value of a single union type you have to do this:
type Foo = Foo of int*string
let processFoo foo =
let (Foo (t1,t2)) = foo
printfn "%A %A" t1 t2
but my question is: if there a way to do that for lists ?:
let processFooList (foolist:Foo list ) =
let ??? = foolist // how to get a int*string list
...
thanks.
The best way would be to use a function combined with List.map like so
let processFooList (foolist:Foo list ) = foolist |> List.map (function |Foo(t1,t2)->t1,t2)
There's no predefined active pattern for converting the list from Foo to int * string, but you could combine the Named Pattern §7.2 (deconstruct single case union) with the projection into your own single case Active Pattern §7.2.3.
let asTuple (Foo(t1, t2)) = t1, t2 // extract tuple from single Foo
let (|FooList|) = List.map asTuple // apply to list
Use as function argument:
let processFooList (FooList fooList) = // now you can extract tuples from Foo list
... // fooList is an (int * string) list
Use in let-binding:
let (FooList fooList) =
[ Foo(1, "a"); Foo(2, "b") ]
printfn "%A" fooList // prints [(1, "a"); (2, "b")]
Distilling/summarising/restating/reposting the other two answers, your cited line:
let ??? = foolist // how to get a int*string list
Can become:
let ``???`` = foolist |> List.map (function |Foo(x,y) -> x,y)
If you're writing a transformation, you can do the matching in the params having defined an Active Pattern using either of the following:
let (|FooList|) = List.map <| fun (Foo(t1, t2)) -> t1,t2
let (|FooList|) = List.map <| function |Foo(t1, t2) -> t1,t2
which can then be consumed as follows:
let processFooList (fooList:Foo list ) =
// do something with fooList

Resources