小编ssa*_*anj的帖子

确实的Scala具有转换一个序号[选项[T]]于Seq [T]的API方法?

是否有Scala API方法来转换Seq[Option[T]] -> Seq[T]

您可以通过以下方式手动完成:

seq.filter(_.isDefined).map(_.get)
Run Code Online (Sandbox Code Playgroud)

想知道是否有一种方法可以在通用API中执行上述操作.

scala option

29
推荐指数
1
解决办法
7623
查看次数

是否有一种API方法可以比较Seq的内容而不管顺序如何?

假设:

val l1 = List(1,2,3) 
val l2 = List(2,3,1)
Run Code Online (Sandbox Code Playgroud)

我想,确认L1等于L2(如在相同的内容,但不同的顺序)的方法.List/Seq上是否有API方法来执行此操作?

l1.sameElements(l2)
Run Code Online (Sandbox Code Playgroud)

不起作用,因为它也验证订单.

我想出了以下内容:

l1.foldLeft(l1.size == l2.size)(_ && l2.contains(_))
Run Code Online (Sandbox Code Playgroud)

有没有比上面更简洁的做这个比较?

scala equality

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

为什么这个Haskell代码编译?

鉴于:

uncurry :: (a-> b -> c) -> (a,b) -> c    
id :: a -> a
Run Code Online (Sandbox Code Playgroud)

uncurry id在类型函数中调用结果:(b -> c, b) -> c

我们如何得到这个结果?

如何使用id(a - > a)作为第一个参数来表示,这需要一个(a - > b - > c)函数?

haskell

17
推荐指数
2
解决办法
682
查看次数

Scala迭代器与地图和为

鉴于:

val list = List("one","two","three")     
val it = list.toIterator
Run Code Online (Sandbox Code Playgroud)

我可以跑:

list map ("_" +) -> List(_one, _two, _three)
for (i <- list) yield("_" + i) -> List(_one, _two, _three)
Run Code Online (Sandbox Code Playgroud)

如果我在迭代器上运行相同的操作,我得到:

it map ("_" + ) -> Iterator[java.lang.String] = empty iterator
for (i <- it) yield("_" + i) -> Iterator[java.lang.String] = empty iterator
Run Code Online (Sandbox Code Playgroud)

在我运行map/for之后,我不应该再找回另一个(非空的)Iterator [String]吗?

iterator for-loop scala map scala-collections

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

如何调用带有Tuple2的2个参数的函数?

我有这样的功能:

def print(name:String, surname:String) { println(name + " " + surname) }
Run Code Online (Sandbox Code Playgroud)

我也有一个Tuple2:

val johnsmith = ("John", "Smith")
Run Code Online (Sandbox Code Playgroud)

当我用johnsmith调用print时,我收到以下错误:

scala> print(johnsmith)                                                       

error: not enough arguments for method print: (name: String,surname: String)Unit.
Unspecified value parameter surname.
       print(johnsmith)
            ^
Run Code Online (Sandbox Code Playgroud)

这有什么办法吗?我可以通过让print接受Tuple2来实现这一点:

def print2(t:Tuple2[String,String]) { println(t._1 + " " + t._2) }
Run Code Online (Sandbox Code Playgroud)

现在我可以用任何一种方式调用它:

scala> print2(johnsmith)
John Smith

scala> print2("john", "smith")
john smith
Run Code Online (Sandbox Code Playgroud)

有什么我想念的吗?

scala tuples

10
推荐指数
2
解决办法
591
查看次数

当与下划线一起使用时,部分函数应用程序会过早地运行代码块

鉴于:

def save(f: => Any)(run:Boolean) { if (run) { println("running f"); f } else println("not running f") } 
Run Code Online (Sandbox Code Playgroud)

我可以用它来调用它:

save("test")(true) -> running f
save("test")(false) -> not running f
save(throw new RuntimeException("boom!"))(false) -> not running f
save(throw new RuntimeException("boom!"))(true) -> running f and then exception thrown
Run Code Online (Sandbox Code Playgroud)

这是部分应用的奇怪行为:

save(throw new RuntimeException("boom!"))(_) -> (Boolean) => Unit = <function1> //as expected
save(throw new RuntimeException("boom!")) _ -> exception thrown
Run Code Online (Sandbox Code Playgroud)

立即评估代码块而不作为函数传递.上述两个陈述有什么区别?

scala currying partialfunction eta-expansion

9
推荐指数
1
解决办法
573
查看次数

带有currying的默认参数

