zoukankan      html  css  js  c++  java
  • 函数式编程的类型系统:typeclass--Functor的解释--构造类型

    函数式编程的类型系统:typeclass

    Typeclass是带有关联构造类型的抽象接口,抽象接口的行为用于约束构造类型。

    构造类型实现了抽象接口的行为约束,就称这个实现为这个构造类型的函子。

    要素:1、关联的构造类型;2、建立在这个构造类型上的的约束。

    3、构造类型的关联类型的概念与行为,及与构造类型复合到一起的行为。

    构造类型与关联类型的复合行为。

    typeclass是上面行为的描述;

    结构:typeclass->构造类型(添加约束)->关联类型(具体类型)。

    //list Functor的实现

    def listFunctor = new Functor[List] {

     def map[A, B](a: List[A])(f: (A) => B) = a.map(f)

    考虑重点:1、构造类型的行为;2、关联类型的行为。

    A typeclass is a sort of interface that defines some behavior. If a type is a part of a typeclass, that means that it supports and implements the behavior the typeclass describes. A lot of people coming from OOP get confused by typeclasses because they think they are like classes in object oriented languages. Well, they’re not. You can think of them kind of as Java interfaces, only better.

    trait Functor[F[_]] {

     def map[A, B](a: F[A])(f: A => B): F[B]

    }

    F[_]:关联构造类型;

    关于FunctorApplicativeMonad的概念,其实各用一句话就可以概括:

    1. 一个Functor就是一种实现了Functor typeclass的数据类型;
    2. 一个Applicative就是一种实现了Applicative typeclass的数据类型;
    3. 一个Monad就是一种实现了Monad typeclass的数据类型。

    勘误:这里的数据类型应该是构造类型;

    当然,你可能会问那什么是typeclass呢?我想当你在看到实现二字的时候,就应该已经猜到了:

    A typeclass is a sort of interface that defines some behavior. If a type is a part of a typeclass, that means that it supports and implements the behavior the typeclass describes. A lot of people coming from OOP get confused by typeclasses because they think they are like classes in object oriented languages. Well, they’re not. You can think of them kind of as Java interfaces, only better.

    https://www.cnblogs.com/feng9exe/p/8626102.html

    Functor的代码表示

    trait Functor[F[_]] {

     def map[A, B](a: F[A])(f: A => B): F[B]

    }

    //list Functor的实现

    def listFunctor = new Functor[List] {

     def map[A, B](a: List[A])(f: (A) => B) = a.map(f)

    }

    https://www.cnblogs.com/feng9exe/p/9700779.html

    Functor 定义如下:

    class Functor f where

      fmap :: (a -> b) -> f a -> f b

    由 f a 和 f b 我们可知,f 不是类型,而是类型构造器(type constructor),即 f 应接受另一类型作为参数并返回一个具体的类型(更精准的表达则是 f 的 kind 必须是 * -> *)。

    https://www.cnblogs.com/feng9exe/p/9152447.html

    swift

    /// A type that has reactive extensions.

    public protocol ReactiveCompatible {

        /// Extended type

        associatedtype CompatibleType

        /// Reactive extensions.

        var rx: Reactive<CompatibleType> { get set }

    }

    extension ReactiveCompatible {

        /// Reactive extensions.

        public var rx: Reactive<Self> {

            get {

                return Reactive(self)

            }

            set {

                // this enables using Reactive to "mutate" base object

            }

        }

    }

    Reactive<CompatibleType> :构造类型;

    rx:接口约束;

  • 相关阅读:
    swift锁屏播放,音乐进度更新,专辑,歌手名显示
    swift Dictionary 字典
    Swift中的集合类型
    Swift String 一些常用方法
    Swift自定义Class实现Hashable
    二元最近的共同祖先问题(O(n) time 而且,只有一次遍历,O(1) Space (它不考虑函数调用栈空间))
    BZOJ1579 USACO 2009 Feb Gold 3.Revamping Trails Solution
    [DEEP LEARNING An MIT Press book in preparation]Linear algebra
    POJ--2391--Ombrophobic Bovines【分割点+Floyd+Dinic优化+二分法答案】最大网络流量
    c#-RTF文本编辑器
  • 原文地址:https://www.cnblogs.com/feng9exe/p/10457666.html
Copyright © 2011-2022 走看看