Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Remove the Kth element from a list.
- Return the list and the removed element in a Tuple. Elements are numbered from 0.
- Example:
- scala> removeAt(1, List('a, 'b, 'c, 'd))
- res0: (List[Symbol], Symbol) = (List('a, 'c, 'd),'b)
- */
- def removeAt[A](index: Int, list: List[A]): Tuple2[List[A], A] = (index, list) match {
- case (0, head :: tail) => (tail, head)
- case (_, head :: tail) => {
- val (acc, element) = removeAt(index - 1, tail)
- (head :: acc, element)
- }
- }
- /*
- Duplicate the elements of a list a given number of times.
- Example:
- scala> duplicateN(3, List('a, 'b, 'c, 'c, 'd))
- res0: List[Symbol] = List('a, 'a, 'a, 'b, 'b, 'b, 'c, 'c, 'c, 'c, 'c, 'c, 'd, 'd, 'd)
- */
- def duplicateN[A](times: Int, list: List[A]): List[A] = list.flatMap((e: A) => List.fill(times)(e))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement