Skip to content

Instantly share code, notes, and snippets.

@Centaur
Last active August 29, 2015 14:17
Show Gist options
  • Save Centaur/ed0b55907d2261b572cd to your computer and use it in GitHub Desktop.
Save Centaur/ed0b55907d2261b572cd to your computer and use it in GitHub Desktop.
nested `andThen` transformed to normal function application

先从简单的情况入手,假设 f1 的类型是 A => B, 看看

    f2 andThen {
	    _ andThen f1
    }

到底是什么意思 先去掉语法糖,完整形式为:

    f2 andThen { x =>
      x andThen f1
    }

其中 x 的类型应为 C => A, 它也是f2的返回类型。

{ x => x andThen f1} 的类型应为 (C => A) => C => B

f2的类型为 D => (C => A)

    f2 andThen {
	    _ andThen f1
    }

的类型应为 D => C => B
等价于 (d:D)=>(c:C)=>f1(f2(d)(c))

下面来看原题:

    f3 andThen {
	    _ andThen {
		      _ andThen f1
	    }
    }
    f3 andThen { x =>
        x andThen {
    	    _ andThen f1
        }
    }

x的类型应为f2的类型 D => C => A ,它也是f3的返回类型,

{x => x andThen ...} 的类型应为 D => C => A => D => C => B

f3 的类型应为 E => D => C => A

整体的类型应为 E => D => C => B

等价于 (e:E)=>(d:D)=>(c:C)=>f1(f3(e)(d)(c))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment