MediumPro challengePython

Method Chaining — Fluent Interface

PythonClean CodePatternsFluent Interface

Method chaining creates readable, declarative code. Each method returns self to allow chaining.

# dirty — verbose, repetitive reassignment
def solve(items):
    result = items
    result = [x for x in result if x > 0]
    result = [x * 2 for x in result]
    result = result[:3]
    return result

Implement a `Pipeline` class (via create_pipeline(items)) with filter(fn), map(fn), take(n) and value() methods — all chainable (each returns self except value).

create_pipeline([1,-2,3,4,5]).filter(lambda x: x>0).map(lambda x: x*2).take(2).value()[2, 6]

Sample tests

Test #1filter positives → double → take 3
Input: [[1,-2,3,4,5]]
Output: [2,6,8]
Test #2All negative → empty after filter
Input: [[-1,-2,-3]]
Output: []
Test #3All positive → double all → take 3
Input: [[10,20,30,40]]
Output: [20,40,60]