0

I got two array of characters, looking like this:

a, b, c, d, e, f, g

k, e, y

and im need to associate each element from first array to elements from second, like this:

a->k,

b->e,

c->y,

d->k,

e->e,

f->y,

g->k

but dont know how to implement this in functional style. Any help will be appreciated!

1 Answer 1

7

You could zip the first Array with a continuous Stream :

scala> val a1 = Array('a,'b,'c,'d,'e,'f,'g)
a1: Array[Symbol] = Array('a, 'b, 'c, 'd, 'e, 'f, 'g)

scala> val a2 = Array('k,'e,'y)
a2: Array[Symbol] = Array('k, 'e, 'y)

scala> val a3 = a1 zip (Stream.continually(a2).flatten)
a3: Array[(Symbol, Symbol)] = Array(('a,'k), ('b,'e), ('c,'y), ('d,'k),
                                    ('e,'e), ('f,'y), ('g,'k))
Sign up to request clarification or add additional context in comments.

2 Comments

Or Map(a1.zip(Stream.continually(a2).flatten):_*) in case the OP wants a Map from elements of a1 to elements of a2. (It's not quite clear from the question.)
@JörgWMittag sorry for that. Il just want to get array, that look like values collection in that answer. Already solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.