将F#函数传递给IEnumerable.Where vs IEnumerable.All

时间:2014-04-23 22:26:03

标签: .net linq f#

鉴于以下内容:

open System.Linq

let even n = n % 2 = 0

let seqA = seq { 0..2..10 }

这是一个有效的表达式:

seqA.Where(even)

但这不是:

seqA.All(even)

为什么允许将even传递给Where但不允许传递给All

1 个答案:

答案 0 :(得分:1)

虽然可能存在错误,但我认为更好的方法是在F#中处理let even n = n % 2 = 0 let seqA = seq { 0..2..10 } seqA |> Seq.filter even //val it : seq<int> = seq [0; 2; 4; 6; ...] seqA |> Seq.forall even //val it : bool = true 时使用Seq高阶函数而不是Linq

public GameObject[] top10 = new GameObject[10];

[System.Serializable]
public class ScoreEntry
{
    public string name;

    public int score;
}

// Use this for initialization
void Start () {
    if (File.Exists(Application.persistentDataPath + "/hiscores.dat"))
    {
        BinaryFormatter b = new BinaryFormatter();
        var f = File.Open(Application.persistentDataPath + "/hiscores.dat", FileMode.Open);

        List<ScoreEntry> hiScores = (List<ScoreEntry>)b.Deserialize(f);
        f.Close();

        for (int i = 0; i == hiScores.Count; i++)
            top10[i].GetComponent<TextMesh>().text += hiScores[i].name + " - " + hiScores[i].score;
    }
}

// Update is called once per frame
void Update () {

}