What is the advantage of arrows over regular functions in haskell. What can they do the functions can't. Functions can map over structures using fmap.
This arrow function reduces lots of code and makes the mode more readable. Arrow function syntax automatically binds “this” to the surrounding code's context. Writing the arrow => is more flexible as compared with the writing function keyword.
The takeaway: Function expressions are best for object methods. Arrow functions are best for callbacks or methods like map, reduce, or forEach. You can read more about scopes on MDN. On a fundamental level, arrow functions are simply incapable of binding a value of this different from the value of this in their scope.
Since regular functions are constructible, they can be called using the new keyword. However, the arrow functions are only callable and not constructible, i.e arrow functions can never be used as constructor functions. Hence, they can never be invoked with the new keyword.
I assume you mean pointers to functions as opposed to direct function calls. No, they are the same speed.
On more of a broad picture, arrows get you out of Hask and into other categories there are to explore. The Kleisli category is probably the best-acquainted to Haskellers, followed by Cokleisli. Those are the natural "extensions" of Hask: add an endofunctor around either the result or argument, then you get a category again if
Kleisli
: the functor is a monad, so id ≅ return :: a -> m a
(.) ≅ (<=<) :: (b->m c) -> (a->m b) -> a->m c
CoKleisli
: the functor is a comonad, so id ≅ coreturn :: m a -> a
and
(.) :: (m b->c) -> (m a->b) -> m a->c
(For that you don't need Arrow
yet, only Category
. But general categories aren't very interesting, you normally want monoidal or even cartesian closed categories, which is what Arrow
is roughly aiming at.)
But there are sure are lots of other categories. Most don't have much to do with Hask and can't be expressed with the standard Arrow
class, mainly because the objects have special properties that not every Haskell type fulfills. Actually, if you add the ability to constrain the object types, the possibilities immediately become much wider. But even if you stay with the standard classes, perhaps even simply in ->
, the point-free composition-style that is natural with arrows often comes out very nice, concise, and opens up new ways to think about transformations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With