| 239 | extend IEnumerable |
| 240 | |
| 241 | def join(sep as String) as String |
| 242 | """ |
| 243 | Join the items in an IEnumerable collection separated by a string. |
| 244 | """ |
| 245 | test |
| 246 | assert ['a', 'b'].join('.') == 'a.b' |
| 247 | assert ['a'].join('.') == 'a' |
| 248 | assert [1, 2].join(', ') == '1, 2' |
| 249 | assert [1, 2].join('') == '12' |
| 250 | assert [].join('.') == '' |
| 251 | |
| 252 | def join(sep as String, lastSep as String) as String |
| 253 | """ |
| 254 | Join the items in an IEnumerable collection with a separator string except for the last |
| 255 | two items; join them with the lastSep string |
| 256 | """ |
| 257 | test |
| 258 | assert ['a', 'b'].join(', ', ' and ') == 'a and b' |
| 259 | assert ['a', 'b', 'c'].join(', ', ' and ') == 'a, b and c' |
| 260 | assert ['a', 'b', 'c', 'd'].join(', ', ' and ') == 'a, b, c and d' |
| 261 | assert [1, 2, 3, 4].join(', ', ' and ') == '1, 2, 3 and 4' |
| 262 | assert [1].join('.', ':') == '1' |
| 263 | assert [1, 2].join('', '') == '12' |
| 264 | assert [].join('.', ':') == '' |
258 | | def get(flexibleIndex as int) as T |
259 | | require |
260 | | .count > 0 |
261 | | (flexibleIndex >= 0 and flexibleIndex < .count) _ |
262 | | or (flexibleIndex < 0 and flexibleIndex >= -.count) |
263 | | |
264 | | def get(flexibleIndex as int, default as T) as T |
265 | | ensure |
266 | | .count == 0 implies result == default |
267 | | (flexibleIndex > .count or flexibleIndex < -.count) _ |
268 | | implies result == default |
269 | | |
| 295 | def get(flexibleIndex as int) as T |
| 296 | """ |
| 297 | Return the item at flexibleIndex. If flexibleIndex < 0 return item at flexibleIndex+.count, |
| 298 | i.e. offset from end of list. |
| 299 | Therefore: |
| 300 | .get(0) returns first item in list |
| 301 | .get(-1) returns last item |
| 302 | .get(-2) returns second to last item |
| 303 | .get(-.count) returns first item |
| 304 | """ |
| 305 | require |
| 306 | .count > 0 |
| 307 | (flexibleIndex >= 0 and flexibleIndex < .count) _ |
| 308 | or (flexibleIndex < 0 and flexibleIndex >= -.count) |
| 309 | |
| 310 | def get(flexibleIndex as int, default as T) as T |
| 311 | """ |
| 312 | Return item at flexibleIndex. If flexibleIndex < 0 return item at flexibleIndex+.count. |
| 313 | Index values outside list index range return default. |
| 314 | """ |
| 315 | ensure |
| 316 | .count == 0 implies result == default |
| 317 | (flexibleIndex > .count or flexibleIndex < -.count) _ |
| 318 | implies result == default |
| 319 | |