Skip to content

Instantly share code, notes, and snippets.

@firesofmay
Last active August 29, 2015 14:06
Show Gist options
  • Save firesofmay/0abc1294b9fd96386a03 to your computer and use it in GitHub Desktop.
Save firesofmay/0abc1294b9fd96386a03 to your computer and use it in GitHub Desktop.
(defn fancy-mod
"Takes the following parameters and
returns a function which takes two integer numbers.
- operator-fn => Mathematical Operator : + OR * OR / OR -
- mod-by => number to mod by, example 2.
- output-fn => str or int.
Example:
((fancy-mod + 2 str) 10 11)
;;=> \"2\"
Also can be used like
> (def mod-by-3->int (fancy-mod + 3 int))
> (mod-by-3->int 11 11)
;;=> 1
Note: Throws exception if invalid parameters passed!"
[operator-fn mod-by output-fn]
{:pre [(#{+ - / *} operator-fn)
(integer? mod-by)
(#{str int} output-fn)]}
(fn [num1 num2]
{:pre [(integer? num1)
(integer? num2)]}
(output-fn (#(mod % mod-by)
(operator-fn num1
num2)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment