├── .fdignore ├── LICENSE ├── README.md ├── multimethod.janet ├── project.janet └── test └── multimethod.janet /.fdignore: -------------------------------------------------------------------------------- 1 | .git 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Jon Staab 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # janet-multimethod 2 | 3 | A multimethod library for Janet 4 | 5 | # Disclaimer 6 | 7 | This is pre-alpha software. Please open an issue if you'd like to use it in a project. 8 | -------------------------------------------------------------------------------- /multimethod.janet: -------------------------------------------------------------------------------- 1 | # Get-meta is a sentinel value that allows us to return meta 2 | (def- $get-meta (gensym)) 3 | 4 | (defn multimethod 5 | [dispatch] 6 | (let [methods @{} 7 | meta {:dispatch dispatch :methods methods}] 8 | (fn [& args] 9 | (if 10 | (= $get-meta (first args)) 11 | meta 12 | (let [method (or (methods (dispatch ;args)) (methods :default))] 13 | (if method 14 | (method ;args) 15 | (error (string/format "No method found for dispatch values: %q" args)))))))) 16 | 17 | (defmacro defmulti 18 | [name & opts] 19 | (let [[docstring dispatch] (if (string? (first opts)) opts [(string name) ;opts])] 20 | ~(def ,name ,docstring (multimethod ,dispatch)))) 21 | 22 | (defn add-method 23 | [mm k f] 24 | (put ((mm $get-meta) :methods) k f)) 25 | 26 | (defmacro defmethod 27 | [mm k & body] 28 | ~(add-method ,mm ,k (fn ,;body))) 29 | -------------------------------------------------------------------------------- /project.janet: -------------------------------------------------------------------------------- 1 | (declare-project 2 | :name "janet-multimethod" 3 | :description "A multimethod library for Janet" 4 | :dependencies ["https://github.com/staab/janet-assert"]) 5 | 6 | (declare-source 7 | :source ["multimethod.janet"]) 8 | -------------------------------------------------------------------------------- /test/multimethod.janet: -------------------------------------------------------------------------------- 1 | (use assert) 2 | (use multimethod) 3 | 4 | (defmulti thing |($ :x)) 5 | 6 | (defmethod thing 1 [{:x x}] (inc x)) 7 | (defmethod thing 2 [{:x x}] (dec x)) 8 | 9 | (assert= 2 (thing {:x 1})) 10 | (assert= 1 (thing {:x 2})) 11 | 12 | (defmulti thing "stuff" |($ :x)) 13 | 14 | (defmethod thing 1 [{:x x}] (inc x)) 15 | (defmethod thing 2 [{:x x}] (dec x)) 16 | 17 | (assert= 2 (thing {:x 1})) 18 | (assert= 1 (thing {:x 2})) 19 | --------------------------------------------------------------------------------