Last active
August 30, 2018 09:43
-
-
Save solson/05f66de4904b793593f08eb04531ee9e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module What | |
| export Whatever, what | |
| struct Whatever{NumArgs, F <: Function} | |
| f::F | |
| Whatever{N}(f::F) where {N, F <: Function} = new{N, F}(f) | |
| end | |
| const what = Whatever{1}(identity) | |
| (w::Whatever{N})(args::Vararg{Any, N}) where N = w.f(args...) | |
| for op in [:+, :-, :*, :/, :div, :^, :(==), :>, :<, :>=, :<=, :!=] | |
| @eval begin | |
| import Base.$op | |
| $op(w::Whatever{N}, x) where N = | |
| Whatever{N}((args...) -> $op(w(args...), x)) | |
| $op(x, w::Whatever{N}) where N = | |
| Whatever{N}((args...) -> $op(x, w(args...))) | |
| $op(w1::Whatever{N1}, w2::Whatever{N2}) where {N1, N2} = | |
| Whatever{N1 + N2}((args...) -> $op( | |
| w1(args[1:N1]...), | |
| w2(args[N1+1:end]...), | |
| )) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment