SlideShare una empresa de Scribd logo
1 de 8
Tupple-ware in Scala



             By VulcanMinds
Tupple - Concepts
Scala provides an interesting collection-like structure with 2 or more ‘Any’ type
elements called Tupples which extends a Product.

for e.g. ("Jane", "jane@jane.edu",24," Oregon, USA")
is a tupple.

scala> val y = ("Jane", "jane@jane.edu",24," Oregon, USA")
y: (java.lang.String, java.lang.String, Int, java.lang.String) = (Jane,jane@jane.edu,24," Oregon, USA")

The number of elements can be found by <tupple>.productArity
for e.g. number of elements in Jane tupple is.

scala> y.productArity
res7: Int = 4

scala> val u = (100,200)
u: (Int, Int) = (100,200)

scala> u.productArity
res6: Int = 2
Simple Scala hacks for Tupples
Accessing individual members......

scala> u._1
res142: Int = 100

scala> u._2
res143: Int = 200

Scala Hacks ......

scala> 1->2
res79: (Int, Int) = (1,2)
OR
scala> val c = 1->2
c: (Int, Int) = (1,2)

OR

scala> val u = "Manish" -> 29
u: (java.lang.String, Int) = (Manish,29)
Scala hacks for Tupples - continued
scala> 1->2->3
res80: ((Int, Int), Int) = ((1,2),3)
OR

scala> 1->2->3->4
res81: (((Int, Int), Int), Int) = (((1,2),3),4)

OR

scala> ("manish"->2) -> ("sandesh", 32)
res106: ((java.lang.String, Int), (java.lang.String, Int)) = ((manish,2),(sandesh,32))

OR

scala> val y = ("manish"->2) -> ("sandesh", 32) -> ("kat", 44)
y: (((java.lang.String, Int), (java.lang.String, Int)), (java.lang.String, Int)) = (((manish,2),(sandesh,32)),(kat,44))
Applying Functions to Tupples
A simple scala function with non-tupple parameter
                                                                          A simple scala function with a tupple
scala> def product(x:Int,y:Int) = {                                       parameter
   | x*y }
product: (x: Int, y: Int)Int                                              scala> def product2(x:(Int,Int)) = {
                                                                             | x._1 * x._2}
scala> val ff = product _                                                 product2: (x: (Int, Int))Int
ff: (Int, Int) => Int = <function2>
                                                                          scala> val s = (100,200)
scala> val h = ff(100,200)                                                s: (Int, Int) = (100,200)
h: Int = 20000
-----------------------------------------------------------------------   scala> val g = product2 _
Converting the function to a ‘tuppled’ type                               g: ((Int, Int)) => Int = <function1>

scala> val fft = ff.tupled                                                scala> g((100,200))
fft: ((Int, Int)) => Int = <function1>                                    res147: Int = 20000

scala> fft((100,200))
res146: Int = 20000

*Note the function parameter passed is of type tupple (Int,Int)
Returning Tupples with variable
                  number of elements
scala> def compute(x:Int) = {
   | x match {
   | case 1 => (1,2)
   | case 2 => (1,2,3)
   | case _ => ("Unknown Number?",0)
   |}
   |}
compute: (x: Int)Product with Serializable

scala> val q = compute _
q: Int => Product with Serializable = <function1>

scala> val e = q(1)
e: Product with Serializable = (1,2)

scala> val e = q(16)
e: Product with Serializable = (Unknown Number?,0)
Playing with Null types in Tupples

Playing with Nulls in tupples

scala> val x = (200, null)
x: (Int, Null) = (200,null)

scala> x match {
   | case (i:Int, v) => println("got tuple (%s: %s)".format(i, v))
   | case _ => println("catch all")
   |}
got tuple (200: null)
Processing values in tupples
processing values in tupples
// a list of people with their names and gender and ages
scala> val w = List(("manish","male", 22),("kat",
"female",32),("Jose","male",30),("Obama","male",35),("Ken","male",44),("Hanna","female",21))

w: List[(java.lang.String, java.lang.String, Int)] = List((manish,male,22), (kat,female,32), (Jose,male,30), (Obama,male,35),
(Ken,male,44), (Hanna,female,21))

scala> def countBoth(x:List[(String,String,Int)]) = {
   | var males = 0
   | var females = 0
   | for(person <- x) {
   | if(person._2 equals "male") males = males + 1
   | else females = females + 1
   |}
   | (males, females)
   |}
countBoth: (x: List[(String, String, Int)])(Int, Int)

scala> val q = countBoth _
q: List[(String, String, Int)] => (Int, Int) = <function1>


scala> val result = q(w)
result: (Int, Int) = (4,2)

Isn’t that concise?

Más contenido relacionado

La actualidad más candente

Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in SwiftSaugat Gautam
 
Arrays in python
Arrays in pythonArrays in python
Arrays in pythonLifna C.S
 
Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about lazinessJohan Tibell
 
4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data StructureMandeep Singh
 
A Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIOA Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIOJorge Vásquez
 
The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189Mahmoud Samir Fayed
 
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaExploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaJorge Vásquez
 
