Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the function type in scala within defined in type meaningfully?

Tags:

scala

I am new and naive to scala. Just know how to define a function type such as Set here(only as an example).

type Set = Int => Boolean 

def set(i: Int): Set = n => n == i 
def contains(s: Set, i: Int) = s(i)

I also read the wiki of language-agnostic function type. It seems C#,C,Haskel also have the similiar grammer. http://en.wikipedia.org/wiki/Function_type.

My question is in which case you prefer to define one of this kind of abstract type function and use it, And no other choice to reach the same target? Comparing to directly define a concrete method using def

Or I can loose the requirement, to say using this function type, I can make the code looks much better. So that I can know more about function type.

Here my main interested part is type Set = Int => Boolean ,when you want to abstract it out? I am looking for real life use case, and how to implement it in concrete method in scala grammer. For example, this one is a bit complex.

type Set2 = (Int,Int,String) => (Boolean  => Int) => (Boolean  => Int).

I know it's called higher-kinded types. The grammer itself is indeed meaningful. But I just need more plain real life examples to scala beginners.

I found this answer describing for it. What is a higher kinded type in Scala?

But it still looks a bit obscure for me. I prefer plain answer for beginner. It seems like the function itself didn't require anything except the parameter and result type to the implementation mentod. For example, if the result (Boolean) doesn't come from parameter (Int) ,it still compiles.

def set(i: Int): Set1 = aa => new Date().getDate() == i 

Am I unstanding it right?

Let me know why this question is not clear or bad, so I can improve it,Sir!

like image 530
Clark Bao Avatar asked Sep 11 '11 06:09

Clark Bao


People also ask

How do you use function as a variable in Scala and what is the usage?

Use def to define a method, val to create a function. When assigning a function to a variable, a function literal is the code on the right side of the expression. A function value is an object, and extends the FunctionN traits in the main scala package, such as Function0 for a function that takes no parameters.

Which keyword are we using to define a function in Scala?

Function Declaration & Definitiondef keyword: “def” keyword is used to declare a function in Scala.

What is the type of a function in Scala?

Scala functions are first-class values. Scala functions are first-class values. You must mention the return type of parameters while defining the function and the return type of a function is optional. If you don't specify the return type of a function, the default return type is Unit.


2 Answers

The keyword type in Scala creates an alias for a given type. For example:

scala> type Str = String
defined type alias Str

scala> val greeting: Str = "Hello World!"
greeting: Str = Hello World!

This is very similar to what you did:

scala> type Set = Int => Boolean
defined type alias Set

scala> val isEven: Set = _ % 2 == 0
isEven: Int => Boolean = <function1>

scala> println(isEven(4))
true

scala> println(isEven(5))
false

Although type aliases may sometimes be useful for clarification purposes, documentation is not their primary use case. Scala's type system is very sophisticated. For instance there is an alternative to generics, namely abstract types. Consider this:

// Generics
abstract class GenericAbstraction[TypeArgument]
class GenericConcrete extends GenericAbstraction[String]

// Abstract types
abstract class TypeAbstraction {
   type TypeArgument
}
class TypeConcrete extends TypeAbstraction {
   type TypeArgument = String
}

These code samples basically accomplish the same thing, but there are situations where you need abstract types, but can't (or shouldn't) use generics. You can find more information here.

like image 140
agilesteel Avatar answered Sep 29 '22 12:09

agilesteel


You can define a function literal as follows:

val incrementor = (x: Int) => x + 1

or if you have some context that can be used by Scala's type inference, you can use reduced forms like:

val listOfInt = List(1, 2, 3, 4, 5)

listOfInt map {x => x + 1}
listOfInt map {_ + 1}

or even

listOfInt map {1 +}

These literals all imply the type themselves or have their type constrained by the expected type of a higher order function they are being passed to.

There have been several questions on SO about the difference between functions and methods which would be good background reading but perhaps taking a look at the free version of Martin Odersky's book Programming in Scala (Version 1) would be a much better starting point to read up about functions and methods.

like image 22
Don Mackenzie Avatar answered Sep 29 '22 12:09

Don Mackenzie