from sequtils import anyIt template `?:`*(a, b: string): string = if len(a) > 0: a else: b template `?:`*(a: object, b: string): string = if a: $a else: b template `?:`*(a: ref, b): auto = if a != nil: a else: b template `?:`*(a, b: object): object = if a: a else: b template `?:`*(a, b): auto = if a: a else: b template `?.`*(a: ref, b: untyped): auto = if a != nil: a.b else: nil proc sameContents*[T](a1, a2: openArray[T]): bool = # Answers the question: do these two arrays contain the same contents, # regardless of their order? if a1.len != a2.len: return false for a in a1: if not a2.anyIt(a == it): return false return true template indexOf*(s, pred: untyped): int = var foundIdx = -1 var idx = 0 for it {.inject.} in items(s): if pred: foundIdx = idx break idx += 1 foundIdx template doWhile*(a, b: untyped): untyped = b while a: b