我可以将函数定义为:

def print(n:Int, s:String = "blah") {}
print: (n: Int,s: String)Unit
Run Code Online (Sandbox Code Playgroud)

我可以用它来调用它:

print(5) 
print(5, "testing")
Run Code Online (Sandbox Code Playgroud)

如果我讨论以上内容:

def print2(n:Int)(s:String = "blah") {} 
print2: (n: Int)(s: String)Unit
Run Code Online (Sandbox Code Playgroud)

我不能用1参数调用它:

print2(5)
<console>:7: error: missing arguments for method print2 in object $iw;
follow this method with `_' if you want to treat it as a partially applied function
       print2(5)
Run Code Online (Sandbox Code Playgroud)

我必须提供这两个参数.这有什么办法吗?

parameters scala currying

8
推荐指数
1
解决办法
1705
查看次数

使用Github Api V4从Github repo获取最后x次提交

我试图使用新的Github上GraphQL API(V4),我似乎并没有能够弄清楚如何获得最后x提交对.我使用过存储库参考,但他们仍然没有给我我需要的东西.

下面的查询几乎给了我我需要的东西:

query{
  repository(owner: "typelevel", name: "cats") {
    refs(refPrefix:"refs/heads/", last: 5) {
      edges{
        node {
          associatedPullRequests(states: MERGED, last: 5) {
            edges{
              node {
                title
                baseRef {
                  name
                  prefix
                }
                baseRefName
                commits(last: 10) {
                  edges {
                    node {
                      commit {
                        abbreviatedOid
                        message

                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但:

  1. 似乎与回购中的内容完全匹配
  2. 限于PR
  3. 看起来太笨重了

我也尝试过使用defaultBranchRef,但这也不起作用:

query{
  repository(owner: "typelevel", name: "cats") {
    defaultBranchRef {
      name
      prefix
      associatedPullRequests(states: [MERGED], last: …
Run Code Online (Sandbox Code Playgroud)

github github-api graphql

7
推荐指数
1
解决办法
1620
查看次数

Scala模式匹配在嵌套案例类中并非详尽无遗

我有一个案例类层次结构来编码一些请求和处理错误:

  sealed trait OpError
  sealed trait RequestErrorType
  sealed trait ProcessingErrorType

  final case class InvalidEndpoint(reason: String) extends RequestErrorType
  final case class InvalidParameters(reason: String) extends RequestErrorType

  final case class InvalidFormat(response: String) extends ProcessingErrorType
  final case class EntityNotFound(id: Long) extends ProcessingErrorType

  final case class RequestError(errorType: RequestErrorType) extends OpError
  final case class ProcessingError(errorType: ProcessingErrorType) extends OpError
Run Code Online (Sandbox Code Playgroud)

如果我在所有模式中编写简单匹配:

  def printMatches(error: OpError): Unit = error match {
    case RequestError(InvalidEndpoint(reason)) => //print something
    case RequestError(InvalidParameters(reason)) => //print something
    case ProcessingError(InvalidFormat(format)) => //print something
    case ProcessingError(EntityNotFound(entityId)) => //print …
Run Code Online (Sandbox Code Playgroud)

scala pattern-matching

6
推荐指数
1
解决办法
1022
查看次数

在函数应用程序上找不到隐式参数

如果我定义仅将数字作为以下内容的打印功能:

def print[T <% Number](value:T) {}
print: [T](value: T)(implicit evidence$1: (T) => java.lang.Number)Unit
Run Code Online (Sandbox Code Playgroud)

我可以通过以下方式致电上述内容:

print(5)
print(5.5)
print(6L) 
Run Code Online (Sandbox Code Playgroud)

但不能使用字符串:

print("aaa")  
<console>:7: error: could not find implicit value for evidence parameter of type (java.lang.String) => java.lang.Number
       print("aaa")
Run Code Online (Sandbox Code Playgroud)

这是预期的。

但是,如果我将打印功能定义为:

def print2[T <% Number]: T => Unit = value => { } 
print2: [T](implicit evidence$1: (T) => java.lang.Number)(T) => Unit
Run Code Online (Sandbox Code Playgroud)

注意隐式参数是第一个参数而不是最后一个参数。

如果我尝试手动定义以上功能:

def print3[T](implicit f: (T) => java.lang.Number)(value:T):Unit =  { }  
<console>:1: error: '=' expected but '(' found.
       def print3[T](implicit f: (T) => …
Run Code Online (Sandbox Code Playgroud)

scala function implicit-conversion

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