I have only glanced, but I think it's a lazy evaluation issue. If you have e.g.

seq { ... yield ...

Close() }

Close() will only be called after the entire seq is walked over (e.g. by calling Seq.toArray).

In general this is typically an anti-pattern (returning lazy sequence objects that have more global side-effects when the seq is evaluated). You might prefer to return a list or array so that it is easier to reason about the timing of the effects (closing the connection).

By on 1/19/2010 1:04 PM ()

Hi, Brianmcn:
Thank you for your advice.
But I want to know how to force the lazy sequence, in my case how I can make the closing database connection before the next database operation begins.
As it is not easy to write the code.
If you have some free time, please show me the code.
Thanks,

By on 1/19/2010 11:02 PM ()

consume it. Seq.toList your_seq. But then, why use seq {}. You are doing strictly imperative programming from what I sense and F# is simple not the right language for it.

you can also put your open function inside the seq or don't close it and pass it into your function and just use it. BTW, sqlreader is not the right class to use for this kind of usage. Read about sqldata adapter which can fill a datatable.

Have you considered getting a book or two (be it F# or even VB/C# or some other fundamental .NET things) ?

By on 1/19/2010 11:37 PM ()

Hi, Gary:
Thank you for your advice. I know VB.NET, however, in VB.NET, there is no lazy functions, unless Microsoft will add those functions later on. So I don't need to know it from VB.NET, am I right?
I have read 2 books for F#: "F# Programming" by Chris Smith and "Expert F#" by Don Syme.
But it is difficult to know which functions are lazy in open/close database connections. I know "seq" series seems to be lazy, but how do I know "seq.ToArray" is not lazy?
According to the introduction of Chapter 1 of "Expert F#":
F# differs from many functional languages in that it embraces imperative and object-oriented (OO) programming. (Page 1 of chapter 1 Introduction)
So, even F# designer told you that you can use both imperative and OO programming for F# besides its functional feature, am I right on this?
For the 2 books I have read, only "Expert F#" discussed about database usage with F#, unfortunately, most of the sample codes didn't work in my current environment, as I can understand, since the book was published more than 2 years ago, and latest F# environment was out only 2 months ago; so the code didn't work, it is understandable.
But if you know other better books for F# on database issue, please let me know!

By on 1/20/2010 10:43 AM ()

The reason why I suggested you may need to read some more books are that seq {} is a concept that is not unique to F# or C# but also python or lua and probably Ruby too.

So it really is a conceptual thing that one needs to grasp if you want to work in most of the modern language

I am not saying you MUST NOT do imperative style in F#(in fact the whole computation expression is for that) but if you only use that style as well as mostly working with mutable things, C# is actually a better language. F# is a mult-paradigm language but tilting towards the functional side.

A similar situation in python which initially has many of the 'functional' stuff in the core name space but is now moving out as the rest of the language doesn't play well with them. Well at least the language inventor don't think they fit.

I sincerely think that you would be less frustrated if you start with C#(and the resources you can access are way more). In fact, there are now more functional equivalent construct in C#(say LINQ) which makes it a 'mostly imperative/OO but can be quite functional' language that seems to fit your background as you can draw on your knowledge and explore new things gradually

It took me a lot of time to study the Haskell basic functions before I can get a sense about the differences(and advantage/disadvantage) of list/array/seq/lazylist etc.

By on 1/20/2010 3:57 PM ()

Hi, Gary:
Thank you for your advice. However, since I know VB.NET, I don't want to learn C#, I think those 2 languages have only syntax difference, no big real difference.
I like F#, since many things can be expressed with a few lines, while VB.NET, you may have to write dozens or even hundreds of lines. Yes, something it is better to use VB.NET/C#, but most of others, F# is better.
As there are so few "good" books about F#, so learning it is not easy.
Finally, as I am not a professional programmer, I just use my spare time to do some F# programming, so learning many languages is not a choice for me.

By on 1/20/2010 11:18 PM ()

I know "seq" series seems to be lazy, but how do I know "seq.ToArray" is not lazy?

I think laziness is the exception in F#. So, sequences, lazy( ) variables, and LazyList are lazy evaluated. But, most others are (everything else is?) eager evaluated. When you call ``ToArray'' or ``ToList'', the lazy sequence is evaluated and its result is copied into the newly created array or list.

So, even F# designer told you that you can use both imperative and OO programming for F# besides its functional feature, am I right on this?

Yes. Some things are more awkward in F#. But, you can do OO programming.

By on 1/20/2010 11:30 AM ()
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