Use Queue or stick to native f# lists

  softwareengineering

I need something that represents a queue in F#.

The queue will be filled with items once, and (as the application progresses) items will be taken from this queue and moved to other lists.

Should I use the Queue class in .NET, or should I avoid it, and use the native lists in F# instead? Is there a better solution?

Why should I use one, or the other, in a functional language such as F#?

2

If you are creating a class library to be shared with other .NET applications, and this is part of the public interface, then you should stick with normal .NET class library types.

If you are sticking solely to the F# domain, or this is part of the private implementation details, I would prefer to use native F# types.

In your case however, the F# list does not correspond to a Queue, at least not a FIFO queue (first in, first out). The F# list corresponds to a stack (last in, first out).

I’m not aware of a native F# data type providing that functionality, and in fact, the the queue data type does not sit well with the purely functional programming paradigm (I’m not an expert on functional programming so perhaps someone will prove this statement wrong).

So the big question on how to solve the problem is really, is your problem well suited to the functional programming paradigm? If so, you should avoid using imperative data types such as queues, and try to solve the problem using purely functional data types.

If your problem on the other hand is not well suited to a functional programming paradigm, perhaps you should not use F# at all, but use a more object-oriented programming paradigm. Not that F# cannot do object oriented programming, because it can. But if that it is the general programming paradigm, it is IMHO easier to use e.g. C#.

7

@bytebuster made a comment above that got me to thinking.

The .NET’s Queue<T> is mutable, and undoubtedly has a standard OO implementation which has guaranteed O(1) for enqueue and dequeue.

Okasaki’s being the standard best-performance immutable functional version of a Queue and not having the same guarantee, perhaps you could apply this rule to your usage in F#:

If the correct data structure is a Queue for your problem, and performance is important (though if it’s not for you, it may be important for your consumers, so worth thinking about then as well), stick with the .NET mutable Queue.

Also however I agree with the other answer here, if it’s a method signature to be used by other .NET languages, use the common .NET Queue<T>

If performance is irrelevant and it’s not to be used by other .NET languages, I guess it would be safe to say it’s up to you whether or not you want to implement a functional Queue in F# to use or just stick with the .NET implementation.

1

If you use a native queue, you can wrap it inside a MailboxProcessor, so that mutation will be synchronized across threads, without needing locks.

1

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT