Reader 2.7.3

Reader 2.7.3

to4iki 维护。



Reader 2.7.3

  • Julius Oklamcak

Reader

Build Status Carthage compatible Cocoapod version

Swift中的Reader monad,灵感来源于 Scala实现

概览

Reader 是对接受一些 输入 产生 元素 的函数的一个封装。

// init(_:)
let reader = Reader<Int, Int> { $0 + 2 }

// execute(_:)
reader.execute(1) // 3
reader.execute(2) // 4

// map(_:)
reader
    .map { $0 * 2 }
    .execute(3) // 10

// flatMap(_:)
reader
    .flatMap { i1 in Reader<Int, Int> { i2 in i1 * i2 } }
    .execute(4) // 24

💉

使用 Reader 可以实现简单和静态的 依赖注入

例子:Playground

安装

Carthage

在Cartfile中添加以下行并运行 carthage update

github "to4iki/Reader"

Swift包管理器

在Package.swift中添加以下行并运行 swift build

dependencies: [
    .Package(url: "https://github.com/to4iki/Reader.git", majorVersion: 0)
]