Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 762
nicolas Avatar asked Mar 01 '12 16:03

nicolas


1 Answers

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
like image 84
JaredPar Avatar answered Sep 25 '22 02:09

JaredPar