小编Kev*_*ith的帖子

两种模式匹配

Scala中的Functional Programming,我正在尝试实现Either.map.

trait Either[+E, +A] {
    def map[B](f: A => B): Either[E, B] =  this match {
        case Either(x: E, y: A) => Right(f(y))
        case _ => Left()
    }
}
Run Code Online (Sandbox Code Playgroud)

编译中出现一个错误,其中包括.我没有展示它们,因为我似乎错过了实施的概念Either.

    Either.scala:3: error: value Either is not a case class constructor, 
nor does it have an unapply/unapplySeq method
                    case Either(x: E, y: A) => Right(f(y))
Run Code Online (Sandbox Code Playgroud)

请告知我实施它.

scala either

3
推荐指数
1
解决办法
2553
查看次数

Scala Equality和HashCode

深度Scala在mutability和上呈现此代码equality.

class Point2(var x: Int, var y: Int) extends Equals {
def move(mx: Int, my: Int) : Unit = {
  x = x + mx
  y = y + my
}
override def hashCode(): Int = y + (31*x)

def canEqual(that: Any): Boolean = that match {
  case p: Point2 => true
  case _ => false
}
override def equals(that: Any): Boolean = {
def strictEquals(other: Point2) =
  this.x == other.x && this.y == other.y
  that …
Run Code Online (Sandbox Code Playgroud)

scala equality hashcode

3
推荐指数
1
解决办法
1382
查看次数

使用窗口访问全局变量

为什么不window.x打印出来10

eval("var x = 10;");
console.log(window.x); // undefined
console.log(x); // 10
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/kzd4z/1/

javascript eval jsfiddle

3
推荐指数
1
解决办法
41
查看次数

IntelliJ - 没有这样的实例方法

使用IntelliJ的12 Ultimate,我在调试器中运行以下代码:

Java的

import play.api.libs.json.JsValue;
public class Foo { 
 ...
 public JsValue toJson() { ... }

public class FooExample {
...
  Foo foo = new Foo(); 
  System.out.println("...); //<-- breakpoint
Run Code Online (Sandbox Code Playgroud)

在断点处,我右键单击我的源代码并选择"评估表达式",输入:

foo.toJson().

但出现以下错误:

No such instance method: play.api.libs.json.JsValue$class.com.foo.Foo.toJson ()

难道我做错了什么?Foo#toJson调用Scala代码,如果重要的话.

编辑我实际上在实例化之后有了断点.Foo.对于那些投票的人,我当之无愧.

java scala intellij-idea

3
推荐指数
1
解决办法
9699
查看次数

使用IO Monad进行flatMap和For-Comprehension

查看Scala中的Functional Programming中的IO Monad示例:

def ReadLine: IO[String] = IO { readLine }
def PrintLine(msg: String): IO[Unit] = IO { println(msg) }

def converter: IO[Unit] = for {
  _ <- PrintLine("enter a temperature in degrees fahrenheit")
  d <- ReadLine.map(_.toDouble)
  _ <- PrintLine((d + 32).toString)
} yield ()
Run Code Online (Sandbox Code Playgroud)

我决定converter用一个重写flatMap.

def converterFlatMap: IO[Unit] = PrintLine("enter a temperate in degrees F").
     flatMap(x => ReadLine.map(_.toDouble)).
       flatMap(y => PrintLine((y + 32).toString))
Run Code Online (Sandbox Code Playgroud)

当我用最后一个替换flatMapmap,我没有看到在控制台上打印出readLine的结果.

flatMap:

enter a …
Run Code Online (Sandbox Code Playgroud)

monads scala

3
推荐指数
1
解决办法
361
查看次数

在编译时捕获ArrayStoreException

考虑以下Java ArrayList#toArray方法测试.请注意,我从这个有用的答案中借用了代码.

public class GenericTest {
    public static void main(String [] args) {
        ArrayList<Integer> foo = new ArrayList<Integer>();
        foo.add(1);
        foo.add(2);
        foo.add(3);
        foo.add(4);
        foo.add(5);
        Integer[] bar = foo.toArray(new Integer[10]);
        System.out.println("bar.length: " + bar.length);

        for(Integer b : bar) { System.out.println(b); }

        String[] baz = foo.toArray(new String[10]);       // ArrayStoreException
        System.out.println("baz.length: " + baz.length);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,请注意,ArrayStoreException在尝试Integer进入时会有一个时间String[].

输出:

$>javac GenericTest.java && java -cp . GenericTest
bar.length: 10
1
2
3
4
5
null
null
null …
Run Code Online (Sandbox Code Playgroud)

java generics

3
推荐指数
1
解决办法
490
查看次数

创建用户的厨师食谱

我想创建kevin一个密码为的新用户kevin.

食谱/ make_user /食谱/ default.rb

user "kevin" do
  comment "default user"
  home "/home/kevin"
  shell "/bin/bash"
  password "kevin"
end
Run Code Online (Sandbox Code Playgroud)

在配置我的Vagrant盒子之后,我将其放入盒子中.然而,我无法sukevin与密码的用户kevin.

[vagrant@vagrant-centos65 ~]$ su kevin
Password: 
su: incorrect password
Run Code Online (Sandbox Code Playgroud)

查看Chef'用户' 文档,我不确定这password是否是要修改的正确属性.

password    The password shadow hash. This attribute requires that 
            ruby-shadow be installed. This is part of the Debian package: 
            libshadow-ruby1.8.
Run Code Online (Sandbox Code Playgroud)

如何修改我上面的食谱,这样我可以sukevin使用相同的密码?

chef-infra

3
推荐指数
1
解决办法
3104
查看次数

在Haskell中修改了`break`?

break[a] -> (a -> Bool) -> ([a], [a])根据我的理解,有第一个元组等于的签名takeWhile predicate is true.第二元组是负责使谓词为假加上剩余列表的项目.

> break (== ' ') "hey there bro"
("hey"," there bro")
Run Code Online (Sandbox Code Playgroud)

但是,是否有一个功能会跳过负责破坏的项目?

>foo? (== ' ') "hey there bro"
("hey","there bro")
Run Code Online (Sandbox Code Playgroud)

haskell

3
推荐指数
1
解决办法
183
查看次数

理解Haskell的'toUpper`

查看toUpper的Haskell源代码:

toUpper c = chr (fromIntegral (towupper (fromIntegral (ord c))))  
... 
foreign import ccall unsafe "u_towupper"
  towupper :: CInt -> CInt
Run Code Online (Sandbox Code Playgroud)

是什么意思chr,以及u_towupper?我对这foreign import ccall unsafe部分也很好奇.Haskell源实际上是否会发生变异,因此unsafe

haskell toupper

3
推荐指数
1
解决办法
437
查看次数

在RangeError中使用`Object.defineProperty`

我正在尝试使用Object.defineProperty更新我的obj对象以使用getset访问器obj.name.

var obj = {};

Object.defineProperty(obj, 'name', { 
    get: function() { return this.name; },
    set: function(x) { this.name = x; }
});

console.log("obj:", obj);
console.log("obj.name:", obj.name);
Run Code Online (Sandbox Code Playgroud)

但是我得到了一个Uncaught RangeError: Maximum call stack size exceeded.

如何使用在属性中Object.defineProperty添加getset访问者?nameobj

http://jsfiddle.net/kman007_us/ZwYp6/

javascript

3
推荐指数
1
解决办法
652
查看次数