Python matplotlib cheat_sheet
Python matplotlib cheat_sheetPython matplotlib cheat_sheet
Python matplotlib cheat_sheetNishant Upadhyay
 
Monoids, monoids, monoids
Monoids, monoids, monoidsMonoids, monoids, monoids
Monoids, monoids, monoidsLuka Jacobowitz
 
Pandas pythonfordatascience
Pandas pythonfordatasciencePandas pythonfordatascience
Pandas pythonfordatascienceNishant Upadhyay
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class PatternsJohn De Goes
 
Python programming Part -6
Python programming Part -6Python programming Part -6
Python programming Part -6Megha V
 
Java Foundations: Maps, Lambda and Stream API
Java Foundations: Maps, Lambda and Stream APIJava Foundations: Maps, Lambda and Stream API
Java Foundations: Maps, Lambda and Stream APISvetlin Nakov
 
Advanced Tagless Final - Saying Farewell to Free
Advanced Tagless Final - Saying Farewell to FreeAdvanced Tagless Final - Saying Farewell to Free
Advanced Tagless Final - Saying Farewell to FreeLuka Jacobowitz
 
Data Structures in javaScript 2015
Data Structures in javaScript 2015Data Structures in javaScript 2015
Data Structures in javaScript 2015Nir Kaufman
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In ScalaKnoldus Inc.
 

La actualidad más candente (20)

Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in Swift
 
Queue
QueueQueue
Queue
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about laziness
 
4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data Structure
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
A Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIOA Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIO
 
The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189
 
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaExploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in Scala
 
Python matplotlib cheat_sheet
Python matplotlib cheat_sheetPython matplotlib cheat_sheet
Python matplotlib cheat_sheet
 
Monoids, monoids, monoids
Monoids, monoids, monoidsMonoids, monoids, monoids
Monoids, monoids, monoids
 
Pandas pythonfordatascience
Pandas pythonfordatasciencePandas pythonfordatascience
Pandas pythonfordatascience
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class Patterns
 
Python programming Part -6
Python programming Part -6Python programming Part -6
Python programming Part -6
 
Java Foundations: Maps, Lambda and Stream API
Java Foundations: Maps, Lambda and Stream APIJava Foundations: Maps, Lambda and Stream API
Java Foundations: Maps, Lambda and Stream API
 
Scala collections
Scala collectionsScala collections
Scala collections
 
Advanced Tagless Final - Saying Farewell to Free
Advanced Tagless Final - Saying Farewell to FreeAdvanced Tagless Final - Saying Farewell to Free
Advanced Tagless Final - Saying Farewell to Free
 
Data Structures in javaScript 2015
Data Structures in javaScript 2015Data Structures in javaScript 2015
Data Structures in javaScript 2015
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In Scala
 
Python array
Python arrayPython array
Python array
 

Destacado

Data choreography in mongo
Data choreography in mongoData choreography in mongo
Data choreography in mongoVulcanMinds
 
Dig up the gold in your godowns
Dig up the gold in your godownsDig up the gold in your godowns
Dig up the gold in your godownsVulcanMinds
 
Eindpresentatie Freek Kusters
Eindpresentatie Freek KustersEindpresentatie Freek Kusters
Eindpresentatie Freek Kustersfreekkusters
 
Mongo db 2.6_security_architecture
Mongo db 2.6_security_architectureMongo db 2.6_security_architecture
Mongo db 2.6_security_architectureMat Keep
 
Munching the mongo
Munching the mongoMunching the mongo
Munching the mongoVulcanMinds
 

Destacado (7)

92624748
9262474892624748
92624748
 
Proyecto de aula q
Proyecto de aula qProyecto de aula q
Proyecto de aula q
 
Data choreography in mongo
Data choreography in mongoData choreography in mongo
Data choreography in mongo
 
Dig up the gold in your godowns
Dig up the gold in your godownsDig up the gold in your godowns
Dig up the gold in your godowns
 
Eindpresentatie Freek Kusters
Eindpresentatie Freek KustersEindpresentatie Freek Kusters
Eindpresentatie Freek Kusters
 
Mongo db 2.6_security_architecture
Mongo db 2.6_security_architectureMongo db 2.6_security_architecture
Mongo db 2.6_security_architecture
 
Munching the mongo
Munching the mongoMunching the mongo
Munching the mongo
 

Similar a Tupple ware in scala

Swift 함수 커링 사용하기
Swift 함수 커링 사용하기Swift 함수 커링 사용하기
Swift 함수 커링 사용하기진성 오
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?Tomasz Wrobel
 
Monadologie
MonadologieMonadologie
Monadologieleague
 
A brief introduction to apply functions
A brief introduction to apply functionsA brief introduction to apply functions
A brief introduction to apply functionsNIKET CHAURASIA
 
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm ProblemsLeet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm ProblemsSunil Yadav
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kirill Rozov
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scalaehsoon
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patternsleague
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala Knoldus Inc.
 
Python Programming: Data Structure
Python Programming: Data StructurePython Programming: Data Structure
Python Programming: Data StructureChan Shik Lim
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meetMario Fusco
 
ハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うbpstudy
 

Similar a Tupple ware in scala (20)

