第35回の発表資料
5/17に開催された勉強会のSession2の発表資料とプログラムです。
ご参加&アンケートのご記入どうもありがとうございました。
数名から資料欲しいと要望をいただいてたのに遅くなってすみません・・・orz
使ってるPowerPoint(2003)がおかしくなって、手元で正しく表示されないので皆さんがちゃんと見れるのか不安です・・・。
ppt + src :
http://www.nobu.dev-asp.net/clr/h/35/no35_csharp_nobu.zip
ためしにSlideShareにもアップしてみました。
ソースコードの色がおかしいことになってる・・・(´・ω・`)
あと、アニメーション部分がカットされているので、クイズ部分の解答が表示されないというw
前日に脳神経外科で頭部CTを撮るに至るような体調だったため全体的に準備が不十分でした・・・。会場に着いた時点ではデモは何一つ作っておらず、id:naoki0311さんが発表している最中に全速力で書いてました。。。
初めてQRコードで自己紹介してみたのですが、多少は(?)反応があって良かったです。初っ端から滑ったらどうしようかと思った。
しかしながら心の可視化を実現したジクン・メソッドには到底及ばないものでした。恐れ入りました。
時間押しまくってしまって(ごめんなさい...)、一番最後にやろうと思っていたデモができませんでした。全米を泣かし損ねました(今回の配布版には含めていません:-)。
一応ソースもまるごと掲載しておきます。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo35 { /// <summary> /// エラー処理等は思いっきり省いているので、 /// 変なエサ(引数)を与えないでください。 /// </summary> class Program { static void Main( string[] args ) { var d = new Demonstration1(); //var d = new FirstClassObject2(); //var d = new Demonstration2(); //var d = new Demonstration3(); d.Run(); } } #region ラムダ式 public class LambdaExpressions { // ご自由に練習してください :) public void Run() { Func<string> f1 = () => "hello, world"; Console.WriteLine( f1() ); } } #endregion #region コレクション初期化子 public class CollectionInitializers { public void Run() { this.Before(); //this.After(); } ///<summary>テスト従業員クラス</summary> public class Employee { public string Name { get; set; } public int Age { get; set; } } public void Before() { List<Employee> lst = new List<Employee>(); Employee john = new Employee(); john.Name = "john"; john.Age = 58; Employee michael = new Employee(); michael.Name = "taro"; michael.Age = 62; lst.Add( john ); lst.Add( michael ); } public void After() { var lst = new List<Employee>() { new Employee { Name = "john", Age = 22 }, new Employee { Name = "taro", Age = 24 }, }; } ///<summary>クラス変数のDicを初期化する例</summary> public class Foo { Dictionary<string, int> dic = new Dictionary<string, int> { { "one", 1 }, { "two", 2 }, { "three", 3 } }; /* public Foo() { this.dic.Add( "one", 1 ); this.dic.Add( "two", 2 ); this.dic.Add( "three", 3 ); } */ } } #endregion #region 型パラメータによって振る舞いを変える public class TypeSelector<T> { private static Func<string> method = () => "default"; // 静的コンストラクタです static TypeSelector() { TypeSelector<int>.method = () => "いんとです"; TypeSelector<string>.method = () => "すとりんぐです"; } public static void Method() { Console.WriteLine( method() ); } } public class Demonstration1 { public void Run() { TypeSelector<int>.Method(); TypeSelector<string>.Method(); TypeSelector<decimal>.Method(); } public void Run2() { new Foo<string>().Method( 10 ); new Foo<int>().Method( 100 ); new Foo<Foo<string>>().Method( 1000 ); } // おまけ〜カリー化 public void Run3() { var f = new Foo<string>(); Action<int> curry = f.Method; curry( 10 ); } } public class Foo<T>{} public static class SampleExpressions { public static void Method( this Foo<string> f, int x ) { Console.WriteLine( x + "かもしれない" ); } public static void Method( this Foo<int> f, int x ) { Console.WriteLine( x + "じゃないと思う" ); } public static void Method<T>( this Foo<T> f, int x ) { Console.WriteLine( x ); } } #endregion #region 関数はファーストクラスオブジェクト public class FirstClassObject1 { public Func<string> GetFunc() { string data = "hello"; Func<string> fns = null; fns += (() => data += " world"); fns += (() => data = data.ToUpper()); fns += (() => data = "<b>" + data + "</b>"); return fns; } public void Run() { var f = this.GetFunc(); Console.WriteLine( f() ); } } public class FirstClassObject2 { public abstract class Food { } public class ちくわ : Food { } public class ネオちくわ : ちくわ { } public class いも : Food { } public class じゃがいも : いも { } public class みそしる : Food { } private Dictionary<string, Func<Food>> table = new Dictionary<string, Func<Food>> { { "taro", () => new ネオちくわ() }, { "hanako", () => new いも() }, { "mae", () => new みそしる() } }; public Food Create( string name ) { if ( this.table.ContainsKey( name ) ) return this.table[ name ](); throw new ArgumentException(); } public Food Create_Before( string name ) { switch ( name ) { case "taro": return new ネオちくわ(); case "hanako": return new いも(); case "mae": return new みそしる(); default: throw new ArgumentException(); } } public void Run() { Console.WriteLine( this.Create( "mae" ) ); Console.WriteLine( this.Create( "taro" ) ); } } #endregion #region メモ化 public class Demonstration2 { public Func<T, TResult> Memoize<T, TResult>( Func<T, TResult> fn ) { var dic = new Dictionary<T, TResult>(); return x => { if ( !dic.ContainsKey( x ) ) dic.Add( x, fn( x ) ); return dic[ x ]; }; } private int HeavyFunc( int x ) { System.Threading.Thread.Sleep( 1 * 1000 ); return (int)Math.Pow( 2, x ); } public void Run() { var newFunc = this.Memoize<int, int>( this.HeavyFunc ); for ( int times = 0 ; times < 3 ; Console.WriteLine(), times++ ) for ( int i = 0 ; i < 9 ; i++ ) Console.WriteLine( newFunc( i ) ); } } #endregion #region 標準クエリ演算子 public class Demonstration3 { public void Run() { this.Demo_SelectMany(); //this.Demo_ToLookup(); //this.Demo_OfType(); } public class Foo { public List<int> Data = null; } public void Demo_SelectMany() { var lst = new List<Foo> { new Foo { Data = new List<int>{ 1, 2, 3 } }, new Foo { Data = new List<int>{ 4, 5, 6 } }, new Foo { Data = new List<int>{ 7, 8, 9 } } }; var result1 = from f in lst from x in f.Data select x; result1.ToList().ForEach( Console.WriteLine ); var result2 = from x in lst.SelectMany( f => f.Data ) select x; result2.ToList().ForEach( Console.WriteLine ); } public void Demo_ToLookup() { var data = new[] { "hello", "taro", ":-)", "jiro", "foo", "?????" }; var lengthTable = data.ToLookup( x => x.Length ); var length3 = lengthTable[ 3 ]; var length4 = lengthTable[ 4 ]; var length5 = lengthTable[ 5 ]; length3.ToList().ForEach( Console.WriteLine ); Console.WriteLine(); length4.ToList().ForEach( Console.WriteLine ); Console.WriteLine(); length5.ToList().ForEach( Console.WriteLine ); } public abstract class AbstractBar { } public class Bar1 : AbstractBar { public void Method() { Console.WriteLine( "Bar 1 !! " ); } } public class Bar2 : AbstractBar { public void Function() { Console.WriteLine( "Bar 2 !!" ); } } public void Demo_OfType() { var lst = new List<AbstractBar> { new Bar1(), new Bar1(), new Bar2(), new Bar1(), new Bar2() }; var result1 = lst.OfType<Bar1>(); var result2 = lst.OfType<Bar2>(); result1.ToList().ForEach( x => x.Method() ); Console.WriteLine(); result2.ToList().ForEach( x => x.Function() ); } } #endregion }
もし聞きたいこととかありましたら可能な範囲でお答えしますのでどうぞ:)