Mono で Rx をはじめてみる

hotmiyacchi.hatenablog.com

上のブログ記事で紹介されている UniRx をみて、コマンドラインから Reactive extensions の練習がしたくなりました。 Mono で Rx をはじめるまでのインストール手順を書きます。

環境

  • OS X El Capitan 10.11.1
$ mono --version
Mono JIT compiler version 4.2.0 (Stable 4.2.0.179/a224653 Fri Aug 28 13:53:46 PDT 2015)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
    TLS:           normal
    SIGSEGV:       altstack
    Notification:  kqueue
    Architecture:  amd64
    Disabled:      none
    Misc:          softdebug
    LLVM:          supported, not enabled.
    GC:            sgen

nuget.exe をダウンロード

NuGet Gallery | Home から nuget.exe をダウンロードしてパスの通ったところに置きます。

nuget で Rx-Main をインストール

$ mono nuget.exe install Rx-Main

を実行すると、カレントディレクトリに Rx-Main がインストールされます。

$ tree -L 2
.
├── Rx-Core.2.2.5
│   ├── Rx-Core.2.2.5.nupkg
│   └── lib
├── Rx-Interfaces.2.2.5
│   ├── Rx-Interfaces.2.2.5.nupkg
│   └── lib
├── Rx-Linq.2.2.5
│   ├── Rx-Linq.2.2.5.nupkg
│   └── lib
├── Rx-Main.2.2.5
│   └── Rx-Main.2.2.5.nupkg
└── Rx-PlatformServices.2.2.5
    ├── Rx-PlatformServices.2.2.5.nupkg
    └── lib

9 directories, 5 files

サンプルコード

// a.cs
using System;
using System.Linq;
using System.Reactive.Linq;

class Program {
    static void Main(string[] args) {
        Observable.Range(1, 3).Subscribe(Console.WriteLine);
    }
}

コンパイルする前に、Rx-XXX-2.2.5 フォルダ以下の dll をカレントディレクトリにコピーします。

  • System.Reactive.Core.dll
  • System.Reactive.Linq.dll
  • System.Reactive.Interfaces.dll
  • System.Reactive.PlatformServices.dll

の 4 つのファイルをカレントディレクトリにコピーしました。いずれも lib/net45 フォルダに配置されているファイルになります。

$ ls
Rx-Core.2.2.5/               System.Reactive.Core.dll
Rx-Interfaces.2.2.5/         System.Reactive.Interfaces.dll
Rx-Linq.2.2.5/               System.Reactive.Linq.dll
Rx-Main.2.2.5/               System.Reactive.PlatformServices.dll
Rx-PlatformServices.2.2.5/   a.cs

次のようにコンパイルします。

$ mcs a.cs -r:System.Reactive.Core.dll,System.Reactive.Linq.dll

実行します。

$ mono a.exe
1
2
3

※カレントディレクトリに dll を配置したのは、mono a.exe で実行時に dll が見つからないエラーが出るためです。

リンク