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!
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.
Function Declaration & Definitiondef keyword: “def” keyword is used to declare 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With