所有人:我是scala的新手。我不太理解"functional programming in scala“一书中关于curry的一个例子。为什么像a,b这样没有变量的函数可以顺利编译和运行?
def curry[A,B,C](f: (A, B) => C): A => (B => C) =
a => b => f(a, b)
我对scala非常陌生,而且还处于学习的初期。我读了一篇有这样一个例子的文章:
def example(_list: List[Positions], function: Position => Option[Path]): Option[Path] = _list match {...}
NB
职位是(Int,Int)
路径是一个List( Position )
据我所知,这种方法将适用于:
list of positions
Option[Path]
并将返回Option[Path]
我不明白的是我们该怎么称呼这个方法?
我试过这个:
example(
package wrap;
public class Anonymous {
public Circle getCircle(int radius){
return new Circle(radius){
private int rad;
public double area() {
return 3.14*rad*rad;
}
};
}
public static void main(String[] args) {
我已经定义了方法double。
def double(i: Int): Int = i * 2
val p = double {
print("Hello!");
5
}
print(p)
正如你所看到的,我们不仅可以传递整数参数,我们还可以调用一些逻辑(print)。为什么我可以这样做呢?那么Scala是如何解决这个问题的呢?在Scala中{}到底是什么意思?
考虑到以下代码:
abstract class Foobar {
type Parent <: Foobar
def parent: Option[Parent]
}
class Foo extends Foobar {
type Parent = Nothing
val parent = None
// ***
// Taking the explicit declaration of the "bar" method's return type
// "Bar" away convinces Scala
我偶然注意到scala可以推断出某个方法的参数类型。但我不明白确切的规则。谁能给我解释一下为什么test1方法可以工作,为什么test2方法不能工作
object Test {
def test1[A]: A => A = a => a
def test2[A]: A = a
}
我找不到一个好的题目来回答我的问题,因为我不明白这两行是怎么回事。你有什么想法吗?
由于Dart文档 describe it,因为这是纯OOP语言,Functions也是object。
在JS中可以这样做:
function functionReturningFunctionJS() {
return function functionReturnedByFunctionJS() {
return "This is function returned by function";
}
}
但是我不能从像这样的函数中返回函数:
Function functionReturningFunctionDart() {
return functi
我试图编写一个返回复杂类型的UDF:
private val toPrice = UDF1<String, Map<String, String>> { s ->
val elements = s.split(" ")
mapOf("value" to elements[0], "currency" to elements[1])
}
val type = DataTypes.createStructType(listOf(
DataTypes.createStructField(
我开始深入研究德尔福(D11) PPL,并编写了这个小示例:
procedure TForm2.LaunchTasks;
const
cmax = 5;
Var
ltask: ITask;
i,j: Integer;
begin
for i := 1 to cmax do
begin
j := i;
ltask := TTask.Create(
procedure ()
begin
Sleep(3000);
SendDebugFmt('Task #%d' ,[j])
end)
以下内容之间的“真正”区别是什么?
val b = ( x:Double) => x * 3
var b = ( x:Double) => x * 3
从技术上讲,一旦为val赋值,就不应该更改它。但是,作为第一条语句的一部分,可以通过传递不同的x值将b的值更改为不同的值。
scala> val b = ( x:Double) => x * 3
b: Double => Double = $$Lambda$1109/411756754@7a522157
scala> b(3)
这个例子是从Scala开始的,但是对我来说解释得还不够好。
val f: Int => String = x => "Dude: "+x
我真的有两个问题:
1)第一个示例与下面的代码相同:
val f = (x:Int) => "Dude: "+x
2)如果是这样的话,有人可以详细说明一下,并分析一下第一个例子。String =x部分让我大吃一惊。我不知道该怎么读这份声明
在下面的示例中,为什么这些方法有不同的类型,它们的类型有何不同?
Int => Int //is a shortcut for
Function1[Int, Int].
但是为什么一个方法有一个不同的类型,(i: Int): Int和Int => Int有什么区别呢?函数是否具有类似于第一个示例中的类似类型?
scala> :t def double(i: Int): Int = i * 2
(i: Int): Int
scala> :t def double: Int => Int = i => i * 2
Int => Int
scala&