Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why pipes defines inner functions

I'm looking at the pipes library source code and for instance in the Core module I don't understand why the author is all over the place using the pattern of defining functions like that:

runEffect = go
  where
    go p = ...

Or:

pull = go
  where
    go a' = ...

Or:

reflect = go
  where
    go p = ...

Is this some trick to enable some optimizations? I find it ugly, if it's some optimization trick I really wish the compiler could do it without things like that. But maybe there's another reason?

like image 216
Emmanuel Touzery Avatar asked Jul 01 '15 18:07

Emmanuel Touzery


People also ask

What does it mean to pipe a function?

The command line programs that do the further processing are referred to as filters. We can define pipe function: A pipe function is a function that accepts a series of functions, which process an input parameter and return a output which will be the input for the next function.

Are nested functions bad practice?

no, there's nothing wrong with that at all, and in js, it's usually a good thing. the inside functions may not be a pure function, if they rely on closure variables. If you don't need a closure or don't need to worry about polluting your namespace, write it as a sibling.

How do you use a pipe function?

This function takes a single argument, an array of two integers (filedes). filedes[0] is used for reading from the pipe, and filedes[1] is used for writing to the pipe. The process which wants to read from the pipe should close filedes[1], and the process which wants to write to the pipe should close filedes[0].

Can I define a function inside a function Python?

If you define a function inside another function, then you're creating an inner function, also known as a nested function. In Python, inner functions have direct access to the variables and names that you define in the enclosing function.


1 Answers

GHC will only inline non-recursive functions, and only when they are "fully applied" from a syntactic point of view (i.e. at the call site they are applied to the number of arguments that appear in the left hand side in the definition).

In the examples you posted there are no arguments, however the definitions are likely recursive and wouldn't be inlined. Doing this transformation probably allows the definitions to be inlined and specialized (for the concrete types of m etc.) at the call site.

Is this some trick to enable some optimizations? I find it ugly, if it's some optimization trick I really wish the compiler could do it without things like that.

Yeah it's super lame.

like image 191
jberryman Avatar answered Sep 22 '22 15:09

jberryman