Fable: error FABLE: Cannot find replacement for Microsoft.FSharp.Collections.ListModule::Indexed

Created on 25 Jul 2017  路  2Comments  路  Source: fable-compiler/Fable

Description

Please add a mapping for List.Indexed.
I need to filter an item based on the index but F# doesn't have a filteri method.
So I use Indexed but it is not supported.

Repro code

...
  | RemoveLineItem index ->
    let items = 
      List.indexed model.Items 
      |> List.filter (fun (ix, item) -> not(ix = index))
      |> List.map snd

Expected and actual results

I'm guessing the JS should be something like

const myList = ["foo", "bar"];
const indexed = myList.map((s,i) => [i, s]);

Related information

  • Fable version (dotnet fable --version): 1.1.17
  • Operating system: Windows 10

Most helpful comment

If need a workaround for now:

module List = 
  let withIndices xs = 
      let items = xs |> Array.ofList
      let length = items.Length
      [ for i in 0 .. (length - 1) -> (i, items.[i]) ]

[1 .. 3]
|> List.withIndices
|> List.filter (fun (idx, item) -> idx <> 1) // [ (0, 1); (2; 3) ]

All 2 comments

If need a workaround for now:

module List = 
  let withIndices xs = 
      let items = xs |> Array.ofList
      let length = items.Length
      [ for i in 0 .. (length - 1) -> (i, items.[i]) ]

[1 .. 3]
|> List.withIndices
|> List.filter (fun (idx, item) -> idx <> 1) // [ (0, 1); (2; 3) ]

Thanks for the report! I also took the chance to clean up some functions within the collection modules. It will be fixed in the next release. For now, please use @Zaid-Ajaj polyfill :+1:

Note also that using many List. or Array. intermediate steps can decrease performance specially with big collections as you have to recreate the whole structure every time. You can use Seq. for the intermediate steps, or in your case, you can easily polyfill the function you need:

module List =
    let filteri f (xs: 't list) =
        let mutable i = -1
        List.filter (fun x -> i <- i + 1; f i x)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

alfonsogarciacaro picture alfonsogarciacaro  路  30Comments

matthid picture matthid  路  29Comments

dbrattli picture dbrattli  路  54Comments

tomcl picture tomcl  路  26Comments

sandeepc24 picture sandeepc24  路  46Comments