introduce `Base.FixNullaryCallable`, generalizing `Base.Fix` #61034
Motivation:
I have a function like this:
function func(x, y)
for e ∈ y
x = g(x, e)
end
x
end
Rewriting it using foreach instead of for:
function func(x, y)
X = Ref{Any}(x)
function f(e)
g(getindex(X), e)
end
foreach(Base.Fix1(setindex!, X) ∘ f, y)
X[]
end
However, with Base.FixNullaryCallable, it is possible to go further along in the tacit programming style, also known as point-free style:
function func(x, y)
X = Ref{Any}(x)
foreach(Base.Fix1(setindex!, X) ∘ Base.FixNullaryCallable{1}(g, Base.Fix1(getindex, X)), y)
X[]
end
const Fix1 = Fix{1}
const Fix2 = Fix{2}
see if this happens to fix the type alias displaying
Currently blocked by what I suppose is a bug/suboptimality in the type-printing machinery. Specifically, the type alias Base.Fix1 gets shown in an overly complicated manner. As does Base.Fix2. Everything else seems as expected and non-breaking.
So I suppose I will have to make a PR for show.jl before this PR can be considered.
this seems a little arcane / niche to me. In my subjective opinion the usage example given in the PR description is a lot more confusing to read and is not an improvement over
function func(x, y)
for e ∈ y
x = g(x, e)
end
x
end
Yeah, but that's just because imperative code is the norm in the Julia world. Functional/tacit programming is a valid style nevertheless.
Seems like it could live in a package? The only change in this PR that isn't just introducing new code is to make Fix a special case, but if Fix is left as-is, the changes here are completely separable.
nsajko
Allow obtaining the value of the fixed argument from a nullary callable, also known as a thunk, instead of just from a fixed value.