# Fix1 (analogue of Base.Fix2)

**URL:** <https://discourse.julialang.org/t/fix1-analogue-of-base-fix2/10161>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [April 4, 2018, 3:46pm UTC](https://discourse.julialang.org/t/fix1-analogue-of-base-fix2/10161 "2018-04-04T15:46:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 4, 2018, 3:46pm UTC](https://discourse.julialang.org/t/fix1-analogue-of-base-fix2/10161/1 "2018-04-04T15:46:01Z")

</div>

I was very happy to see `Base.Fix2` since it saves typing for creating closures.

Would it make sense to introduce a `Fix1` that does the same, except for fixing the _first_ argument?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 4, 2018, 4:31pm UTC](https://discourse.julialang.org/t/fix1-analogue-of-base-fix2/10161/2 "2018-04-04T16:31:50Z")

</div>

On the other hand, [https://github.com/JuliaLang/julia/pull/24990](https://github.com/JuliaLang/julia/pull/24990) would turn `Fix2(f, x)` into `f(_, x)` and the hypothetical `Fix1(f, x)` into `f(x, _)` which I personally like quite a lot.

I guess another question would be whether you want the result of `Fix1(f, x)` to be `y -> f(x, y)` as above or the more general `(args...) -> f(x, args...)` ? `Fix2` seems more clear in its purpose, since it assumes that `f` has exactly two arguments, but it feels odd for `Fix1` to make the same assumption.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [April 4, 2018, 4:36pm UTC](https://discourse.julialang.org/t/fix1-analogue-of-base-fix2/10161/3 "2018-04-04T16:36:28Z")

</div>

I don’t remember where I saw the discussion, but last I heard the core devs seemed quite resolved to support general currying with `_` as in #24990, just not in 1.0. (I think I remember seeing 1.x, but I don’t want to put words in anyone’s mouth.) So, I have a pretty strong feeling that this is something we’ll have in the not-so-distant future.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 4, 2018, 5:14pm UTC](https://discourse.julialang.org/t/fix1-analogue-of-base-fix2/10161/4 "2018-04-04T17:14:47Z")

</div>

I would distinguish

1. having a parametric type that embodies partial application, eg `Fix2` above,

2. surface syntax for partial application, eg the proposed syntax with `_`.

(1) exposes the components of what would be an otherwise opaque closure, and allows neat things like a `show` method for certain function/argument combinations (this is what I want to use it for, actually).

(2) is syntactic sugar.

They can be done independently.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [April 4, 2018, 5:21pm UTC](https://discourse.julialang.org/t/fix1-analogue-of-base-fix2/10161/5 "2018-04-04T17:21:56Z")

</div>

> [@Tamas\_Papp](#):
>
> exposes the components of what would be an otherwise opaque closure, and allows neat things like a show method for certain function/argument combinations (this is what I want to use it for, actually)

I guess I’m lacking in imagination. Could you give an example of what you have in mind?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [April 4, 2018, 5:34pm UTC](https://discourse.julialang.org/t/fix1-analogue-of-base-fix2/10161/6 "2018-04-04T17:34:13Z")

</div>

Sure, want to make a PR?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 4, 2018, 5:35pm UTC](https://discourse.julialang.org/t/fix1-analogue-of-base-fix2/10161/7 "2018-04-04T17:35:27Z")

</div>

Imagine an interface with

```julia
loglikelihood(model::SomeModel, parameters)

```

where `SomeModel` is a user-defined type.

Now with

```julia
loglikelihood(model) = Fix1(loglikelihood, model) # hypothetical

function Base.show(io::IO, l::Fix1{typeof(loglikelihood), M}) where M
    println(io, "log likelihood callable for model $(l.x)")
end

```

one could do custom printing.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 4, 2018, 5:35pm UTC](https://discourse.julialang.org/t/fix1-analogue-of-base-fix2/10161/8 "2018-04-04T17:35:37Z")

</div>

Yes, will make on tomorrow.

---

<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:** [April 5, 2018, 2:49am UTC](https://discourse.julialang.org/t/fix1-analogue-of-base-fix2/10161/9 "2018-04-05T02:49:07Z")

</div>

> [@Tamas\_Papp](#):
>
> Would it make sense to introduce a Fix1 that does the same, except for fixing the first argument?

Note that this is already planned. See e.g. [replace `equalto` and `occursin` with curried `isequal`, `==`, and `in` by JeffBezanson · Pull Request #26436 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/26436#issuecomment-373490587)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 5, 2018, 8:15am UTC](https://discourse.julialang.org/t/fix1-analogue-of-base-fix2/10161/10 "2018-04-05T08:15:09Z")

</div>

PR done:  
[https://github.com/JuliaLang/julia/pull/26708](https://github.com/JuliaLang/julia/pull/26708)

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [April 5, 2018, 7:40pm UTC](https://discourse.julialang.org/t/fix1-analogue-of-base-fix2/10161/11 "2018-04-05T19:40:22Z")

</div>

Merged.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [April 5, 2018, 8:16pm UTC](https://discourse.julialang.org/t/fix1-analogue-of-base-fix2/10161/12 "2018-04-05T20:16:32Z")

</div>

Would it make sense to go in the direction of C++ [`std::bind`](http://en.cppreference.com/w/cpp/utility/functional/bind) (see examples section)?