An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Meet scala
Meet scalaMeet scala
Meet scala
 
Introducing scala
Introducing scalaIntroducing scala
Introducing scala
 
Scala Bootcamp 1
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1
 
Monadologie
MonadologieMonadologie
Monadologie
 
Scala Intro
Scala IntroScala Intro
Scala Intro
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 
Scala for curious
Scala for curiousScala for curious
Scala for curious
 
A brief introduction to apply functions
A brief introduction to apply functionsA brief introduction to apply functions
A brief introduction to apply functions
 
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
 
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm ProblemsLeet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scala
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patterns
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
 
Python Programming: Data Structure
Python Programming: Data StructurePython Programming: Data Structure
Python Programming: Data Structure
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
ハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使う
 

Tupple ware in scala

  • 1. Tupple-ware in Scala By VulcanMinds
  • 2. Tupple - Concepts Scala provides an interesting collection-like structure with 2 or more ‘Any’ type elements called Tupples which extends a Product. for e.g. ("Jane", "jane@jane.edu",24," Oregon, USA") is a tupple. scala> val y = ("Jane", "jane@jane.edu",24," Oregon, USA") y: (java.lang.String, java.lang.String, Int, java.lang.String) = (Jane,jane@jane.edu,24," Oregon, USA") The number of elements can be found by <tupple>.productArity for e.g. number of elements in Jane tupple is. scala> y.productArity res7: Int = 4 scala> val u = (100,200) u: (Int, Int) = (100,200) scala> u.productArity res6: Int = 2
  • 3. Simple Scala hacks for Tupples Accessing individual members...... scala> u._1 res142: Int = 100 scala> u._2 res143: Int = 200 Scala Hacks ...... scala> 1->2 res79: (Int, Int) = (1,2) OR scala> val c = 1->2 c: (Int, Int) = (1,2) OR scala> val u = "Manish" -> 29 u: (java.lang.String, Int) = (Manish,29)
  • 4. Scala hacks for Tupples - continued scala> 1->2->3 res80: ((Int, Int), Int) = ((1,2),3) OR scala> 1->2->3->4 res81: (((Int, Int), Int), Int) = (((1,2),3),4) OR scala> ("manish"->2) -> ("sandesh", 32) res106: ((java.lang.String, Int), (java.lang.String, Int)) = ((manish,2),(sandesh,32)) OR scala> val y = ("manish"->2) -> ("sandesh", 32) -> ("kat", 44) y: (((java.lang.String, Int), (java.lang.String, Int)), (java.lang.String, Int)) = (((manish,2),(sandesh,32)),(kat,44))
  • 5. Applying Functions to Tupples A simple scala function with non-tupple parameter A simple scala function with a tupple scala> def product(x:Int,y:Int) = { parameter | x*y } product: (x: Int, y: Int)Int scala> def product2(x:(Int,Int)) = { | x._1 * x._2} scala> val ff = product _ product2: (x: (Int, Int))Int ff: (Int, Int) => Int = <function2> scala> val s = (100,200) scala> val h = ff(100,200) s: (Int, Int) = (100,200) h: Int = 20000 ----------------------------------------------------------------------- scala> val g = product2 _ Converting the function to a ‘tuppled’ type g: ((Int, Int)) => Int = <function1> scala> val fft = ff.tupled scala> g((100,200)) fft: ((Int, Int)) => Int = <function1> res147: Int = 20000 scala> fft((100,200)) res146: Int = 20000 *Note the function parameter passed is of type tupple (Int,Int)
  • 6. Returning Tupples with variable number of elements scala> def compute(x:Int) = { | x match { | case 1 => (1,2) | case 2 => (1,2,3) | case _ => ("Unknown Number?",0) |} |} compute: (x: Int)Product with Serializable scala> val q = compute _ q: Int => Product with Serializable = <function1> scala> val e = q(1) e: Product with Serializable = (1,2) scala> val e = q(16) e: Product with Serializable = (Unknown Number?,0)
  • 7. Playing with Null types in Tupples Playing with Nulls in tupples scala> val x = (200, null) x: (Int, Null) = (200,null) scala> x match { | case (i:Int, v) => println("got tuple (%s: %s)".format(i, v)) | case _ => println("catch all") |} got tuple (200: null)
  • 8. Processing values in tupples processing values in tupples // a list of people with their names and gender and ages scala> val w = List(("manish","male", 22),("kat", "female",32),("Jose","male",30),("Obama","male",35),("Ken","male",44),("Hanna","female",21)) w: List[(java.lang.String, java.lang.String, Int)] = List((manish,male,22), (kat,female,32), (Jose,male,30), (Obama,male,35), (Ken,male,44), (Hanna,female,21)) scala> def countBoth(x:List[(String,String,Int)]) = { | var males = 0 | var females = 0 | for(person <- x) { | if(person._2 equals "male") males = males + 1 | else females = females + 1 |} | (males, females) |} countBoth: (x: List[(String, String, Int)])(Int, Int) scala> val q = countBoth _ q: List[(String, String, Int)] => (Int, Int) = <function1> scala> val result = q(w) result: (Int, Int) = (4,2) Isn’t that concise?