utils_stream.py 3.11 KB
Newer Older
Leo Gao's avatar
Leo Gao committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import os
from functools import reduce
import operator
from tqdm import tqdm
import json


class ExitCodeError(Exception): pass


def sh(x):
    if os.system(x): raise ExitCodeError()

def ls(x):
    return [x + '/' + fn for fn in os.listdir(x)]

def lsr(x):
    if os.path.isdir(x):
        return reduce(operator.add, map(lsr, ls(x)), [])
    else:
        return [x]

def fwrite(fname, content):
    with open(fname, 'w') as fh:
        fh.write(content)

def fread(fname):
    with open(fname) as fh:
        return fh.read()

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

    def __rrshift__(self, other):
        return list(map(self.f, other))

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

    def __rrshift__(self, other):
        return list(filter(self.f, other))

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

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

class one:
    def __rrshift__(self, other):
        try:
            if isinstance(other, list): 
                assert len(other) == 1
                return other[0]
            return next(other)
        except:
            return None

class join:
    def __init__(self, sep):
        self.sep = sep

    def __rrshift__(self, other):
        if other is None: return
        try:
            return self.sep.join(other)
        except:
            return None


Y = object()

def id(x):
    return x

class Reflective:
    def __getattribute__(self, f):
        def _fn(*args, **kwargs):
            return lambda x: x.__getattribute__(f)(*args, **kwargs)
        return _fn
    
    def __getitem__(self, a):
        return lambda x: x[a]
    
    def __mul__(self, other):
        if other == Y:
            def _f(x, y=None):
                if y == None:
                    x, y = x
                
                return x * y
            return  _f
        
        return lambda x: x * other
    
    def __rmul__(self, other):
        if other == Y:
            def _f(x, y=None):
                if y == None:
                    x, y = x
                
                return y * x
            return  _f
        
        return lambda x: other * x
    
    def __add__(self, other):
        if other == Y:
            def _f(x, y=None):
                if y == None:
                    x, y = x
                
                return x + y
            return  _f
        
        return lambda x: x + other
    
    def __radd__(self, other):
        if other == Y:
            def _f(x, y=None):
                if y == None:
                    x, y = x
                
                return y + x
            return  _f
        
        return lambda x: other + x

# (b -> a -> b) -> b -> [a] -> b
def foldl(f, init, arr):
    curr = init
    for elem in arr:
        curr = f(curr, elem)
    return curr

# (a -> b -> b) -> b -> [a] -> b
def foldr(f, init, arr):
    curr = init
    for elem in arr[::-1]:
        curr = f(elem, curr)
    return curr


def comp(*fs):
    if len(fs) == 1:
        return fs[0]
    
    def _f(x):
        for f in fs[::-1]:
            x = f(x)
    
        return x
    return _f


X = Reflective()