archermarx / partialfunctions.jl Goto Github PK
View Code? Open in Web Editor NEWA small package to simplify partial function application
License: MIT License
A small package to simplify partial function application
License: MIT License
This issue is used to trigger TagBot; feel free to unsubscribe.
If you haven't already, you should update your TagBot.yml
to include issue comment triggers.
Please see this post on Discourse for instructions and more details.
If you'd like for me to do this for you, comment TagBot fix
on this issue.
I'll open a PR within a few hours, please be patient!
Hello.
Over the last weeks, I ended up creating my own partial application package. One interesting feature I managed to implement that I think is pretty cool is a special "placeholder constant". It acts very similar to the _
in your @$
-macro.
As an example, look at this:
greeter = println ¦ "Hello, " ¦ ⎵ ¦ ", nice to meet you!"
greeter("bobby")
# ->Hello, bobby, nice to meet you!
greeter("peter", " Have a nice Day")
# ->Hello, peter, nice to meet you! Have a nice Day
The ¦
in my code has the same role as $
in your package. The ⎵
is my placeholder constant, signifying that an argument should be supplied here.
If you want to implement this feature, one thing to consider is the name of this constant. Some suggestions:
⎵
(\underbracket
) like I did. Advantages: expressive, the function of this is visually very obvious, unlikely to clash with other definitions made by the user/other packages loaded by the user. Disadvantage: People without IDE support (VSCode, Neovim, Jupyter) will have difficulty typing this character, as it is not part of any keyboard I know of._!
(_
itself is not possible). Advantage: still somewhat expressive, sidesteps the problem from the last point. Disadvantage: pretty ugly!The fact that there is a placeholder and its position should be accessible in the type signature, so code generation can make use of this and shuffle the arguments accordingly at compile time.
As in my last issue, I would supply this as a PR myself, but I am a bit short on time recently, and the way I have structured my package is quite a bit different to yours, so some adaptation is needed. I will give you a link to my code, if you want to do this you can use whatever you like from it, it is pretty simple and well documented. The relevant code is anything involving the type ReserveFunc
. Anything past line 110 is just pretty printing and can be ignored.
Link: https://github.com/arikheinss/PartialApplication.jl/blob/main/src/PartialApplication.jl
Hello once more,
I noticed another source of performance regression when using partial functions compared to native anonymous functions x -> ....
, which is that partial functions may lock the compiler out of some of its optimization routines. I documented my findings in details in a topic on discourse here.
As mentioned in the topic, AFAIK there is no solution to this problem that could be employed for your package, but I thought as the maintainer maybe you would want to know about the issue.
That is all for now. Cheers, and merry christmas!
Any reason for this? https://juliahub.com/ui/Packages/PartialFunctions/dX5Z0/ gives 1.0.4
On Julia v1.6 it seems the flip
method is not exported by the PartialFunctions module for me, I get the following
julia> names(PartialFunctions)
3-element Vector{Symbol}:
:$
:<|
:PartialFunctions
I cloned the package and made a change to the export
statement on PartialFunctions.jl
, from
export $, <|
to
export $
export <|
and it seemed to solve the problem, although I am not sure why. I can make a pull request with the changes.
Thanks!
Hello.
I have noticed pretty significant performance regressions when partially applying data types, like convert $ Float64
, round $ Int
, parse $ Int
etc. To reproduce, take this as an example:
using PartialFunctions, BenchmarkTools
data = rand(1:20, 1000) .|> string
@btime val_native = mapreduce( x -> parse(Int, x), +, data)
# -> 18.243 μs (1 allocation: 16 bytes)
@btime val_partial = mapreduce( parse $ Int, +, data)
# -> 129.363 μs (958 allocations: 14.98 KiB)
The reason for this can be found when inspecting the type:
typeof(parse $ Int)
# -> PartialFunctions.PartialFunction{nothing, nothing, typeof(parse), Tuple{DataType}, NamedTuple{(), Tuple{}}}
Notice the Tuple{DataType}
in there. When compiling all the compiler sees is this signature: parse(::DataType, ::String)
, which is not enough information to compile type stable code.
The reason that happens is that
typeof(Int) # -> DataType
# same for all other data types
and that DataType
is technically a concrete type but practically not really. Ideally this should be replaced with Type{Int}
.
One way to fix this would be to supply a new specialized method for the $
function,
($)(f::Function, ::Type{T}) where T = ...
wherein the type parameter is manually set to Type{T}
.
This problem has been solved before by the Fix1
type, see here.
Alternatively, I experimentally created my own partial application package. You can have a look how I solved the problem here.
I would offer the PR with a fix myself, but I have had very little time for hobby programming the past weeks, so I decided to just notify you about the problem instead 🤷♂️.
Hey, thanks for this package! I've been playing around with it in order to implement some functional programming stuff.
I was wondering if there is a way to run a function that has a limited amount of arguments, without having to explicitly call the <| ()
. For example
f(x,y) = 2(x+y)
f $ 1 $ 2 # This should immediately run the function
It seems like there is a 1.2 tag on github but it never got released on the registry
https://github.com/JuliaRegistries/General/blob/master/P/PartialFunctions/Versions.toml
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.