Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantage of Arrows over Functions

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.

like image 307
PyRulez Avatar asked Apr 15 '14 23:04

PyRulez


People also ask

What is advantages of arrow functions over normal functions?

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.

Should I use arrow functions or functions?

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.

How are arrows different from normal functions?

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.

Is arrow function faster than function?

I assume you mean pointers to functions as opposed to direct function calls. No, they are the same speed.


1 Answers

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.

like image 99
leftaroundabout Avatar answered Sep 29 '22 08:09

leftaroundabout