Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
my_function(sum(x for x in xrange(100) if x % 3 == 1))[x for x in range(100)] | where(lambda x: x % 3 == 1) | sum | my_functionrange(100) | where(lambda x: x % 3 == 1) | add | my_functiontotal = sum(x for x in range(100) if x % 3 == 1)
result = my_function(total)f.compose(
f1,
f2,
f3(arg)
)
import pipe
class L(object):
def __init__(self, v):
self.v = v
def __or__(self, r):
return 'abra'
print L(range(5)) | pipe.as_list
[1, 2, 3] | set([3, 4])from pipe import as_list
a = {1, 2, 3}
b = {4, 5}
print a | b | as_list
wtf = compose([where(lambda x: x % 3 == 1),
sum,
my_function])
result = wtf(x for x in range(100))
from functools import partial
compose2 = lambda f, g: lambda *args, **kws: g(f(*args, **kws))
compose = partial(reduce, compose2)
where = partial(partial, filter) # %) ?
Post.objects | where(user_id = 1) & where(is_published = True) | order('-time_published') | by_page(1, 10)
pipeline = socket | read_request | (callback & errback) | send_response
server.register(pipeline)
server.run(80)
image | scale(640, 960, crop = True) | rotate(PI/2) | mirrow(...) | invert()
image.scale(640, 960, crop = True).rotate(PI/2).mirrow(...).invert()
from super.image.processor import i
image | i.scale(640, 960, crop = True) | i.rotate(PI/2) | i.mirrow(...) | i.invert()
my_function(sum(filter(lambda x: x % 3 == 1, [x for x in range(100)])))
my_function ((1..100) . to_a . keep_if {|x| x % 3 == 1 } . reduce(:+))
def fib
a, b = 0, 1
loop do
yield a
a, b = b, a + b
end
end
fib . take_while {|x| x < 10}
def fib
a, b = 0, 1
res = []
loop do
return res unless yield a
res << a
a, b = b, a + b
end
end
fib {|x| x < 10} # => [0, 1, 1, 2, 3, 5, 8]
class Fiber
def take_while
res = []
loop do
val = self.resume
return res unless yield val
res << val
end
end
end
def fib
Fiber.new do
a, b = 0, 1
loop do
Fiber.yield a
a, b = b, a + b
end
end
end
fib . take_while {|x| x < 10} # => [0, 1, 1, 2, 3, 5, 8]
class Generator < Decorator
def call(this, *args)
Fiber.new { loop { @method.bind(this).call(*args) { |x| Fiber.yield x } } }
end
end
class FibGen
extend MethodDecorators
Generator()
def fib
a, b = 0, 1
loop do
yield a
a, b = b, a + b
end
end
end
FibGen.new.fib . take_while {|x| x < 10} # => [0, 1, 1, 2, 3, 5, 8]
FibGen.new.fib . take(7) . map {|x| x * 2} . reduce(:+) # => 40
Пайпы, the pythonic way