Still plugging away and trying to grasp the concepts here. I love it, and I am committed now, but just wanted to stop and ask everyone something pretty general — How you do read the types? As in, what words go through your head in your attempt to translate these things.
A good example for me is the definition for the pipe operator and backwards func operator.
(<|) : (a → b) → a → b
&
(|>) : a → (a → b) → b
I get the idea, and have a good understanding of how they work, but when I try to just explain these type definitions I cannot really do it. Just wondering how people with more experience read these things. Thought maybe it will unlock something trapped in my brain or something.
If I’m reading them to myself: (a → b) → a → b (A to B) to A to B a → (a → b) → b A to (A to B) to B
where the words in parentheses are said more quickly together.
If I’m explaining them to someone else: (a → b) → a → b a function that takes two arguments and returns a B. The first of argument is a function from A to B and the second argument is an A. a → (a → b) → b a function that takes two arguments and returns a B. The first argument is an A and the second argument is a function from A to B.
(a -> b) -> a -> b A function that takes a function from a to b and an a and returns a b. a -> (a -> b) -> b A function that takes an a and a function from a to b and returns a b.
There are multiple aspects in that type definition that make it a little harder than most.
(<|) : (a → b) → a → b
First, you need to mention that this is a function that is designed to be used infix. An infix function takes 2 arguments. The first argument is the left argument and in this case it is a function from a to b. The second argument is the right argument and in this case it is a value of type a. The result of the infix application of the arguments is of type b.
Explaining a type signature as above requires that the person already understands the concept of a function and of a higher order function ( a function that takes a function as an argument, or returns a function).
value : a - a value of type a
unary : a -> b - a function that takes a value of type a and evaluates to a value of type b
binary : a -> b -> c - a function that takes two arguments. First is a value of type a, second is a value of type b. The function evaluates to a value of type c
higherOrder : (a -> b) -> a -> b - a binary function that takes an unary function as the first argument and a value of type a as the second and evaluates to a value of type b.
And then you get into currying and partial application where a higher arity function can be reduced to a lower arity function by partial application.
So, binary: a -> b -> c it is actually an unary function that produces an unary function so… binary : a -> (b -> c) . ternary : a -> ( b -> ( c -> d)) , etc.
All this gets confusing fast if you don’t understand the idea of higher order functions.
I find when things get complicated, I try to use different sentence patterns for the same concepts to help distinguish between different levels of nesting. And I tend to repeat small chunks to myself and then build up larger chunks. So in your examples, I might think:
(<|) : (a → b) → a → b
okay, we’ve got a function
the first argument is another function that converts a to b
the second argument is an a
and it returns a b
(|>) : a → (a → b) → b
okay, we’ve got an a
and we’ve got a function that converts a's to b's
and it returns a b
(I guess I like to hype myself up by starting with “okay” )
With those specific examples, I’m familiar enough with them that I wouldn’t normally think all that, but when I see functions that start to confuse me, that’s the type of self-talk I tend to fall back on.
@avh4@pdamoc@plaxdan@Lucas_Payr@dta Thank you all! I think I was almost just relieved that I was not completely off in my own mind. Its one of those seemingly mundane things but I find myself questioning myself a lot when it comes to this particular practice, so just hearing how you all approach it really helps – especially knowing I’m not the only one doing the “okay okay” thing