Skip to main content
Copy edited.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

scala anonymous Anonymous Scala function syntax

I'm learning more about Scala, and I'm having a little trouble understanding the example of anonymous functions herein http://www.scala-lang.org/node/135. I've copied the entire code block below:

object CurryTest extends Application {
    def filter(xs: List[Int], p: Int => Boolean): List[Int] =
        if (xs.isEmpty) xs
        else if (p(xs.head)) xs.head :: filter(xs.tail, p)
        else filter(xs.tail, p)

    def modN(n: Int)(x: Int) = ((x % n) == 0)

    val nums = List(1, 2, 3, 4, 5, 6, 7, 8)
    println(filter(nums, modN(2)))
    println(filter(nums, modN(3)))
}

I'm confused with the application of the modN function

def modN(n: Int)(x: Int) = ((x % n) == 0)

In the example, it's called with 1one argument

modN(2) and modN(3)

Can someone clarify whatWhat does the syntax of modN(n: Int)(x: Int) meansmean? 

Since it's called with 1one argument, I'm assuming they're not both arguments, but I can't really figure out how the values from nums get used by the mod function.

thanks, Jeff

scala anonymous function syntax

I'm learning more about Scala and I'm having a little trouble understanding the example of anonymous functions here http://www.scala-lang.org/node/135. I've copied the entire code block below:

object CurryTest extends Application {
  def filter(xs: List[Int], p: Int => Boolean): List[Int] =
    if (xs.isEmpty) xs
    else if (p(xs.head)) xs.head :: filter(xs.tail, p)
    else filter(xs.tail, p)

def modN(n: Int)(x: Int) = ((x % n) == 0)

val nums = List(1, 2, 3, 4, 5, 6, 7, 8)
println(filter(nums, modN(2)))
println(filter(nums, modN(3)))
}

I'm confused with the application of the modN function

def modN(n: Int)(x: Int) = ((x % n) == 0)

In the example, it's called with 1 argument

modN(2) and modN(3)

Can someone clarify what the syntax of modN(n: Int)(x: Int) means? Since it's called with 1 argument, I'm assuming they're not both arguments, but I can't really figure out how the values from nums get used by the mod function.

thanks, Jeff

Anonymous Scala function syntax

I'm learning more about Scala, and I'm having a little trouble understanding the example of anonymous functions in http://www.scala-lang.org/node/135. I've copied the entire code block below:

object CurryTest extends Application {
    def filter(xs: List[Int], p: Int => Boolean): List[Int] =
        if (xs.isEmpty) xs
        else if (p(xs.head)) xs.head :: filter(xs.tail, p)
        else filter(xs.tail, p)

    def modN(n: Int)(x: Int) = ((x % n) == 0)

    val nums = List(1, 2, 3, 4, 5, 6, 7, 8)
    println(filter(nums, modN(2)))
    println(filter(nums, modN(3)))
}

I'm confused with the application of the modN function

def modN(n: Int)(x: Int) = ((x % n) == 0)

In the example, it's called with one argument

modN(2) and modN(3)

What does the syntax of modN(n: Int)(x: Int) mean? 

Since it's called with one argument, I'm assuming they're not both arguments, but I can't really figure out how the values from nums get used by the mod function.

source formatting
Source Link
Daniel C. Sobral
  • 297.7k
  • 88
  • 508
  • 688

I'm learning more about Scala and I'm having a little trouble understanding the example of anonymous functions here http://www.scala-lang.org/node/135. I've copied the entire code block below:

object CurryTest extends Application {
  def filter(xs: List[Int], p: Int => Boolean): List[Int] =
    if (xs.isEmpty) xs
    else if (p(xs.head)) xs.head :: filter(xs.tail, p)
    else filter(xs.tail, p)

def modN(n: Int)(x: Int) = ((x % n) == 0)

val nums = List(1, 2, 3, 4, 5, 6, 7, 8)
println(filter(nums, modN(2)))
println(filter(nums, modN(3)))
}

I'm confused with the application of the modN function

def modN(n: Int)(x: Int) = ((x % n) == 0)

In the example, it's called with 1 argument

modN(2) and modN(3)

Can someone clarify what the syntax of modN(n: Int)(x: Int) means? Since it's called with 1 argument, I'm assuming they're not both arguments, but I can't really figure out how the values from nums get used by the mod function.

thanks, Jeff

I'm learning more about Scala and I'm having a little trouble understanding the example of anonymous functions here http://www.scala-lang.org/node/135. I've copied the entire code block below:

object CurryTest extends Application {
  def filter(xs: List[Int], p: Int => Boolean): List[Int] =
  if (xs.isEmpty) xs
  else if (p(xs.head)) xs.head :: filter(xs.tail, p)
  else filter(xs.tail, p)

def modN(n: Int)(x: Int) = ((x % n) == 0)

val nums = List(1, 2, 3, 4, 5, 6, 7, 8)
println(filter(nums, modN(2)))
println(filter(nums, modN(3)))
}

I'm confused with the application of the modN function

def modN(n: Int)(x: Int) = ((x % n) == 0)

In the example, it's called with 1 argument

modN(2) and modN(3)

Can someone clarify what the syntax of modN(n: Int)(x: Int) means? Since it's called with 1 argument, I'm assuming they're not both arguments, but I can't really figure out how the values from nums get used by the mod function.

thanks, Jeff

I'm learning more about Scala and I'm having a little trouble understanding the example of anonymous functions here http://www.scala-lang.org/node/135. I've copied the entire code block below:

object CurryTest extends Application {
  def filter(xs: List[Int], p: Int => Boolean): List[Int] =
    if (xs.isEmpty) xs
    else if (p(xs.head)) xs.head :: filter(xs.tail, p)
    else filter(xs.tail, p)

def modN(n: Int)(x: Int) = ((x % n) == 0)

val nums = List(1, 2, 3, 4, 5, 6, 7, 8)
println(filter(nums, modN(2)))
println(filter(nums, modN(3)))
}

I'm confused with the application of the modN function

def modN(n: Int)(x: Int) = ((x % n) == 0)

In the example, it's called with 1 argument

modN(2) and modN(3)

Can someone clarify what the syntax of modN(n: Int)(x: Int) means? Since it's called with 1 argument, I'm assuming they're not both arguments, but I can't really figure out how the values from nums get used by the mod function.

thanks, Jeff

Source Link
Jeff Storey
  • 57.4k
  • 75
  • 245
  • 414

scala anonymous function syntax

I'm learning more about Scala and I'm having a little trouble understanding the example of anonymous functions here http://www.scala-lang.org/node/135. I've copied the entire code block below:

object CurryTest extends Application {
  def filter(xs: List[Int], p: Int => Boolean): List[Int] =
  if (xs.isEmpty) xs
  else if (p(xs.head)) xs.head :: filter(xs.tail, p)
  else filter(xs.tail, p)

def modN(n: Int)(x: Int) = ((x % n) == 0)

val nums = List(1, 2, 3, 4, 5, 6, 7, 8)
println(filter(nums, modN(2)))
println(filter(nums, modN(3)))
}

I'm confused with the application of the modN function

def modN(n: Int)(x: Int) = ((x % n) == 0)

In the example, it's called with 1 argument

modN(2) and modN(3)

Can someone clarify what the syntax of modN(n: Int)(x: Int) means? Since it's called with 1 argument, I'm assuming they're not both arguments, but I can't really figure out how the values from nums get used by the mod function.

thanks, Jeff