Skip to content

Instantly share code, notes, and snippets.

@solson
Last active August 30, 2018 09:43
Show Gist options
  • Select an option

  • Save solson/05f66de4904b793593f08eb04531ee9e to your computer and use it in GitHub Desktop.

Select an option

Save solson/05f66de4904b793593f08eb04531ee9e to your computer and use it in GitHub Desktop.
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