そういう意味でObjective-Cを考えると、文字列の操作はまぁまぁ良い(特にUnicode周りがなかなか優れている、正規化もできるし)のですが、配列・辞書・集合の操作がイマイチで、作るの面倒なら操作するのも面倒。さらには良く欲しくなる以下の操作が欠けています。
map
- 条件式を渡して、もとの集合の各要素に条件式を通した結果を新たな集合として返す。reduce
- 条件式を渡して、要素を前から順番に計算して畳み込み、集合から一つの要素にする。any
- 一つでも要素が条件式を満たすならtrue, すべての要素が満たさないならfalseall
- すべての要素が条件式を満たすならtrue, 一つでも満たさないならfalse
http://docs.go-mono.com/?link=T%3aSystem.Linq.Queryable
http://msdn.microsoft.com/ja-jp/library/system.linq.queryable(v=vs.90).aspx
ということで早速使ってみます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collection.Generics; | |
using System.Linq; | |
public void MapExample() { | |
// System.Linq.Queryable.Selectメソッドがいわゆるmapに相当するメソッドになります | |
List<UnityEngine.Object> allResources = new List<UnityEngine.Object>(Resources.FindObjectsOfTypeAll(typeof(UnityEngine.Object))); | |
IEnumerable<string> allResourceNames = allResources.Select(x => x.name); | |
Debug.Log(string.Join("\n", allResourceNames.ToArray())); | |
} | |
public void ReduceExample() { | |
// System.Linq.Queryable.Aggregateメソッドがいわゆるreduceに相当するメソッドになります | |
List<UnityEngine.Object> allResources = new List<UnityEngine.Object>(Resources.FindObjectsOfTypeAll(typeof(UnityEngine.Object))); | |
string joinedAllResourceName = allResources.Aggregate((x, y) => x.name + "\n" + y.name); | |
Debug.Log(joinedAllResourceName); | |
} | |
public void AnyExample() { | |
// System.Linq.Queryable.Anyを使います | |
List<UnityEngine.Object> allResources = new List<UnityEngine.Object>(Resources.FindObjectsOfTypeAll(typeof(UnityEngine.Object))); | |
bool result = allResources.Any(x => x.name.EndsWith(".png")); | |
Debug.Log(result); | |
} | |
public void AllExample() { | |
// System.Linq.Queryable.Allを使います | |
List<UnityEngine.Object> allResources = new List<UnityEngine.Object>(Resources.FindObjectsOfTypeAll(typeof(UnityEngine.Object))); | |
bool result = allResources.All(x => x.name.EndsWith(".png")); | |
Debug.Log(result); | |
} |
実行速度が高速なのかどうかはわからないのですが、なかなか面白いです。ラムダ式が使えるのもスマートで素敵ですね。うーん、C#好きになってきたかも。