Functional

重载了几个运算符,了解运算符重载的概念,同时了解在JSON 处理中的一些逻辑:

infix operator >>> {
  // bind
  associativity left precedence 150
}

infix operator <^> {
  // Functor's fmap(usually <$>)
  associativity left
}

infix operator <*> {
  // Applicative's apply
  associativity left
}

func >>><A, B>(a: A?, f: A->B?)->B? {
    if let x = a {
        return f(x)
    }else {
        return .None
    }
}

func >>><A, B>(a:Result<A>, f:A-> Result<B>)->Result<B> {
    switch a {
        case let .Value(x): return f(x.value)
        case let .Error(error): return .Error(error)
    }
}

func <^><A, B>(f: A->B, a: A?)->B? {
    if let x = a {
        return f(x)
    }else {
        return .None
    }
}

func <*><A, B>(f:(A->B)?, a: A?)->B? {
    if let x = a {
        if let fx = f {
            return fx(x)
        }
    }

    return .None
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容