1*bcb5dc79SHONG Yifan# Copyright 2017 The Bazel Authors. All rights reserved. 2*bcb5dc79SHONG Yifan# 3*bcb5dc79SHONG Yifan# Licensed under the Apache License, Version 2.0 (the "License"); 4*bcb5dc79SHONG Yifan# you may not use this file except in compliance with the License. 5*bcb5dc79SHONG Yifan# You may obtain a copy of the License at 6*bcb5dc79SHONG Yifan# 7*bcb5dc79SHONG Yifan# http://www.apache.org/licenses/LICENSE-2.0 8*bcb5dc79SHONG Yifan# 9*bcb5dc79SHONG Yifan# Unless required by applicable law or agreed to in writing, software 10*bcb5dc79SHONG Yifan# distributed under the License is distributed on an "AS IS" BASIS, 11*bcb5dc79SHONG Yifan# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*bcb5dc79SHONG Yifan# See the License for the specific language governing permissions and 13*bcb5dc79SHONG Yifan# limitations under the License. 14*bcb5dc79SHONG Yifan 15*bcb5dc79SHONG Yifan"""Skylib module containing functions that operate on collections.""" 16*bcb5dc79SHONG Yifan 17*bcb5dc79SHONG Yifandef _after_each(separator, iterable): 18*bcb5dc79SHONG Yifan """Inserts `separator` after each item in `iterable`. 19*bcb5dc79SHONG Yifan 20*bcb5dc79SHONG Yifan Args: 21*bcb5dc79SHONG Yifan separator: The value to insert after each item in `iterable`. 22*bcb5dc79SHONG Yifan iterable: The list into which to intersperse the separator. 23*bcb5dc79SHONG Yifan 24*bcb5dc79SHONG Yifan Returns: 25*bcb5dc79SHONG Yifan A new list with `separator` after each item in `iterable`. 26*bcb5dc79SHONG Yifan """ 27*bcb5dc79SHONG Yifan result = [] 28*bcb5dc79SHONG Yifan for x in iterable: 29*bcb5dc79SHONG Yifan result.append(x) 30*bcb5dc79SHONG Yifan result.append(separator) 31*bcb5dc79SHONG Yifan 32*bcb5dc79SHONG Yifan return result 33*bcb5dc79SHONG Yifan 34*bcb5dc79SHONG Yifandef _before_each(separator, iterable): 35*bcb5dc79SHONG Yifan """Inserts `separator` before each item in `iterable`. 36*bcb5dc79SHONG Yifan 37*bcb5dc79SHONG Yifan Args: 38*bcb5dc79SHONG Yifan separator: The value to insert before each item in `iterable`. 39*bcb5dc79SHONG Yifan iterable: The list into which to intersperse the separator. 40*bcb5dc79SHONG Yifan 41*bcb5dc79SHONG Yifan Returns: 42*bcb5dc79SHONG Yifan A new list with `separator` before each item in `iterable`. 43*bcb5dc79SHONG Yifan """ 44*bcb5dc79SHONG Yifan result = [] 45*bcb5dc79SHONG Yifan for x in iterable: 46*bcb5dc79SHONG Yifan result.append(separator) 47*bcb5dc79SHONG Yifan result.append(x) 48*bcb5dc79SHONG Yifan 49*bcb5dc79SHONG Yifan return result 50*bcb5dc79SHONG Yifan 51*bcb5dc79SHONG Yifandef _uniq(iterable): 52*bcb5dc79SHONG Yifan """Returns a list of unique elements in `iterable`. 53*bcb5dc79SHONG Yifan 54*bcb5dc79SHONG Yifan Requires all the elements to be hashable. 55*bcb5dc79SHONG Yifan 56*bcb5dc79SHONG Yifan Args: 57*bcb5dc79SHONG Yifan iterable: An iterable to filter. 58*bcb5dc79SHONG Yifan 59*bcb5dc79SHONG Yifan Returns: 60*bcb5dc79SHONG Yifan A new list with all unique elements from `iterable`. 61*bcb5dc79SHONG Yifan """ 62*bcb5dc79SHONG Yifan unique_elements = {element: None for element in iterable} 63*bcb5dc79SHONG Yifan 64*bcb5dc79SHONG Yifan # list() used here for python3 compatibility. 65*bcb5dc79SHONG Yifan # TODO(bazel-team): Remove when testing frameworks no longer require python compatibility. 66*bcb5dc79SHONG Yifan return list(unique_elements.keys()) 67*bcb5dc79SHONG Yifan 68*bcb5dc79SHONG Yifancollections = struct( 69*bcb5dc79SHONG Yifan after_each = _after_each, 70*bcb5dc79SHONG Yifan before_each = _before_each, 71*bcb5dc79SHONG Yifan uniq = _uniq, 72*bcb5dc79SHONG Yifan) 73