Example compose operator

(Chris Lattner) #1

Here’s a random one I got online. I don’t think the @escaping's are required though, if we can drop them, please do. (they are needed).

precedencegroup Group { associativity: left }
infix operator >| : Group
func >| <A, B, C>(_ lhs: @escaping (A) -> B,
                   _ rhs: @escaping (B) -> C) -> (A) -> C {
    return { rhs(lhs($0)) }
}
2 Likes

(urmas pitsi) #2

Would it make sense to use similar operators as in F#:

  1. pipe forward: |>, example: let inline (|>) x f = f x

  2. function composition: >>, example: let inline (>>) f g x = g(f x)

as explained here: https://blogs.msdn.microsoft.com/chrsmith/2008/06/14/function-composition/

1 Like

(Jeremy Howard (Admin)) #3

Yes I wondered about that too. It would mean >> has two very different meanings however (i.e bit shift) but since they’re very different contexts I’m not sure it matters.

1 Like

(urmas pitsi) #4

that’s a good point. I didn’t notice that ‘>>’ operator has been taken already by bit shift.
Well, maybe it just comes down to getting more used to ‘>|’ operator. At first it just seemed kind of weird for me.
Must admit that I love the nice declarative style of Swift and functional programming features.

0 Likes

(Jeremy Howard (Admin)) #5

Honestly we only picked that because it’s the first thing @clattner found with a quick search. At some point we’ll think about this more carefully. At https://www.pointfree.co/ they have suggestions for a bunch of composition operators we should probably steal instead.

BTW in F# they use >>> for bit-shift.

2 Likes

(Chris Lattner) #6

Yeah, I’d prefer to avoid overloading operators to have two different meanings, it is better to invent a new operator for a new meaning.

C++ is notoriously bad for this, it uses << and >> for i/o etc, because you can’t define new operators.

2 Likes

#7

this is also being used by elixir language as pipe forward

0 Likes