In the second example, change

1
... fun x y -> ...

to

1
... fun (x:WhateverTypeItIs) y -> ...

Type inference in F# works left-to-right, which is one reason pipeline syntax is preferred. See also

[link:stackoverflow.com]

[link:lorgonblog.wordpress.com]

By on 4/8/2011 1:03 PM ()

For completeness I've attached the code that now works as per Brian's suggestion. Hopefully it may help others in the future (this type inference rules takes some getting used to IMHO).

@Brian (Thx for your help) - I see that ex 1 uses the pipeline operator - however I'm unclear as to how/whether there is a way to recode ex 2 to use the pipeline operator and avoid the explicit type definition that solves the problem. Can you comment?

1
2
3
4
5
6
7
8
9
// Example 2- Iterate over a pair of dictionary sequences 
let diff = Seq.iter2 (fun (x:Dictionary&ltstring,obj&gt) y -> 
            for KeyValue(k,v) in x do
                 printfn "Key: %A; Value: %A; ktype: %A; 
            let k = "hashvalue"
            let v = x.[k]				// now works correctly
            printfn "Key: %A; Value: %A;" k v

        ) Dict1 Dict2
By on 4/8/2011 1:43 PM ()

There's a double-pipieline for passing two args, e.g. instead of

1
Seq.iter2 someFun d1 d2

you do

1
(d1, d2) ||> Seq.iter2 someFun
By on 4/8/2011 2:23 PM ()
1
> (d1, d2) ||> Seq.iter2 someFun

You learn (or remember you forgot...) something new every day!

Sorry - can't get in front of Reflector at the moment...

Does this actually create a tuple and a curried fn, etc. - or is there a syntactic transformation that makes this equivelent to "Seq.iter2 someFun d1 d2" ? If not, seems like this shoud be possible, even in nested cases. Although F# can generate very fast code (faster than c# in some cases) it seems I see more and more code eschew certain constructs like DU's for perf. reasons. I'd love to see cases like pipelineing handled as a syntactic transformation in -o mode -- don't want f# to be a "the value of everything but cost of nothing" language when using its full expressive power. (Same with "match (a,b) with (1,2) .. " - no need to actually construct any tuples unless there passed somewhere, I'd think.)

By on 4/8/2011 8:53 PM ()

Oops - looks like my code got mangled - so line 1 should read - for some reason the Dictionary'<'string,ob'j>' got replaced by Dictionary (without the dict type spec):

1
2
let diff = Seq.iter2 (fun (x:Dictionary'<'string,ob'j>') y -> 
IOW - (fun x y)   becomes fun (x:Dictionary'<'string,obj'>') y 

Sorry for any confusion

By on 4/8/2011 1:51 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper