English 中文(简体)
一些操作者的问题
原标题:some operator questions
  • 时间:2011-11-20 21:08:27
  •  标签:
  • scala

如果情况容易的话,我会 new新的.笑,但我却很难找到答案。

我在时间上很难理解什么和什么,以及(......)和什么(......)单位是做的。 我对这些认识的理解是,“和”有时用于海滩,而且=和”在地图上使用。 试图“scala”“<-”的斜线证明是很有成效的。 见http://jim-mcbeath.blogspot.com/2008/12/scala-operator-cheat-sheet.html。 但它在看一看一看时就无助。

val numbers = List("one", "two", "three","four","five")
def operateOnList() {
  for(number <- numbers) {
    println(number + ": came out of this crazy thing!")
  }
}

def tweener(method: () => Unit) {
  method()
}

tweener(operateOnList)
问题回答

<代码>() => Unit系指该方法不包含任何参数和回报的功能(Unit)。

<- is used in the for comprehension as an kind of assignation operator. for comprehension are a little bit specific because they are internally transformed. In your case, that would be transforms as numbers.foreach(i => println(i + ": came out of this crazy thing!"))

<代码><- in the for comprehension means that we will iterate over each elements of the numbers/code> list and adopted to number

......

for(number <- numbers){
...
}

可在上将每一编号改为

和带;-与不同的语种:-> 。 简而言之,这只是 t子的替代品:(a,b)相当于(a->b)或仅相当于“>”;b。 这一编号之后的含义是maps to b. 因此,这常常用于地图的定义:

 Map("a" -> 1,"aba" -> 3)
 Map("London" -> "Britain", "Paris" -> "France")

这里,你可以想像,通过某种功能(例如,扼杀时间、资本)绘制地图。

www.un.org/Depts/DGACM/index_spanish.htm 更好的解释是

最后,但并非最不重要的是 =>, 也是地图,但具有一般性的语义。 => is in use all over the place inony expression:

scala> List(1,2,3,4).map(current => current+1)
res5: List[Int] = List(2, 3, 4, 5)

每一要素的列出现有职能加一个职能的清单要素

List(1,2,3,4).map(c => c%2 match {
     | case 0 => "even"
     | case 1 => "odd"
     | }
     | )
res6: List[java.lang.String] = List(odd, even, odd, even)

Map当期元件,配制方式:

方法

def tweener(method: () => Unit) {
  method()
}

该方法称为tweener,该参数被任意命名为method,而> /code>的类型为 => Unit, 这是一个功能类别,你可以从=>上报。

<代码>Unit是一种类似于的回程类型,在 Java,不值得注意。 例如,<代码>印有的返回类型是Unit

简言之,(>>也可用作Unit,称作unit Value,唯一价值是Unit。 但not在功能类型中的含义是什么? => Unit,就像你有职能类别42 => Unit

重在您的榜样:tweener < > > > <> > > > >> > > > > > > ; :method, 但其部分应用由汇编者将其转化为功能价值。 你们可以将方法转化为职能,如:

scala> def m = println("hi")
m: Unit

scala> m _
res17: () => Unit = <function0>

OnList 可变成正确的职能类别,因为其参数清单是空洞的<条码>(<>>>,其返回类型是暗含的<条码>。

作为副注,如果在无空参数清单的情况下界定了<代码>OnList(根据法律,如果返回类型不是单位,则更为常见),则需要人工部分适用,否则其价值将转嫁给:

def f1() {}
def f2 {}
def g(f: () => Unit) {}
g(f1)   // OK
g(f2)   // error, since we re passing f2 s result (), 
        //   rather than partial function () => Unit
g(f2 _) // OK




相关问题
How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

To use or not to use Scala for new Java projects? [closed]

I m impressed with Twitter and investigating to use Scala for a new large scale web project with Hibernate and Wicket. What do you think about Scala, and should I use it instead of Java? EDIT: And, ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a ...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail ...

热门标签