小编Kev*_*ith的帖子

转发类示例

阅读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)

java effective-java

5
推荐指数
1
解决办法
4505
查看次数

Scala命名为参数

Named ArgumentsScala中深入研究这个例子:

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)

scala

5
推荐指数
1
解决办法
706
查看次数

ui-router的$ urlRouterProvider.otherwise与HTML5模式

请考虑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 …

html5 angularjs angular-ui-router

5
推荐指数
1
解决办法
1万
查看次数

在Scala和Haskell中"展平"一个List

鉴于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通道flatMapflatten:

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)

haskell scala

5
推荐指数
2
解决办法
510
查看次数

`this`输入Scala

看看这个问题,在创建时用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

5
推荐指数
1
解决办法
92
查看次数

总和或产品类型?

给定以下代数数据类型

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?为什么?

scala algebraic-data-types

5
推荐指数
1
解决办法
2105
查看次数

jQuery`child`与`find`

HTML

<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)

https://jsfiddle.net/o71x6co6/1/

jquery

5
推荐指数
1
解决办法
54
查看次数

将元组添加到地图?

使用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)

scala

5
推荐指数
1
解决办法
1825
查看次数

Coproduct对"密封特性"的好处?

我在优秀的无形指南中阅读了以下关于副产品内容:

...值得一提的是,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?的好处是什么?

scala shapeless

5
推荐指数
1
解决办法
460
查看次数

模式匹配 - @对:?

鉴于:

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)

scala

5
推荐指数
1
解决办法
278
查看次数