是否可以foldLeft在参数列表上执行a ,其中提供给fold的初始值是完全curried函数,运算符是apply,并且列表是要传递给函数的参数列表f?
例如,假设f定义为:
scala> val f = (i: Int, j: Int, k: Int, l: Int) => i+j+k+l
f: (Int, Int, Int, Int) => Int = <function4>
Run Code Online (Sandbox Code Playgroud)
我们当然可以直接使用它:
scala> f(1, 2, 3, 4)
res1: Int = 10
Run Code Online (Sandbox Code Playgroud)
或者咖喱并一次应用一个参数:
scala> f.curried
res2: Int => Int => Int => Int => Int = <function1>
scala> f.curried.apply(1).apply(2).apply(3).apply(4)
res3: Int = 10
Run Code Online (Sandbox Code Playgroud)
乍一看,这看起来像是一份工作foldLeft.
我第一次尝试描述这个apply使用顺序foldLeft如下:
scala> List(1, 2, 3, 4).foldLeft(f.curried)({ (g, x) => g.apply(x) …Run Code Online (Sandbox Code Playgroud) 我一直支持在工作中使用Moose(和MooseX :: Declare)几个月.它鼓励的风格将真正有助于我们的代码库的可维护性,但不是没有学习新语法的初始成本,特别是在学习如何解析类型验证错误.
我已经在网上看到了这个问题的讨论,并认为我会向这个社区发布一个查询:
a)已知的解决方案
b)讨论验证错误消息应该是什么样子
c)提出实现一些想法的概念证明
我也会联系作者,但我也看到了这个论坛的一些很好的讨论,所以我想我会公开发布一些东西.
#!/usr/bin/perl
use MooseX::Declare;
class Foo {
has 'x' => (isa => 'Int', is => 'ro');
method doit( Int $id, Str :$z, Str :$y ) {
print "doit called with id = " . $id . "\n";
print "z = " . $z . "\n";
print "y = " . $y . "\n";
}
method bar( ) {
$self->doit(); # 2, z => 'hello', y => 'there' );
}
}
my …Run Code Online (Sandbox Code Playgroud) 目前推荐的Scala家族多态性模式是什么?
在尝试建模游戏的方法时,最近出现了这个解决方案:
trait Game[G <: Game[G]] {
type PLAYER <: Player[G]
type STATE <: State[G]
def players(): Set[G#PLAYER]
def startState(): G#STATE
}
trait Player[G <: Game[G]]
trait State[G <: Game[G]] {
def player(): G#PLAYER
}
Run Code Online (Sandbox Code Playgroud)
特定游戏(本例中的扑克)可以用这样的特征来表达:
class Poker() extends Game[Poker] {
type PLAYER = PokerPlayer
type STATE = PokerState
val p1 = new PokerPlayer()
def players() = Set(p1)
def startState(): PokerState = ...
}
class PokerPlayer() extends Player[Poker]
class PokerState() extends State[Poker] {
def player(): PokerPlayer = ...
}
Run Code Online (Sandbox Code Playgroud)
我有几个关于此设置的问题: …
如何配置sbt 0.10以将junitxml选项与specs2配合使用?
该specs2文档说,这是用SBT 0.7.x来做到这一点:
override def testOptions = super.testOptions ++ Seq(TestArgument("junitxml"))
如何在sbt 0.10中说同样的话?
感谢https://github.com/milessabin/shapeless/wiki/Feature-overview:-shapeless-2.0.0我理解如何压缩无形的HLists:
从Shapeless 2.0.0-M1导入一些东西:
import shapeless._
import shapeless.ops.hlist._
import syntax.std.tuple._
import Zipper._
Run Code Online (Sandbox Code Playgroud)
创建两个HLists:
scala> val h1 = 5 :: "a" :: HNil
h1: shapeless.::[Int,shapeless.::[String,shapeless.HNil]] = 5 :: a :: HNil
scala> val h2 = 6 :: "b" :: HNil
h2: shapeless.::[Int,shapeless.::[String,shapeless.HNil]] = 6 :: b :: HNil
Run Code Online (Sandbox Code Playgroud)
拉链他们:
scala> (h1, h2).zip
res52: ((Int, Int), (String, String)) = ((5,6),(a,b))
Run Code Online (Sandbox Code Playgroud)
现在尝试定义一个执行相同操作的函数:
scala> def f[HL <: HList](h1: HL, h2: HL) = (h1, h2).zip
f: [HL <: shapeless.HList](h1: HL, h2: HL)Unit
Run Code Online (Sandbox Code Playgroud)
推断的返回类型是Unit,实际上将f应用于h1和h2就是这样:
scala> …Run Code Online (Sandbox Code Playgroud)