How to make this method generic in Scala -
this simple function given index, item in array last
e.g. given val arr = array(1,2,3)
, val = 0
; return 3
def findindexfromlast(arr: array[relationship], i: int): relationship = { val cur = val size = arr.length arr(size - - 1) }
i want make generic , able accept array of type
if make 1st argument seq()
, take many different types of collections input.
def fromend[t](coll: seq[t], index: int):t = coll.reverse(index) fromend(array(1,3,5,7,9), 0) // res0: int = 9 fromend(list('x','y','q','b'), 3) // res1: char = x fromend(vector(2.2, 5.4, 7.7), 2) // res2: double = 2.2 fromend(stream(true, true, false, true), 1) // res3: boolean = false
Comments
Post a Comment