Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a scala identity function?

If I have something like a List[Option[A]] and I want to convert this into a List[A], the standard way is to use flatMap:

scala> val l = List(Some("Hello"), None, Some("World")) l: List[Option[java.lang.String]] = List(Some(Hello), None, Some(World))  scala> l.flatMap( o => o) res0: List[java.lang.String] = List(Hello, World) 

Now o => o is just an identity function. I would have thought there'd be some way to do:

l.flatMap(Identity) //return a List[String] 

However, I can't get this to work as you can't generify an object. I tried a few things to no avail; has anyone got something like this to work?

like image 441
oxbow_lakes Avatar asked Nov 25 '09 15:11

oxbow_lakes


People also ask

What is identity function in scala?

The identity function's type parameter is A , which like the value parameter a is simply a unique identifier. It is used to define the type of the value parameter a and the return type of the function.

What is the use of identity function?

The identity function is a function which returns the same value, which was used as its argument. It is also called an identity relation or identity map or identity transformation. If f is a function, then identity relation for argument x is represented as f(x) = x, for all values of x.

What does => mean in scala?

=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .

What is the syntax of defining method in scala?

Syntax. def functionName ([list of parameters]) : [return type] = { function body return [expr] } Here, return type could be any valid Scala data type and list of parameters will be a list of variables separated by comma and list of parameters and return type are optional.


2 Answers

There's an identity function in Predef.

l flatMap identity[Option[String]]  > List[String] = List(Hello, World) 

A for expresion is nicer, I suppose:

for(x <- l; y <- x) yield y 

Edit:

I tried to figure out why the the type parameter (Option[String]) is needed. The problem seems to be the type conversion from Option[T] to Iterable[T].

If you define the identity function as:

l.flatMap( x => Option.option2Iterable(identity(x))) 

the type parameter can be omitted.

like image 141
Thomas Jung Avatar answered Oct 26 '22 23:10

Thomas Jung


FWIW, on Scala 2.8 you just call flatten on it. Thomas has it mostly covered for Scala 2.7. He only missed one alternative way of using that identity:

l.flatMap[String](identity) 

It won't work with operator notation, however (it seems operator notation does not accept type parameters, which is good to know).

You can also call flatten on Scala 2.7 (on a List, at least), but it won't be able to do anything without a type. However, this works:

l.flatten[String] 
like image 25
Daniel C. Sobral Avatar answered Oct 27 '22 00:10

Daniel C. Sobral