         }
         return nil
     }
-    return parser
 }
 
 private func many<A>(_ parser: @escaping Parser<A>) -> Parser<[A]> {
-    let parser: Parser<[A]> = { stream in
+    return { stream in
         var result = [A]()
         var remainder = stream
         while let (element, newRemainder) = parser(remainder) {

private typealias Parser<A> = (Stream) -> (A, Stream)?

private func map<A, B>(_ parser: @escaping Parser<A>, _ transform: @escaping (A) -> B) -> Parser<B> {
    let newParser: Parser<B> = { stream in
        guard let (result, remainder) = parser(stream) else { return nil }
        return (transform(result), remainder)
    }
    return newParser
}

private func or<A>(_ leftParser: @escaping Parser<A>, _ rightParser: @escaping Parser<A>) -> Parser<A> {
    let parser: Parser<A> = { stream in
        return leftParser(stream) ?? rightParser(stream)
    }
    return parser
}

private func one<A>(of parsers: [Parser<A>]) -> Parser<A> {
    let parser: Parser<A> = { stream in
        for parser in parsers {
            if let x = parser(stream) {
                return x

        }
        return (result, remainder)
    }
}

private func many1<A>(_ parser: @escaping Parser<A>) -> Parser<[A]> {
    return { stream in
        guard let (element, remainder1) = parser(stream) else { return nil }
        if let (array, remainder2) = many(parser)(remainder1) {
            return ([element] + array, remainder2)
