Skip to content

Instantly share code, notes, and snippets.

@theteachr
Last active September 4, 2022 22:24
Show Gist options
  • Select an option

  • Save theteachr/b3bbe6b180b6a6af19feb5b7988d29c6 to your computer and use it in GitHub Desktop.

Select an option

Save theteachr/b3bbe6b180b6a6af19feb5b7988d29c6 to your computer and use it in GitHub Desktop.

Revisions

  1. theteachr revised this gist Sep 4, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions bitshift.py
    Original file line number Diff line number Diff line change
    @@ -54,8 +54,8 @@ def main():
    print(square << 2) # 4
    print(square . inc << 2) # 9
    print(inc . square << 2) # 5
    print(inc.inc.square.inc << 2) # 11
    print((inc.inc.square.inc * 2) << 2) # 146
    print(inc . inc . square . inc << 2) # 11
    print((inc . inc . square . inc * 2) << 2) # 146
    print((inc * 4) << 2) # 6
    print(2 >> inc) # 3
    print([8, 2] >> (curried_map << square))
  2. theteachr revised this gist Sep 4, 2022. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions bitshift.py
    Original file line number Diff line number Diff line change
    @@ -4,10 +4,10 @@ def __init__(self, f):

    def __lshift__(self, rhs):
    return self.f(rhs)

    def __rrshift__(self, lhs):
    return self.f(lhs)

    def __getattr__(self, f):
    @BitShiftFunc
    def composed(x):
    @@ -52,8 +52,8 @@ def main():
    print(BitShiftFunc(len) << 'Hello') # 5
    print(square(2)) # 4
    print(square << 2) # 4
    print(square.inc << 2) # 9
    print(inc.square << 2) # 5
    print(square . inc << 2) # 9
    print(inc . square << 2) # 5
    print(inc.inc.square.inc << 2) # 11
    print((inc.inc.square.inc * 2) << 2) # 146
    print((inc * 4) << 2) # 6
  3. theteachr revised this gist Sep 4, 2022. 1 changed file with 6 additions and 10 deletions.
    16 changes: 6 additions & 10 deletions bitshift.py
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,13 @@
    class Arg:
    def __init__(self, val) -> None:
    self.val = val

    def __rshift__(self, f):
    return f(self.val)

    class BitShiftFunc:
    def __init__(self, f):
    self.f = f

    def __lshift__(self, rhs):
    return self.f(rhs)

    def __rrshift__(self, lhs):
    return self.f(lhs)

    def __getattr__(self, f):
    @BitShiftFunc
    def composed(x):
    @@ -48,7 +44,7 @@ def cube(x):

    @BitShiftFunc
    def curried_map(f):
    return lambda lst: list(map(f, lst))
    return BitShiftFunc(lambda lst: list(map(f, lst)))


    def main():
    @@ -61,8 +57,8 @@ def main():
    print(inc.inc.square.inc << 2) # 11
    print((inc.inc.square.inc * 2) << 2) # 146
    print((inc * 4) << 2) # 6
    print(Arg(2) >> inc) # 3
    print(Arg([1, 2]) >> (curried_map << square))
    print(2 >> inc) # 3
    print([8, 2] >> (curried_map << square))


    if __name__ == '__main__':
  4. theteachr revised this gist Sep 4, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bitshift.py
    Original file line number Diff line number Diff line change
    @@ -61,7 +61,7 @@ def main():
    print(inc.inc.square.inc << 2) # 11
    print((inc.inc.square.inc * 2) << 2) # 146
    print((inc * 4) << 2) # 6
    print(Arg(2) >> inc) # 6
    print(Arg(2) >> inc) # 3
    print(Arg([1, 2]) >> (curried_map << square))


  5. theteachr revised this gist Sep 4, 2022. No changes.
  6. theteachr revised this gist Sep 4, 2022. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions bitshift.py
    Original file line number Diff line number Diff line change
    @@ -12,9 +12,6 @@ def __init__(self, f):
    def __lshift__(self, rhs):
    return self.f(rhs)

    def __rshift__(self, rhs):
    return self.f(rhs)

    def __getattr__(self, f):
    @BitShiftFunc
    def composed(x):
    @@ -69,4 +66,4 @@ def main():


    if __name__ == '__main__':
    main()
    main()
  7. theteachr revised this gist Sep 4, 2022. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions bitshift.py
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,20 @@
    class Arg:
    def __init__(self, val) -> None:
    self.val = val

    def __rshift__(self, f):
    return f(self.val)

    class BitShiftFunc:
    def __init__(self, f):
    self.f = f

    def __lshift__(self, rhs):
    return self.f(rhs)

    def __rshift__(self, rhs):
    return self.f(rhs)

    def __getattr__(self, f):
    @BitShiftFunc
    def composed(x):
    @@ -39,6 +49,11 @@ def cube(x):
    return x ** 3


    @BitShiftFunc
    def curried_map(f):
    return lambda lst: list(map(f, lst))


    def main():
    print(BitShiftFunc(cube) << 3) # 27
    print(BitShiftFunc(len) << 'Hello') # 5
    @@ -49,6 +64,8 @@ def main():
    print(inc.inc.square.inc << 2) # 11
    print((inc.inc.square.inc * 2) << 2) # 146
    print((inc * 4) << 2) # 6
    print(Arg(2) >> inc) # 6
    print(Arg([1, 2]) >> (curried_map << square))


    if __name__ == '__main__':
  8. theteachr created this gist Sep 4, 2022.
    55 changes: 55 additions & 0 deletions bitshift.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    class BitShiftFunc:
    def __init__(self, f):
    self.f = f

    def __lshift__(self, rhs):
    return self.f(rhs)

    def __getattr__(self, f):
    @BitShiftFunc
    def composed(x):
    return self.f(globals()[f](x))

    return composed

    def __mul__(self, times):
    @BitShiftFunc
    def runner(x):
    start = x
    for _ in range(times):
    start = self.f(start)
    return start

    return runner

    def __call__(self, *args, **kwds):
    return self.f(*args, **kwds)

    @BitShiftFunc
    def square(x):
    return x * x


    @BitShiftFunc
    def inc(x):
    return x + 1


    def cube(x):
    return x ** 3


    def main():
    print(BitShiftFunc(cube) << 3) # 27
    print(BitShiftFunc(len) << 'Hello') # 5
    print(square(2)) # 4
    print(square << 2) # 4
    print(square.inc << 2) # 9
    print(inc.square << 2) # 5
    print(inc.inc.square.inc << 2) # 11
    print((inc.inc.square.inc * 2) << 2) # 146
    print((inc * 4) << 2) # 6


    if __name__ == '__main__':
    main()