Skip to content

Commit

Permalink
add /ScalaBasic/mapOpDemo_3.scala
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 6, 2021
1 parent 803d10a commit b281b2f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
- map (op)
- [mapOPDemo_1](src/main/scala/ScalaBasic/mapOpDemo_1.scala)
- [mapOpDemo_2](src/main/scala/ScalaBasic/mapOpDemo_2.scala)
- [mapOpDemo_3](src/main/scala/ScalaBasic/mapOpDemo_3.scala)
- FlatMap
- flatMap will go to `every sub group` in a group and map the operation on each of them then return the result
- [FlatMapDemo_1](./src/main/scala/ScalaBasic/FlatMapDemo_1.scala)
Expand Down
28 changes: 28 additions & 0 deletions src/main/scala/ScalaBasic/mapOpDemo_3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ScalaBasic

/** mapOpDemo_3 : demo how to explicitly declare var type when do mapping*/


object mapOpDemo_3 extends App {

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

val x_1= x.map(x => x+1)
println(x_1)

println("================")

// *** NOTE : via `(x:Int)` syntax, we can define var type explicitly in mapping
val x_2= x.map( (x:Int) => x+1)
println(x_2)

println("================")

val y = List("a","b","c")
val y_1 = y.map(x => x + "-")
println(y_1)

// *** NOTE : via `(x:String)` syntax, we can define var type explicitly in mapping
val y_2 = y.map( (x:String) => x + "-")
println(y_2)
}

0 comments on commit b281b2f

Please sign in to comment.