# Functional version of Base library

**URL:** https://discourse.julialang.org/t/functional-version-of-base-library/54987
**Category:** Internals & Design
**Created:** [February 10, 2021, 12:16pm UTC](https://discourse.julialang.org/t/functional-version-of-base-library/54987 "2021-02-10T12:16:54Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![anon74562486](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@anon74562486](https://discourse.julialang.org/u/anon74562486)
#### Post date: [February 10, 2021, 12:16pm UTC](https://discourse.julialang.org/t/functional-version-of-base-library/54987/1 "2021-02-10T12:16:54Z")

</div>

Is there a functional version of the Base library?  
is it possible to do this?

```julia
x = rand(10, 10)
r_x = x |> 
            reshape((1, length(x))) |> # convert x to a 1xlength(x) Matrix 
            r |> # apply r
            reshape(size(x)) # converto to original size 

```

instead of this:

```julia
x = rand(10, 10)
r_x = reshape(reshape(x, (1, length(x))) |> r, size(x)) # convert to a 1xlength(x) Matrix, apply r, convert to original size

```

Thank you.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [February 10, 2021, 12:24pm UTC](https://discourse.julialang.org/t/functional-version-of-base-library/54987/2 "2021-02-10T12:24:56Z")

</div>

With a little bit more typing:

```julia
r_x = x |> reshape((1, length(x)))

```

works with:

```julia
x |> x -> reshape(x,(1,length(x)))

```

---

<div class="post-metadata">

### Author: ![anon74562486](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@anon74562486](https://discourse.julialang.org/u/anon74562486)
#### Post date: [February 10, 2021, 1:21pm UTC](https://discourse.julialang.org/t/functional-version-of-base-library/54987/3 "2021-02-10T13:21:59Z")

</div>

Thank you.  
Does multiple dispatch allow you to do this?

```julia
Base.reshape(dim::Tuple) = x -> Base.reshape(x, dim)

```

Is there a reason why this is not present in the library Base?  
It’s a bad practice?

I think it would be useful to have functional versions of the Base library functions.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 10, 2021, 1:44pm UTC](https://discourse.julialang.org/t/functional-version-of-base-library/54987/4 "2021-02-10T13:44:01Z")

</div>

> [@anon74562486](#):
>
> I think it would be useful to have functional versions of the Base library functions.

This is possible, and in several cases (e.g. `==`) we already do this. However, in a multi-argument function it’s often not clear what argument you would want to “functionalize” (i.e. “curry”).

If `x ->` is too verbose, it would be better to have a syntax for this (e.g. [RFC: curry underscore arguments to create anonymous functions by stevengj · Pull Request #24990 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/24990)), but we’ve had a hard time agreeing on the details.

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [February 10, 2021, 4:25pm UTC](https://discourse.julialang.org/t/functional-version-of-base-library/54987/5 "2021-02-10T16:25:40Z")

</div>

You might like one of several packages implementing ideas like this. Chain.jl has a good summary of the options:

> **[GitHub - jkrumbiegel/Chain.jl: A Julia package for piping a value through a...](https://github.com/jkrumbiegel/Chain.jl)**
>
> A Julia package for piping a value through a series of transformation expressions using a more convenient syntax than Julia's native piping functionality. - GitHub - jkrumbiegel/Chain.jl: A Jul...

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [February 10, 2021, 4:38pm UTC](https://discourse.julialang.org/t/functional-version-of-base-library/54987/6 "2021-02-10T16:38:33Z")

</div>

> [@anon74562486](#):
>
> `reshape(x, (1, length(x))`

BTW, you can make this more simple

```julia
reshape(x, 1, :) # or reshape(x, (1, :))

```

---

<div class="post-metadata">

### Author: ![anon74562486](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@anon74562486](https://discourse.julialang.org/u/anon74562486)
#### Post date: [February 10, 2021, 5:32pm UTC](https://discourse.julialang.org/t/functional-version-of-base-library/54987/7 "2021-02-10T17:32:36Z")

</div>

Thanks to everyone.

> [@stevengj](#):
>
> This is possible, and in several cases (e.g. `==` ) we already do this. However, in a multi-argument function it’s often not clear what argument you would want to “functionalize” (i.e. “curry”).

I think functions like reshape, map, filter, reduce, + operator, etc, can be made functional without ambiguity.

Is this code good or there are bad practices?

```julia
Base.reshape(dim::Tuple) = x -> Base.reshape(x, dim) # if the function takes the dimension
Base.reshape(array) = x -> Base.reshape(array, x) # if the function takes the array

```

* * *

> [@contradict](#):
>
> You might like one of several packages implementing ideas like this. Chain.jl has a good summary of the options

Thank you, but I don’t like custom solutions with macros and I don’t want that who reads my code must know too many libraries.

* * *

> [@DNF](#):
>
> BTW, you can make this more simple
> 
> ```julia
> reshape(x, 1, :) # or reshape(x, (1, :))
> 
> ```

Thank you, but I don’t like a reshape inside another reshape. I would like to use a more elegant style with pipe operator.

* * *

Sorry for my english.

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [February 10, 2021, 6:51pm UTC](https://discourse.julialang.org/t/functional-version-of-base-library/54987/8 "2021-02-10T18:51:24Z")

</div>

> [@anon74562486](#):
>
> I think functions like reshape, map, filter, reduce, + operator, etc, can be made functional without ambiguity.

You might think so, but see e.g. [Deprecate/remove map(f) and foreach(f) · Issue #35293 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/35293). `+` is also a good example to illustrate the problem, since we do have a unary `+` already (as in `+(42)`) in symmetry with unary `-`, so we can’t add a curried version.

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [February 10, 2021, 6:53pm UTC](https://discourse.julialang.org/t/functional-version-of-base-library/54987/9 "2021-02-10T18:53:41Z")

</div>

> [@anon74562486](#):
>
> ```julia
> Base.reshape(dim::Tuple) = x -> Base.reshape(x, dim) # if the function takes the dimension
> Base.reshape(array) = x -> Base.reshape(array, x) # if the function takes the array
> 
> ```

This is [type piracy](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-type-piracy) and should generally be avoided. The latter also has a meaning already:

```julia
julia> reshape([1])
0-dimensional Array{Int64,0}:
1

```

---

<div class="post-metadata">

### Author: ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)
#### Post date: [February 10, 2021, 6:59pm UTC](https://discourse.julialang.org/t/functional-version-of-base-library/54987/10 "2021-02-10T18:59:07Z")

</div>

An alternative, if you don’t like the nested reshapes but need to repeat this pattern several times, is to stick it into a function – so that at least your code has it only once. I think this is quite readable:

```julia
apply_reshaped(f, x, shape) = reshape(f(reshape(x, shape)), size(x))

apply_reshaped(r, x, (1, :))

```

---

<div class="post-metadata">

### Author: ![anon74562486](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@anon74562486](https://discourse.julialang.org/u/anon74562486)
#### Post date: [February 10, 2021, 9:07pm UTC](https://discourse.julialang.org/t/functional-version-of-base-library/54987/11 "2021-02-10T21:07:28Z")

</div>

Thanks for the answers, I found out a lot of new things I didn’t know.  
Before asking this question I thought that a more functional style was more elegant and less prone to errors.  
Now I find out that there are several problems with this functional approach.  
At this point I’m wondering: how popular is the pipe operator?  
Does it make sense to have a more functional style?  
Sorry for my English, I’m using an automatic translator.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [February 10, 2021, 9:13pm UTC](https://discourse.julialang.org/t/functional-version-of-base-library/54987/12 "2021-02-10T21:13:39Z")

</div>

> [@anon74562486](#):
>
> Thank you, but I don’t like a reshape inside another reshape.

No, I’m not trying to convince you to use `reshape` for this purpose. I’m saying that _if_ you use `reshape` for anything, you can use `:` instead of `length(x)`. It is more elegant.
