项目作者: killme2008

项目描述 :
A macro to define clojure functions with parameter pattern matching just like erlang or elixir.
高级语言: Clojure
项目地址: git://github.com/killme2008/defun.git
创建时间: 2014-09-14T06:29:15Z
项目社区:https://github.com/killme2008/defun

开源协议:Eclipse Public License 1.0

下载


Clojure CI

defun

A macro to define clojure functions with pattern matching just as erlang or elixir. It supports both clojure and clojurescript.

Usage

Dependency in leiningen:

  1. [defun "0.4.0"]

Basic usage

Require defun.core in clojure:

  1. (require '[defun.core :refer [defun]])

Or refer-macros in clojurescript:

  1. (ns cljs-test
  2. (:require [defun.core :refer-macros [defun]])
  3. (enable-console-print!)

Try to define function just like defn:

  1. (defun hello
  2. "hello world"
  3. [name] (str "hello," name))
  4. (hello "defun")
  5. ;; "hello,defun"

Supports variadic arguments, doc, metadata etc. as defun too. No surprises?

The fun thing is coming, let’s say hi to people:

  1. (defun say-hi
  2. ([:dennis] "Hi,good morning, dennis.")
  3. ([:catty] "Hi, catty, what time is it?")
  4. ([:green] "Hi,green, what a good day!")
  5. ([other] (str "Say hi to " other)))

Then calling say-hi with different names:

  1. (say-hi :dennis)
  2. ;; "Hi,good morning, dennis."
  3. (say-hi :catty)
  4. ;; "Hi, catty, what time is it?"
  5. (say-hi :green)
  6. ;; "Hi,green, what a good day!"
  7. (say-hi "someone")
  8. ;; "Say hi to someone"

We define functions just like Erlang’s function with parameters pattern match (thanks to core.match), we don’t need if,cond,case any more, that’s cool!

Recursion

Let’s move on, what about define a recursive function? That’s easy too:

  1. (defun count-down
  2. ([0] (println "Reach zero!"))
  3. ([n] (println n)
  4. (recur (dec n))))

Invoke it:

  1. (count-down 5)
  2. ;;5
  3. ;;4
  4. ;;3
  5. ;;2
  6. ;;1
  7. ;;Reach zero!
  8. nil

An accumulator from zero to number n:

  1. (defun accum
  2. ([0 ret] ret)
  3. ([n ret] (recur (dec n) (+ n ret)))
  4. ([n] (recur n 0)))
  5. (accum 100)
  6. ;;5050

A fibonacci function:

  1. (defun fib
  2. ([0] 0)
  3. ([1] 1)
  4. ([n] (+ (fib (- n 1)) (fib (- n 2)))))

Output:

  1. (fib 10)
  2. ;; 55

Of course it’s not tail recursive, but it’s really cool, isn’t it?

Guards

Added a guard function to parameters:

  1. (defun funny
  2. ([(N :guard #(= 42 %))] true)
  3. ([_] false))
  4. (funny 42)
  5. ;; true
  6. (funny 43)
  7. ;; false

Another function to detect if longitude and latitude values are both valid:

  1. (defun valid-geopoint?
  2. ([(_ :guard #(and (> % -180) (< % 180)))
  3. (_ :guard #(and (> % -90) (< % 90)))] true)
  4. ([_ _] false))
  5. (valid-geopoint? 30 30)
  6. ;; true
  7. (valid-geopoint? -181 30)
  8. ;; false

Private defun

Of course, you can use defun- to define a function that is private just as defn-

More Patterns

In fact ,the above say-hi function will be expanded to be:

  1. (defn
  2. say-hi
  3. {:arglists '([& args])}
  4. [& args#]
  5. (clojure.core.match/match
  6. [(vec args#)]
  7. [[:dennis]]
  8. (do "Hi,good morning, dennis.")
  9. [[:catty]]
  10. (do "Hi, catty, what time is it?")
  11. [[:green]]
  12. (do "Hi,green, what a good day!")
  13. [[other]]
  14. (do (str "Say hi to " other))))

The argument vector is in fact a pattern in core.match, so we can use all patterns that supported by core.match.

For example, matching literals

  1. (defun test1
  2. ([true false] 1)
  3. ([true true] 2)
  4. ([false true] 3)
  5. ([false false] 4))
  6. (test1 true true)
  7. ;; 2
  8. (test1 false false)
  9. ;; 4

Matching sequence:

  1. (defun test2
  2. ([([1] :seq)] :a0)
  3. ([([1 2] :seq)] :a1)
  4. ([([1 2 nil nil nil] :seq)] :a2))
  5. (test2 [1 2 nil nil nil])
  6. ;; a2

Matching vector:

  1. (defun test3
  2. ([[_ _ 2]] :a0)
  3. ([[1 1 3]] :a1)
  4. ([[1 2 3]] :a2))
  5. (test3 [1 2 3])
  6. ;; :a2

Rest Pattern, Map Pattern, Or Pattern etc.

I don’t want to copy the core.match’s wiki,please visit it by yourself.

fun and letfun

  1. ((fun
  2. ([[_ _ 2]] :a0)
  3. ([[1 1 3]] :a1)
  4. ([[1 2 3]] :a2))
  5. [1 2 3])
  6. ;; :a2
  7. (letfun [(test3 ([[_ _ 2]] :a0)
  8. ([[1 1 3]] :a1)
  9. ([[1 2 3]] :a2))]
  10. (test3 [1 2 3]))
  11. ;; :a2

Criterium benchmarking

Uses the above function accum compared with a normal clojure function:

  1. (require '[criterium.core :refer [bench]])
  2. (defn accum-defn
  3. ([n] (accum-defn 0 n))
  4. ([ret n] (if (= n 0) ret (recur (+ n ret) (dec n)))))
  5. (defun accum-defun
  6. ([0 ret] ret)
  7. ([n ret] (recur (dec n) (+ n ret)))
  8. ([n] (recur n 0)))
  9. (bench (accum-defn 10000))
  10. ;;Evaluation count : 106740 in 60 samples of 1779 calls.
  11. ;; Execution time mean : 578.777537 µs
  12. ;; Execution time std-deviation : 23.354350 µs
  13. ;; Execution time lower quantile : 552.627735 µs ( 2.5%)
  14. ;; Execution time upper quantile : 637.001868 µs (97.5%)
  15. ;; Overhead used : 17.111650 ns
  16. (bench (accum-defun 10000))
  17. ;;Evaluation count : 54660 in 60 samples of 911 calls.
  18. ;; Execution time mean : 1.115643 ms
  19. ;; Execution time std-deviation : 32.916487 µs
  20. ;; Execution time lower quantile : 1.078117 ms ( 2.5%)
  21. ;; Execution time upper quantile : 1.180711 ms (97.5%)
  22. ;; Overhead used : 17.111650 ns

accum-defn is faster than accum-defun. Pattern matching does have a tradeoff.

Contributors

Thanks .

License

Copyright © 2023 Dennis Zhuang

Distributed under the Eclipse Public License either version 1.0 or (at

your option) any later version.