Transformation for Sequence.distinct_by :param func: distinct_by function :return: transformation
(func)
| 154 | |
| 155 | |
| 156 | def distinct_by_t(func): |
| 157 | """ |
| 158 | Transformation for Sequence.distinct_by |
| 159 | :param func: distinct_by function |
| 160 | :return: transformation |
| 161 | """ |
| 162 | |
| 163 | def distinct_by(sequence): |
| 164 | distinct_lookup = {} |
| 165 | for element in sequence: |
| 166 | key = func(element) |
| 167 | if key not in distinct_lookup: |
| 168 | distinct_lookup[key] = element |
| 169 | return distinct_lookup.values() |
| 170 | |
| 171 | return Transformation("distinct_by({0})".format(name(func)), distinct_by, None) |
| 172 | |
| 173 | |
| 174 | def sorted_t(key=None, reverse=False): |