Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell functions left-to-right

I have a function that I use quite frequently, which allows me to write my code in a way which seems more natural to me.

infixl 6 $:
($:) :: a -> (a -> b) -> b
a $: f = f a

This lets me do something like

let x = getData
        $: sort
        $: group
        $: aggregate

instead of

let x = aggregate 
        $ group 
        $ sort 
        $ getData

I recently learned that Clojure has something like this built in (I don't know much Clojure, but I think it would be written (-> getData sort group aggregate)?) which makes me wonder if Haskell has it built in as well. Hoogle doesn't have any results though.

Are there any standard libs with something similar included? It probably makes my code hard for others to read if I have such a common part is idiosyncratic.

like image 706
Xodarap Avatar asked Apr 04 '12 01:04

Xodarap


People also ask

What does flip do in Haskell?

flip f takes its (first) two arguments in the reverse order of f. flip the order of the first two arguments of a function.

How are higher-order functions used in Haskell?

In Haskell, a function that can take other functions as arguments or return functions is called a higher-order function. These are particularly useful in that they allow us to create new functions on top of the ones we already have, by passing functions as arguments to other functions.

How does map function work in Haskell?

The map() function takes two parameters namely a list and the function to be applied on each element in the list and returns a new list as the output. The map() function is available in Data. Map module in Haskell programming language.

What are functions in Haskell?

Functions play a major role in Haskell, as it is a functional programming language. Like other languages, Haskell does have its own functional definition and declaration. Function declaration consists of the function name and its argument list along with its output.


2 Answers

The reverse application operator your are describing is now part of the standard package base (since 4.8.0) as the & operator.

Note that this operator is defined with a lower precedence than the one you proposed (infixl 1).

like image 32
Yoann B. Avatar answered Sep 30 '22 14:09

Yoann B.


There's nothing like this built in, but Control.Category.(>>>) is close: it's flip (.), so you can write

f x = x $: sort $: group $: aggregate

as

f = sort >>> group >>> aggregate

There's no shortage of definitions and names for your ($:) combinator. I think functions tend to suit the pipeline style more often than simple applications, so I don't feel any great need for it; (>>>) is a bit ugly, though.

(Besides, Haskell's non-strict semantics mean that the flow of data isn't necessarily in the direction the arrows are pointing here; after all, aggregate could provide the first constructor before sort even gets a chance to look at the argument. So I tend to just use (.) and ($); I'm used to the order.)

like image 194
ehird Avatar answered Sep 30 '22 15:09

ehird