<commit_before>//
//  ApplicativeParser.swift
//  tibasic
//
//  Created by <NAME> on 7/22/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation


// Like Haskell Alternative <|>
public func <|><ParserA1:ParserType, ParserA2:ParserType, A where ParserA1.TokenType==A, ParserA2.TokenType==A>(lhs:ParserA1, rhs:ParserA2) -> Parser<A> {
    return Parser { input in
        let result = lhs.parse(input)
        switch result {
        case .None: return rhs.parse(input)
        case .Some(_): return result
        }
    }
}


// Like Haskell Applicative <*>
public func <*><ParserAB:ParserType, ParserA:ParserType, A, B where ParserAB.TokenType==A->B,
    ParserA.TokenType==A>(lhs:ParserAB, rhs:ParserA) -> Parser<B> {
        return apply(lhs, rhs)
}

// Haskell Applicative <*
public func <*<ParserA:ParserType, ParserB:ParserType, A, B where
    ParserA.TokenType==A, ParserB.TokenType==B>(lhs:ParserA, rhs:ParserB) -> Parser<A> {
        return liftA2(const)(lhs)(rhs)
}

// Haskell Applictive *>
public func *><ParserA:ParserType, ParserB:ParserType, A, B where
    ParserA.TokenType==A, ParserB.TokenType==B>(lhs:ParserA, rhs:ParserB) -> Parser<B> {
        return liftA2(const(id))(lhs)(rhs)
}

public func liftA2<ParserA:ParserType, ParserB:ParserType, A, B, C
    where ParserA.TokenType==A, ParserB.TokenType==B>(f:A -> B -> C)(_ a:ParserA)(_ b:ParserB) -> Parser<C> {
        return f <§> a <*> b<commit_msg>Add Application operator <*> for Optional<commit_after>//
//  ApplicativeParser.swift
//  tibasic
//
//  Created by <NAME> on 7/22/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation


// Like Haskell Alternative <|>
public func <|><ParserA1:ParserType, ParserA2:ParserType, A where ParserA1.TokenType==A, ParserA2.TokenType==A>(lhs:ParserA1, rhs:ParserA2) -> Parser<A> {
    return Parser { input in
        let result = lhs.parse(input)
        switch result {
        case .None: return rhs.parse(input)
        case .Some(_): return result
        }
    }
}


// Like Haskell Applicative <*>
public func <*><ParserAB:ParserType, ParserA:ParserType, A, B where ParserAB.TokenType==A->B,
    ParserA.TokenType==A>(lhs:ParserAB, rhs:ParserA) -> Parser<B> {
        return apply(lhs, rhs)
}

public func <*><A,B>(lhs:(A->B)?, rhs:A?) -> B? {
    return lhs.flatMap { rhs.map($0) }
}

// Haskell Applicative <*
public func <*<ParserA:ParserType, ParserB:ParserType, A, B where
    ParserA.TokenType==A, ParserB.TokenType==B>(lhs:ParserA, rhs:ParserB) -> Parser<A> {
        return liftA2(const)(lhs)(rhs)
}

// Haskell Applictive *>
public func *><ParserA:ParserType, ParserB:ParserType, A, B where
    ParserA.TokenType==A, ParserB.TokenType==B>(lhs:ParserA, rhs:ParserB) -> Parser<B> {
        return liftA2(const(id))(lhs)(rhs)
}

public func liftA2<ParserA:ParserType, ParserB:ParserType, A, B, C
    where ParserA.TokenType==A, ParserB.TokenType==B>(f:A -> B -> C)(_ a:ParserA)(_ b:ParserB) -> Parser<C> {
        return f <§> a <*> b
{'fraction_non_alphanumeric': 0.16378162450066577, 'fraction_numerical': 0.012316910785619174, 'mean_word_length': 3.6806853582554515, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7118283', 'n_tokens_mistral': 1157, 'n_tokens_neox': 1105, 'n_words': 239}