Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving items from an F# List passed to C#

Tags:

c#

.net

f#

I have a function in C# that is being called in F#, passing its parameters in a Microsoft.FSharp.Collections.List<object>.

How am I able to get the items from the F# List in the C# function?

EDIT

I have found a 'functional' style way to loop through them, and can pass them to a function as below to return C# System.Collection.List:

private static List<object> GetParams(Microsoft.FSharp.Collections.List<object> inparams)
{
    List<object> parameters = new List<object>();
    while (inparams != null)
    {
        parameters.Add(inparams.Head);
        inparams = inparams.Tail;
     }
     return inparams;
 }

EDIT AGAIN

The F# List, as was pointed out below, is Enumerable, so the above function can be replaced with the line;

new List<LiteralType>(parameters);

Is there any way, however, to reference an item in the F# list by index?

like image 345
johnc Avatar asked Mar 19 '09 04:03

johnc


People also ask

How do I get back something I accidentally deleted?

Before you do anything else, check the Recycle Bin (Windows) or Trash (macOS) to see if your files are there. If so, a right-click or a copy-and-paste is enough to restore your files to their former spot, and you can breathe easy again.

How do I find deleted items?

Open the Google Drive app. Swipe from left to right, and select Trash. If you see a file you wish to restore, select the 3-dot menu for that file. Select Restore from the menu.


1 Answers

In general, avoid exposing F#-specific types (like the F# 'list' type) to other languages, because the experience is not all that great (as you can see).

An F# list is an IEnumerable, so you can create e.g. a System.Collections.Generic.List from it that way pretty easily.

There is no efficient indexing, as it's a singly-linked-list and so accessing an arbitrary element is O(n). If you do want that indexing, changing to another data structure is best.

like image 161
Brian Avatar answered Oct 23 '22 06:10

Brian