functional programming - Creating a Map from Tuples in Scala -


i new function programming language , need 1 of problem.

i need create map between 2 tuple of sequences :

a = tuple1(tuple1(tuple1(x,y),z),a)

b = tuple2(tuple2(tuple2(1,2),3),4)

now need below :

c = ((x,1),(y,2),(z,3),(a,4)) , if search x need 1 ;

the number of tuple occurrences unknown both tuple structure similar. not understand , map solution similar kind of questions in stackoverflow. solution explanation helpful

i think (ugly) trick.

def tomap(x: (any,string), y: (any,int)): map[string, int] = {   @tailrec   def rec(x: (any,string), y: (any,int), map: map[string, int]): map[string, int] =     x match {       case (a: string, b) =>          val (c: int, d) = y         map ++ map(a -> c, b -> d)       case (a: (any,string), b) =>          val (c: (any,int), d) = y         rec(a, c, map ++ map(b -> d))     }   rec(x, y, map.empty[string, int]) } 

assuming want use this:

scala> val = ((("x","y"),"z"),"a") a: (((string, string), string), string) = (((x,y),z),a)  scala> val b = (((1,2),3),4) b: (((int, int), int), int) = (((1,2),3),4)  scala> tomap(a,b) res1: map[string,int] = map(a -> 4, z -> 3, x -> 1, y -> 2) 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -