Skip to content

Instantly share code, notes, and snippets.

@joebutler2
Created April 10, 2020 01:47
Show Gist options
  • Save joebutler2/7034e18ecdc17764fd9da1bcc58e43ac to your computer and use it in GitHub Desktop.
Save joebutler2/7034e18ecdc17764fd9da1bcc58e43ac to your computer and use it in GitHub Desktop.
How to implement the complement function from Clojure
# TIL in Clojure the complement function takes a predicate and
# returns a function that is like the original function but negates
# the results.
# E.g.
# def is_greater(a, b); a > b; end
# complement(:is_greater)[1, 2] # => true
def complement(method_name)
lambda do |*args|
!method(method_name).call(*args)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment