English 中文(简体)
使用阵列数组的 Array.map
原标题:Using Array.map on an array of arrays
  • 时间:2012-05-24 04:30:02
  •  标签:
  • arrays
  • f#

我有一组阵列P,它代表一个矩阵,作为一个行矢量的阵列(对于我的目的来说,表示更方便),我想提取该阵列的柱形矢量j。我的第一个通行证是:

let column (M: float[][]) (j: int) =
   Array.map(fun v -> v.[j]) M

这无法编译, 告诉我 v. v. [j] 正在使用操作员expr. [idx] 在一个不确定类型对象上使用操作员expr. [idx] 。 这让我困惑不解, 因为盘旋于 v 承认 v 是一个浮点, 我认为这是一个行矢量 。

此外,下列代码还起作用:

let column (M: float[][]) (j: int) =
   Array.map(fun v -> v) M
   |> Array.map (fun v -> v.[j])

我无法理解第二个例子与第一个例子有什么不同。第二个例子的第一幅地图看起来多余:我为自己绘制了一个阵列,然而这似乎解决了类型确定问题。

任何帮助理解我做错事或看不到的事都会感激不尽!

最佳回答

问题是F#类型推论严格由右至右,以便编译者看到

let column (M: float[][]) (j: int) =
   Array.map(fun v -> v.[j]) 

此点, 它绝对对 < code> 绝对一无所知 < code> v , 所以会丢出一个错误 。 这就是为什么前端管道操作员 < code\\ gt; 如此常见 - 将您的代码重写为

let column (M: float[][]) (j: int) =
   M |> Array.map(fun v -> v.[j]) 

很好,这也是为什么你的第二个例子起作用的原因

问题回答

由于类型检查器从左到右的工作方式, 类型( v ) 并不具体,尽管 M 类型在以后某个时候可用。 因此 :

let column (M: float[][]) (j: int) =
   M |> Array.map (fun v -> v.[j])

let column M (j: int) =
   Array.map (fun (v: float []) -> v.[j]) M

工作。

在第二个例子中, fun v-> v 在任何类型上都是可以的。 因此, 数组元素的类型没有问题。 第二部分是 和 < code > 一样有效, 并展示了为什么我们应该使用管道操作员的另一个点 。





相关问题
WordPress Data Storage Efficiency

I ve been asked to review a WordPress plugin of sorts and try to find ways of making it faster. The premise of this plugin is basically to store a bunch of users and shifts and appointments and ...

Convert a 2D array index into a 1D index

I have two arrays for a chess variant I am coding in java...I have a console version so far which represents the board as a 1D array (size is 32) but I am working on making a GUI for it and I want it ...

Convert an array of integers for use in a SQL "IN" clause

Surely there is a framework method that given an array of integers, strings etc converts them into a list that can be used in a SQL "IN" clause? e.g. int[] values = {1,2,3}; would go to "(1,2,3)"

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

Best practice of big javascript objects

sry for this imprecise topic name. I am querying a dataset a lot of times so using ajax request would end up in tons of http requests. For this reason I decided to use the json encode method to ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....