def test(): fred = tvseq((N**num)[tvseq], [1, 2]) fred >> _v >> check >> equal >> [1, 2] fred >> check >> typeOf >> (N**num)[tvseq] fred = fred >> append >> 3 fred = fred >> join >> tvseq((N**num)[tvseq], [4, 5]) fred >> _v >> check >> equal >> [1, 2, 3, 4, 5] print(repr(fred)) print(str(fred))
def testPolymorhpic(): xs = tvseq((N**pyint)[tvseq], [1, 2, 3]) fxs = xs >> each_ >> polyAddOne fxs >> check >> typeOf >> (N**pyint)[tvseq] xs = tvseq((N ** pystr)[tvseq], ['One', 'Two', 'Three']) fxs = xs >> each_ >> polyAddOne fxs >> check >> typeOf >> (N ** pystr)[tvseq] xs = tvseq((N ** (pyint+pystr))[tvseq], [1, 'Two', 3]) fxs = xs >> each_ >> polyAddOne fxs >> check >> typeOf >> (N ** (pyint+pystr))[tvseq]
def take(xs:(N**T1)[tvseq], n:tCount, tByT) -> (N**T1)[tvseq]: if n == 0: return xs elif n > 0: return tvseq((N**tByT[T1])[tvseq], xs[:n]) elif n < 0: raise NotYetImplemented()
def each(xs:(N**T1)[tvseq], f:T1^T2, tByT) -> (N**T2)[tvseq]: # fxs = [] # for x in xs: # y = f(x) # fxs.append(y) # return fxs | (N**tByT[T2])[tvseq] return tvseq((N**tByT[T2])[tvseq], [f(x) for x in xs])
def testOverloaded(): d1, tByT1 = each_((N ** pyint)[tvseq], addOne2(pyint).d._t) d2, tByT2 = each_((N ** pyint)[tvseq], addOne2._t) assert d2.module == '__main__' or d2.module == 'bones.core.tests.test_polymorphic' xs = tvseq((N ** pyint)[tvseq], [1, 2, 3]) fxs = xs >> each_ >> addOne2 fxs >> check >> typeOf >> (N**pyint)[tvseq]
def joinAll(xs:pylist+pytuple) -> pystr + ((N**T1)[tvseq]) + pylist + pytuple: # could be a list of strings or a list of (N**T1) & tvseq # answer a string if no elements if not xs: return '' typeOfFirst = typeOf(xs[0]) if typeOfFirst >> fitsWithin >> pystr: return ''.join(xs) elif typeOfFirst >> fitsWithin >> (N**T1)[tvseq]: elements = [] for x in xs: # could check the type of each list using metatypes.fitsWithin elements.extend(x.data) return tvseq(xs[0]._t, elements) elif typeOfFirst >> fitsWithin >> pylist: answer = [] for e in xs: answer += e return answer elif typeOfFirst >> fitsWithin >> pytuple: answer = () for e in xs: answer += e return answer
def join(xs:(N**T1)[tvseq], ys:(N**T1)[tvseq], tByT) -> (N**T1)[tvseq]: return tvseq((N**(tByT[T1]))[tvseq], xs.data + ys.data)
def each_(xs:(N**T1)[tvseq], f:T1^T2, tByT) -> (N**T2)[tvseq]: fxs = [f(x) for x in xs] return tvseq((N**tByT[T2])[tvseq], fxs)
def testExample(): tvseq((N ** pyint)[tvseq], [1, 2, 3]) >> each_ >> polyAddOne >> PP >> check >> equal >> 'tvseq of 3 pyint' tvseq((N ** pystr)[tvseq], ['One', 'Two', 'Three']) >> each_ >> polyAddOne >> PP >> check >> equal >> 'tvseq of 3 pystr'
def append(xs:(N**T1)[tvseq], x:T1) -> (N**T1)[tvseq]: xs = tvseq(xs) xs.append(x) return xs
def drop(xs:(N**T1)[tvseq], n:tCount, tByT) -> (N**T1)[tvseq]: return tvseq((N**tByT[T1])[tvseq], xs[n:])
def drop(xs:(N**T1)[tvseq], element:T1, tByT) -> (N**T1)[tvseq]: answer = tvseq((N**tByT[T1])[tvseq]) for e in xs: if e is not element: answer.append(e) return answer
def drop(xs:(N**T1)[tvseq], indexes:pylist+pytuple, tByT) -> (N**T1)[tvseq]: answer = tvseq((N**tByT[T1])[tvseq]) for i, x in enumerate(xs._v): if i not in indexes: answer.append(x) return answer
def to(x: pylist + pytuple, t: (N**T1)[tvseq], tByT) -> (N**T1)[tvseq]: return tvseq((N**tByT[T1])[tvseq], x)