SlideShare una empresa de Scribd logo
1 de 10
F# と C# で見る関数志向プロ
        グラミング

      目的
 F#とC#で同じこと
  をやってみよう
変数いろいろ
let n = 1 // 数値リテラル                         var n = 1; // 数値リテラル

let s = "123" // 文字列リテラル                    var s = "123"; // 文字列リテラル

let li = [ 1; 2; 3; 4; 5] // リストリテラル        // リストリテラル?
                                            var li = new List<int> { 1, 2, 3, 4, 5 };
// list comprehension
let li2 =                                   // リスト内包表記...?
  [ for x in 1..20 do                       var li2 =
    if x % 2 = 0 then                          Enum   erable.Range(1, 20)
     yield x ]                                     .W here(x => x % 2 == 0).ToList();

// Sequence = Enum   elable<T>              // もうLinqで
let sq =                                    var sq =
 seq { for x in 1..10 - > x, x*x, x*x*x }      from x in Enumerable.Range(1, 10)
                                               select new { X=x, XX=x*x, XXX=x*x*x };
関数(メソッド)いろいろ
// 普通の                                       // 普通の
let func x = x + 10                          static int Func(int x) { return x + 10; }

// 繰り返し                                      // 繰り返し
let factorial n =                            static int Factorial(int n) {
    let rec loop accum n =                     var result = 1;
        if n < 1 then accum                    for (int i = 1; i <= n; i++)
        else loop (accum * n) (n - 1)             result *= i;
    loop 1 n                                   return result;
                                             }
// 高階関数
let rec fold proc init li =                  // 高階関数
    m atch li w ith                          static TResult Fold<TSource, TResult>(
    | [] - > init                                 Func<TResult, TSource, TResult> proc,
    | x::xs - > fold proc (proc init x) xs        TResult init,
                                                  List<TSource> li) {
                                                       foreach (TSource e in li)
                                                          init = proc(init, e);
                                                       return init;
                                             }
