How do I pass an operator as a closure outside of an argument?

I'm trying out code like:

guard areIndicesForMinima ? element > subRoot : element < subRoot else {
  continue
}

and wanted to compact the two comparisons into a single closure. But I can't figure out how to express the comparison operations.

let compare: (Element, Element) -> Bool
if areIndicesForMinima {
    compare = >
} else {
    compare = <
}

I think the compiler saw this as a generic-type building error. Also, I can't get "Element.>" or "Element.(>)" to work. I know from the Sequence.reduce method that raw operators work if put them in as a function argument, but I'm not doing that here.

This is similar to the post " Question about passing addition operator as function" that was suggested to me, but I'm not calling the result immediately.

What am I missing? Or is this a hole in naming entities?

compare = (<)
2 Likes

Thanks.

I have right now:

guard (areIndicesForMinima ?(>):(<))(element, subRoot) else { continue }

(The space between the "areIndicesForMinima" and "?" is required.)

Terms of Service

Privacy Policy

Cookie Policy