I was reading this article and I came across the following two functions:
// Sequence actions, discarding the value of the second argument
func <* <A, B>(p: Parser<A>, q: Parser<B>) -> Parser<A> {
return const <^> p <*> q
}
// Sequence actions, discarding the value of the first argument
func *> <A, B>(p: Parser<A>, q: Parser<B>) -> Parser<B> {
return const(id) <^> p <*> q
}
What are the const
and const(id)
? I'm guessing they are some kind of values, but what values? Are they implicit left- or right-hand side operands? (This is just a shot in the dark). I could not find any info about it.
Swift has no const
keyword.
The talk uses the TryParsec library, which defines this const
function:
/// Constant function.
internal func const<A, B>(_ a: A) -> (B) -> A
{
return { _ in a }
}
In an expression like const(a)(b)
, the types of a
and b
can be anything. The expression evaluates a
, then evaluates b
, then discards the value of b
and returns the value of a
.
It is modeled on the Haskell function const
, which is itself an implementation of the K combinator.