小编si1*_*i14的帖子

最先进的数据结构

您能对现代数据结构有什么看法?我们都知道经典的,比如树木,尝试,堆叠,列表,B树等等(我认为Cormen的书是一本非常好的"经典之书").但是最近的研究呢?我可以至少命名其中两个:手指树朱迪阵列.我想知道更多.

data-structures

30
推荐指数
2
解决办法
3179
查看次数

在Python中使用语法糖进行函数组合是一个好主意吗?

前段时间我查看了Haskell文档,发现它的功能组合运算符非常好.所以我实现了这个小装饰:

from functools import partial

class _compfunc(partial):
    def __lshift__(self, y):
        f = lambda *args, **kwargs: self.func(y(*args, **kwargs)) 
        return _compfunc(f)

    def __rshift__(self, y):
        f = lambda *args, **kwargs: y(self.func(*args, **kwargs)) 
        return _compfunc(f)

def composable(f):
    return _compfunc(f)

@composable    
def f1(x):
    return x * 2

@composable
def f2(x):
    return  x + 3

@composable
def f3(x):
    return (-1) * x

@composable
def f4(a):
    return a + [0]

print (f1 >> f2 >> f3)(3) #-9
print (f4 >> f1)([1, 2]) #[1, 2, 0, 1, 2, …
Run Code Online (Sandbox Code Playgroud)

python function-composition

18
推荐指数
3
解决办法
1457
查看次数

Python函数组合

我试着用很好的语法实现函数组合,这就是我所拥有的:

from functools import partial

class _compfunc(partial):
    def __lshift__(self, y):
        f = lambda *args, **kwargs: self.func(y(*args, **kwargs)) 
        return _compfunc(f)

    def __rshift__(self, y):
        f = lambda *args, **kwargs: y(self.func(*args, **kwargs)) 
        return _compfunc(f)

def composable(f):
    return _compfunc(f)

@composable    
def f1(x):
    return x * 2

@composable
def f2(x):
    return  x + 3

@composable
def f3(x):
    return (-1) * x

print f1(2) #4
print f2(2) #5
print (f1 << f2 << f1)(2) #14
print (f3 >> f2)(2) #1
print (f2 >> f3)(2) #-5
Run Code Online (Sandbox Code Playgroud)

它适用于整数,但在列表/元组上失败: …

python function-composition

9
推荐指数
1
解决办法
3894
查看次数