阅读Effective Java,我从Item 16: Favor composition over inheritance.
在下面InstrumentedSet,这本书显示我们可以跟踪元素被插入的次数(通过InstrumentedSet.addCount变量)。
要做到这一点,我们可以简单地附加到这个类对象的addCount,然后调用ForwardingSet.add(),它调用实际Set类的 实际实现add()。
// Reusable forwarding class
public class ForwardingSet<E> implements Set<E> {
private final Set<E> s;
public ForwardingSet(Set<E> s) { this.s = s; }
public void clear() { s.clear(); }
public boolean contains(Object o) { return s.contains(o); }
...
}
// Wrapper class - uses composition in place of inheritance
public class InstrumentedSet<E> extends ForwardingSet<E> { …Run Code Online (Sandbox Code Playgroud) 我Named Arguments在Scala中深入研究这个例子:
scala> class Parent {
| def foo(bar: Int = 1, baz: Int = 2): Int = bar + baz
| }
defined class Parent
scala> class Child extends Parent {
| override def foo(baz: Int = 3, bar: Int = 4): Int = super.foo(baz, bar)
| }
defined class Child
scala> val p = new Parent
p: Parent = Parent@6100756c
scala> p.foo()
res1: Int = 3
scala> val x = new Child
x: Child …Run Code Online (Sandbox Code Playgroud) 请考虑ui-router的wiki中的以下略微修改的代码.
var myApp = angular.module('myApp',['ui.router']);
myApp.config(function($stateProvider, $urlRouterProvider, $locationProvider) {
// for any unmatched url
$urlRouterProvider.otherwise("/state1");
$locationProvider.html5Mode(true);
// now set up the states
$stateProvider
.state('state1', {
url: '/state1',
templateUrl: "partials/state1.html"
})
.state('state1.list', {
url: "/list",
templateUrl: "partials/state1.list.html",
controller: function($scope) {
$scope.items = ["A", "List", "of", "Items"];
}
})
.state('state2', {
url: "/state2",
templateUrl: "partials/state2.list.html",
controller: function($scope) {
$scope.things = ["a", "set", "of", "things"];
}
})
});
Run Code Online (Sandbox Code Playgroud)
运行python 3的SimpleHttpServer,我404 error在尝试访问时得到:http://localhost:8000/state1234324324.
为什么没有$urlRouterProvider.otherwise("/state1");重定向所有未知路由/state1,根据这个问题,已经定义state …
鉴于List[Option[Int]]:
scala> list
res8: List[Option[Int]] = List(Some(1), Some(2), None)
Run Code Online (Sandbox Code Playgroud)
我可以得到List(1,2),即提取list通道flatMap和flatten:
scala> list.flatten
res9: List[Int] = List(1, 2)
scala> list.flatMap(x => x)
res10: List[Int] = List(1, 2)
Run Code Online (Sandbox Code Playgroud)
鉴于[Maybe Int]Haskell中的以下内容,我该如何执行上述操作?
我尝试了下面的失败:
import Control.Monad
maybeToList :: Maybe a -> [b]
maybeToList Just x = [x]
maybeToList Nothing = []
flatten' :: [Maybe a] -> [a]
flatten' xs = xs >>= (\y -> y >>= maybeToList)
Run Code Online (Sandbox Code Playgroud) 看看这个问题,在创建时用for循环填充不可变地图,我很好奇是什么this意思Map(1 -> this).
scala> Map(1 -> this)
res6: scala.collection.immutable.Map[Int,type] = Map(1 -> @53e28097)
scala> res6(1)
res7: type = @53e28097
Run Code Online (Sandbox Code Playgroud)
我之前没有见过type作为一种类型.
它是什么?
给定以下代数数据类型:
scala> sealed trait Person
defined trait Person
scala> case class Boy(name: String, age: Int, x: String) extends Person
defined class Boy
scala> case class Girl(name: String, age: Int, y: Boolean) extends Person
defined class Girl
Run Code Online (Sandbox Code Playgroud)
注意 - 我知道它不是递归类型 - 不涉及递归。
那么,这是 aSum还是Product Type?为什么?
<table id='t'>
<tr>
<td id='foo' class='a b c'>blah</td>
<td id='bar' class='a c'>bloo</td>
<td id='zip' class='a b c'>blop</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
使用jQuery,为什么以下children调用返回0
$('#t').children('tr').length
Run Code Online (Sandbox Code Playgroud)
但find返回1?
$('#t').find('tr').length
Run Code Online (Sandbox Code Playgroud)
使用Scala 2.11.8,我可以key - value通过以下方式将一对附加到Map:
scala> Map( (1 -> 1) ) + (2 -> 2)
res8: scala.collection.immutable.Map[Int,Int] = Map(1 -> 1, 2 -> 2)
Run Code Online (Sandbox Code Playgroud)
但是,如何使用元组作为+的参数?
scala> Map( (1 -> 1) ) + (2, 2)
<console>:12: error: type mismatch;
found : Int(2)
required: (Int, ?)
Map( (1 -> 1) ) + (2, 2)
^
Run Code Online (Sandbox Code Playgroud)
也不起作用:
scala> Map( (1, 1) ) + (2, 2)
<console>:12: error: type mismatch;
found : Int(2)
required: (Int, ?)
Map( (1, 1) ) + …Run Code Online (Sandbox Code Playgroud) ...值得一提的是,Coproducts并不特别特别.上述功能可以使用Either和Nothing代替:+:和CNil来实现.
这是上面的代码:
import shapeless.{Coproduct, :+:, CNil, Inl, Inr}
case class Red()
case class Amber()
case class Green()
type Light = Red :+: Amber :+: Green :+: CNil
val red: Light = Inl(Red())
// red: Light = Inl(Red())
val green: Light = Inr(Inr(Inl(Green())))
// green: Light = Inr(Inr(Inl(Green())))
Run Code Online (Sandbox Code Playgroud)
根据我自己的理解,使用Coproduct而不是sealed trait?的好处是什么?
鉴于:
case object A
在@和之间有什么区别(如果有的话)::
def f(a: A.type): Int = a match {
case aaa @ A => 42
}
Run Code Online (Sandbox Code Playgroud)
和
def f(a: A.type): Int = a match {
case aaa : A.type => 42
}
Run Code Online (Sandbox Code Playgroud)