Search code examples
f#sequenceconscdr

cons :: operator for sequences in F#?


Is there a better code that does not need to transform the sequence into a list ?

let rec addentry map keys  =
   match keys with 
   | ((i,j) :: tail) ->  Map.add (i,j) ((inputd.[i]).[j]) (addentry map tail)
   | ([]) -> map

addentry Map.empty (Cartesian keys1 keys2 |> Seq.toList)

Solution

  • This is a great place to use Seq.fold. It collapses a collection down into a single value.

    Cartesian keys1 keys2
    |> Seq.fold (fun map (i, j) ->
      let value = (inputd.[i]).[j]
      Map.add (i, j) value map) Map.empty