ライブラリの高階関数
// 要素番号と要素を列挙する                                    // 要素番号と要素を列挙する
Seq.iteri (printfn "%d:%d") [1..10]                var index = 0;
                                                   foreach (var x in Enum  erable.Range(1,
// 要素に関数を適用した結果をSeqにして返す                               10).Select( x => new { i=index++, x=x }))
let a = Seq.m (fun x - > x * x) [1..10]
             ap                                      Console.W  riteLine(x.ToString());

// 条件を満たす要素のみのSeqを返す                               // 要素に関数を適用した結果をEnum              rable<T>に
let b = Seq.filter (fun x - > x % 2 = 0) [1..10]       して返す
                                                   var a = from x in Enumerable.Range(1, 10)
// グルーピング                                                select x * x;
let c = Seq.groupBy (fun x - > x % 3) [1..20]
                                                   // 条件を満たす要素のみのSeqを返す
                                                   var b = from x in Enumerable.Range(1, 10)
                                                         where x % 2 == 0
                                                         select x;

                                                   // グルーピング
                                                   var c = Enum erable.Range(1, 20).GroupBy(x
                                                       => x % 3);
関数を返す高階関数
// メモイズ                                     // メモイズ
let m oize func =
      em                                    static Func<T, TResult> M oize<T,
                                                                       em
    let cach = ref M   ap.em pty                 TResult>(Func<T, TResult> func)
    fun n - >                               {
        m atch M  ap.tryFind n !cach with     var cach = new Dictionary<T, TResult>();
        | Som v - > v
                e                             return (T n) => {
        | None - >                                var v = default(TResult);
           let tem = func n
                   p                              if (cach.TryGetValue(n, out v))
           cach := M   ap.add n tem !cach
                                   p                  return v;
           tem  p                                 else
                                                  {
                                                       var tem = func(n);
                                                               p
                                                       cach.Add(n, temp);
                                                       return temp;
                                                  }
                                              };
                                            }
まとめ

F# と C# で同じことはできるの
        か?
        ↓
 できる!がんばればっ!
関数を整理してみる

         model                            type
         action                        unit -> unit
  comparison function              Type -> Type -> int
        callback                      Type -> unit
  delayed computation                 unit -> Type
      transformer                   Type1 -> Type2
          sink                        Type -> unit
visitor accumulating function   Type1 -> Type2 -> Type2
高階関数って結局何ができ
     る?
「処理」と「制御」と「構造」を
  簡単 に分離できる
      ↓

  IoC パターンの促進
みなさんの引き出しの中に
  関数型言語を!!
ご清聴ありがとうございました

Más contenido relacionado

La actualidad más candente

メタプログラミングRubyはこの付録が美味しい
メタプログラミングRubyはこの付録が美味しいメタプログラミングRubyはこの付録が美味しい
メタプログラミングRubyはこの付録が美味しい
Shigeru UCHIYAMA
 
Effective java 勉強会
Effective java 勉強会Effective java 勉強会
Effective java 勉強会
Takinami Kei
 
テンプレートメタプログラミング as 式
テンプレートメタプログラミング as 式テンプレートメタプログラミング as 式
テンプレートメタプログラミング as 式
digitalghost
 
C++ lecture-2
C++ lecture-2C++ lecture-2
C++ lecture-2
sunaemon
 

La actualidad más candente (20)

言語処理系入門4
言語処理系入門4言語処理系入門4
言語処理系入門4
 
Mock and patch
Mock and patchMock and patch
Mock and patch
 
ナウなヤングにバカうけのイカしたタグ付き共用体
ナウなヤングにバカうけのイカしたタグ付き共用体ナウなヤングにバカうけのイカしたタグ付き共用体
ナウなヤングにバカうけのイカしたタグ付き共用体
 
Sml#探検隊
Sml#探検隊Sml#探検隊
Sml#探検隊
 
メタプログラミングRubyはこの付録が美味しい
メタプログラミングRubyはこの付録が美味しいメタプログラミングRubyはこの付録が美味しい
メタプログラミングRubyはこの付録が美味しい
 
みんなで Swift 復習会での談笑用スライド – 4th #minna_de_swift
みんなで Swift 復習会での談笑用スライド – 4th #minna_de_swiftみんなで Swift 復習会での談笑用スライド – 4th #minna_de_swift
みんなで Swift 復習会での談笑用スライド – 4th #minna_de_swift
 
Effective java 勉強会
Effective java 勉強会Effective java 勉強会
Effective java 勉強会
 
Swift 3.0 の新機能 - 追加・変更まわりだけ、ざっくり紹介 2 #devsap
Swift 3.0 の新機能 - 追加・変更まわりだけ、ざっくり紹介 2 #devsapSwift 3.0 の新機能 - 追加・変更まわりだけ、ざっくり紹介 2 #devsap
Swift 3.0 の新機能 - 追加・変更まわりだけ、ざっくり紹介 2 #devsap
 
テンプレートメタプログラミング as 式
テンプレートメタプログラミング as 式テンプレートメタプログラミング as 式
テンプレートメタプログラミング as 式
 
Java8 Lambda chapter5
Java8 Lambda chapter5Java8 Lambda chapter5
Java8 Lambda chapter5
 
C++ lecture-2
C++ lecture-2C++ lecture-2
C++ lecture-2
 
Task
TaskTask
Task
 
大人のお型付け
大人のお型付け大人のお型付け
大人のお型付け
 
Freer Monads, More Extensible Effects
Freer Monads, More Extensible EffectsFreer Monads, More Extensible Effects
Freer Monads, More Extensible Effects
 
Functional Way
Functional WayFunctional Way
Functional Way
 
Apg4b 2.05.再帰 sum関数の動作説明
Apg4b 2.05.再帰 sum関数の動作説明Apg4b 2.05.再帰 sum関数の動作説明
Apg4b 2.05.再帰 sum関数の動作説明
 
おいしいLisp
おいしいLispおいしいLisp
おいしいLisp
 
たのしい高階関数
たのしい高階関数たのしい高階関数
たのしい高階関数
 
マスターオブゴールーチンアンドチャネル スタートGo #1
マスターオブゴールーチンアンドチャネル   スタートGo #1マスターオブゴールーチンアンドチャネル   スタートGo #1
マスターオブゴールーチンアンドチャネル スタートGo #1
 
Google Developer Day 2010 Japan: プログラミング言語 Go (鵜飼 文敏)
Google Developer Day 2010 Japan: プログラミング言語 Go (鵜飼 文敏)Google Developer Day 2010 Japan: プログラミング言語 Go (鵜飼 文敏)
Google Developer Day 2010 Japan: プログラミング言語 Go (鵜飼 文敏)
 

Destacado

MasterCertificateSupleInfor_FS
MasterCertificateSupleInfor_FSMasterCertificateSupleInfor_FS
MasterCertificateSupleInfor_FS
Filipa Saraiva
 
Analisis colombian petroleum industry platts oilgram news july 2 2014
Analisis colombian petroleum industry platts oilgram news july 2 2014Analisis colombian petroleum industry platts oilgram news july 2 2014
Analisis colombian petroleum industry platts oilgram news july 2 2014
Orlando Hernandez
 
Platts oilgram news 20140404
Platts oilgram news 20140404Platts oilgram news 20140404
Platts oilgram news 20140404
Orlando Hernandez
 

Destacado (20)

Bachelor of ICT
Bachelor of ICTBachelor of ICT
Bachelor of ICT
 
Monitoring
MonitoringMonitoring
Monitoring
 
Developing With Django
Developing With DjangoDeveloping With Django
Developing With Django
 
5 ways to embrace HTML5 today
5 ways to embrace HTML5 today5 ways to embrace HTML5 today
5 ways to embrace HTML5 today
 
BGP Hijack Issue on Nov 6 2015
BGP Hijack Issue on Nov 6 2015BGP Hijack Issue on Nov 6 2015
BGP Hijack Issue on Nov 6 2015
 
Platts oilgram news juio 21 2014 Page 9 Colombia’s Cano Limon line bombed in ...
Platts oilgram news juio 21 2014 Page 9 Colombia’s Cano Limon line bombed in ...Platts oilgram news juio 21 2014 Page 9 Colombia’s Cano Limon line bombed in ...
Platts oilgram news juio 21 2014 Page 9 Colombia’s Cano Limon line bombed in ...
 
MasterCertificateSupleInfor_FS
MasterCertificateSupleInfor_FSMasterCertificateSupleInfor_FS
MasterCertificateSupleInfor_FS
 
CSS3: Possibilities, Best Practices and Pitfalls
CSS3: Possibilities, Best Practices and PitfallsCSS3: Possibilities, Best Practices and Pitfalls
CSS3: Possibilities, Best Practices and Pitfalls
 
Analisis colombian petroleum industry platts oilgram news july 2 2014
Analisis colombian petroleum industry platts oilgram news july 2 2014Analisis colombian petroleum industry platts oilgram news july 2 2014
Analisis colombian petroleum industry platts oilgram news july 2 2014
 
CSS for Mobile
CSS for MobileCSS for Mobile
CSS for Mobile
 
Grails Worst Practices
Grails Worst PracticesGrails Worst Practices
Grails Worst Practices
 
Aavisa - Chennai's first Eco Sensitive Golf Township.
Aavisa  - Chennai's first Eco Sensitive Golf Township.Aavisa  - Chennai's first Eco Sensitive Golf Township.
Aavisa - Chennai's first Eco Sensitive Golf Township.
 
NMDC under pressure to reduce iron ore prices
NMDC under pressure to reduce iron ore pricesNMDC under pressure to reduce iron ore prices
NMDC under pressure to reduce iron ore prices
 
Echoing the brief 013113
Echoing the brief 013113Echoing the brief 013113
Echoing the brief 013113
 
HDOT one of the topmost Manufacturing Units in India
HDOT one of the topmost Manufacturing Units in IndiaHDOT one of the topmost Manufacturing Units in India
HDOT one of the topmost Manufacturing Units in India
 
Platts oilgram news 20140404
Platts oilgram news 20140404Platts oilgram news 20140404
Platts oilgram news 20140404
 
IVRCL - Company Profile.
IVRCL - Company Profile.IVRCL - Company Profile.
IVRCL - Company Profile.
 
242266287 case-study-on-guil
242266287 case-study-on-guil242266287 case-study-on-guil
242266287 case-study-on-guil
 
RINL plans to set up cement rs.3000 crore, 6mtpa cement plant
RINL plans to set up cement rs.3000 crore, 6mtpa cement plantRINL plans to set up cement rs.3000 crore, 6mtpa cement plant
RINL plans to set up cement rs.3000 crore, 6mtpa cement plant
 
242269855 dell-case-study
242269855 dell-case-study242269855 dell-case-study
242269855 dell-case-study
 

Similar a F#とC#で見る関数志向プログラミング

Ekmett勉強会発表資料
Ekmett勉強会発表資料Ekmett勉強会発表資料
Ekmett勉強会発表資料
時響 逢坂
 
PostgreSQL - C言語によるユーザ定義関数の作り方
PostgreSQL - C言語によるユーザ定義関数の作り方PostgreSQL - C言語によるユーザ定義関数の作り方
PostgreSQL - C言語によるユーザ定義関数の作り方
Satoshi Nagayasu
 
Replace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JPReplace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JP
Akira Takahashi
 
Effective java 輪読会 第6章 項目32-34
Effective java 輪読会 第6章 項目32-34Effective java 輪読会 第6章 項目32-34
Effective java 輪読会 第6章 項目32-34
Appresso Engineering Team
 

Similar a F#とC#で見る関数志向プログラミング (20)

たのしい関数型
たのしい関数型たのしい関数型
たのしい関数型
 
(Ruby使いのための)Scalaで学ぶ関数型プログラミング
(Ruby使いのための)Scalaで学ぶ関数型プログラミング(Ruby使いのための)Scalaで学ぶ関数型プログラミング
(Ruby使いのための)Scalaで学ぶ関数型プログラミング
 
Ekmett勉強会発表資料
Ekmett勉強会発表資料Ekmett勉強会発表資料
Ekmett勉強会発表資料
 
プログラミング言語Scala
プログラミング言語Scalaプログラミング言語Scala
プログラミング言語Scala
 
PostgreSQL - C言語によるユーザ定義関数の作り方
PostgreSQL - C言語によるユーザ定義関数の作り方PostgreSQL - C言語によるユーザ定義関数の作り方
PostgreSQL - C言語によるユーザ定義関数の作り方
 
yieldとreturnの話
yieldとreturnの話yieldとreturnの話
yieldとreturnの話
 
Swiftおさらい
SwiftおさらいSwiftおさらい
Swiftおさらい
 
モナドハンズオン前座
モナドハンズオン前座モナドハンズオン前座
モナドハンズオン前座
 
Replace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JPReplace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JP
 
Effective java 輪読会 第6章 項目32-34
Effective java 輪読会 第6章 項目32-34Effective java 輪読会 第6章 項目32-34
Effective java 輪読会 第6章 項目32-34
 
Swift 2.0 で変わったところ「後編」 #cswift
Swift 2.0 で変わったところ「後編」 #cswiftSwift 2.0 で変わったところ「後編」 #cswift
Swift 2.0 で変わったところ「後編」 #cswift
 
Rの高速化
Rの高速化Rの高速化
Rの高速化
 
Ekmett勉強会発表資料
Ekmett勉強会発表資料Ekmett勉強会発表資料
Ekmett勉強会発表資料
 
Node.jsでつくるNode.js ミニインタープリター&コンパイラー
Node.jsでつくるNode.js ミニインタープリター&コンパイラーNode.jsでつくるNode.js ミニインタープリター&コンパイラー
Node.jsでつくるNode.js ミニインタープリター&コンパイラー
 
Composable Callbacks & Listeners
Composable Callbacks & ListenersComposable Callbacks & Listeners
Composable Callbacks & Listeners
 
Pythonで始めるDropboxAPI
Pythonで始めるDropboxAPIPythonで始めるDropboxAPI
Pythonで始めるDropboxAPI
 
20141128 iOSチーム勉強会 My Sweet Swift
20141128 iOSチーム勉強会 My Sweet Swift20141128 iOSチーム勉強会 My Sweet Swift
20141128 iOSチーム勉強会 My Sweet Swift
 
何が選ばれたかという情報から評価関数推定2
何が選ばれたかという情報から評価関数推定2何が選ばれたかという情報から評価関数推定2
何が選ばれたかという情報から評価関数推定2
 
boost tour 1.48.0 all
boost tour 1.48.0 allboost tour 1.48.0 all
boost tour 1.48.0 all
 
言語処理系入門€5
言語処理系入門€5言語処理系入門€5
言語処理系入門€5
 

Último

Último (10)

論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
 
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
 
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアルLoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
 
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
 
Utilizing Ballerina for Cloud Native Integrations
Utilizing Ballerina for Cloud Native IntegrationsUtilizing Ballerina for Cloud Native Integrations
Utilizing Ballerina for Cloud Native Integrations
 
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
 
新人研修 後半 2024/04/26の勉強会で発表されたものです。
新人研修 後半        2024/04/26の勉強会で発表されたものです。新人研修 後半        2024/04/26の勉強会で発表されたものです。
新人研修 後半 2024/04/26の勉強会で発表されたものです。
 
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
 
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
 
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
LoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイスLoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイス
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
 

F#とC#で見る関数志向プログラミング

  • 1. F# と C# で見る関数志向プロ グラミング 目的 F#とC#で同じこと をやってみよう
  • 2. 変数いろいろ let n = 1 // 数値リテラル var n = 1; // 数値リテラル let s = "123" // 文字列リテラル var s = "123"; // 文字列リテラル let li = [ 1; 2; 3; 4; 5] // リストリテラル // リストリテラル? var li = new List<int> { 1, 2, 3, 4, 5 }; // list comprehension let li2 = // リスト内包表記...?   [ for x in 1..20 do var li2 =     if x % 2 = 0 then Enum erable.Range(1, 20)      yield x ] .W here(x => x % 2 == 0).ToList(); // Sequence = Enum elable<T> // もうLinqで let sq = var sq =  seq { for x in 1..10 - > x, x*x, x*x*x } from x in Enumerable.Range(1, 10) select new { X=x, XX=x*x, XXX=x*x*x };
  • 3. 関数(メソッド)いろいろ // 普通の // 普通の let func x = x + 10 static int Func(int x) { return x + 10; } // 繰り返し // 繰り返し let factorial n = static int Factorial(int n) { let rec loop accum n = var result = 1; if n < 1 then accum for (int i = 1; i <= n; i++) else loop (accum * n) (n - 1) result *= i; loop 1 n return result; } // 高階関数 let rec fold proc init li = // 高階関数 m atch li w ith static TResult Fold<TSource, TResult>( | [] - > init Func<TResult, TSource, TResult> proc, | x::xs - > fold proc (proc init x) xs TResult init, List<TSource> li) { foreach (TSource e in li) init = proc(init, e); return init; }
  • 4. ライブラリの高階関数 // 要素番号と要素を列挙する // 要素番号と要素を列挙する Seq.iteri (printfn "%d:%d") [1..10] var index = 0; foreach (var x in Enum erable.Range(1, // 要素に関数を適用した結果をSeqにして返す 10).Select( x => new { i=index++, x=x })) let a = Seq.m (fun x - > x * x) [1..10] ap Console.W riteLine(x.ToString()); // 条件を満たす要素のみのSeqを返す // 要素に関数を適用した結果をEnum rable<T>に let b = Seq.filter (fun x - > x % 2 = 0) [1..10] して返す var a = from x in Enumerable.Range(1, 10) // グルーピング select x * x; let c = Seq.groupBy (fun x - > x % 3) [1..20] // 条件を満たす要素のみのSeqを返す var b = from x in Enumerable.Range(1, 10) where x % 2 == 0 select x; // グルーピング var c = Enum erable.Range(1, 20).GroupBy(x => x % 3);
  • 5. 関数を返す高階関数 // メモイズ // メモイズ let m oize func = em static Func<T, TResult> M oize<T, em let cach = ref M ap.em pty TResult>(Func<T, TResult> func) fun n - > { m atch M ap.tryFind n !cach with var cach = new Dictionary<T, TResult>(); | Som v - > v e return (T n) => { | None - > var v = default(TResult); let tem = func n p if (cach.TryGetValue(n, out v)) cach := M ap.add n tem !cach p return v; tem p else { var tem = func(n); p cach.Add(n, temp); return temp; } }; }
  • 6. まとめ F# と C# で同じことはできるの か? ↓ できる!がんばればっ!
  • 7. 関数を整理してみる model type action unit -> unit comparison function Type -> Type -> int callback Type -> unit delayed computation unit -> Type transformer Type1 -> Type2 sink Type -> unit visitor accumulating function Type1 -> Type2 -> Type2
  • 8. 高階関数って結局何ができ る? 「処理」と「制御」と「構造」を 簡単 に分離できる ↓ IoC パターンの促進