practicalli / practicalli/clojure
Polymorphism in Clojure
Nobody has claimed this yet.
- Dominant language
- Makefile
- Stars
- 117
- Forks
- 36
- PR merge metrics
- No merged PRs in 30d
Description
ots of ways to do polymorphism in clojure. One example would be
(defmulti area :shape)
(defmethod area :circle [{:keys [radius]}] (* Math/PI radius radius))
(defmethod area :square [{:keys [length]}] (* length length))(area {:shape :circle :radius 3})
3 replies
phronmophobic 1 day ago
@Travis Jungroth, this is also possible with protocols:
(defprotocol IArea
(area [this]))(defrecord Circle [radius]
IArea
(area [this]
(* Math/PI radius radius)))(defrecord Square [length]
IArea
(area [this]
(* length length)))(area (->Circle 2)) ;; 12.566370614359172
(area (->Square 2)) ;; 4
Clojure has pretty flexible dispatch mechanisms. You may be interested in these articles that talk about the CS theory as well:
https://en.wikipedia.org/wiki/Dynamic_dispatch
https://en.wikipedia.org/wiki/Multiple_dispatch (edited)
WikipediaWikipedia
Dynamic dispatch
In computer science, dynamic dispatch is the process of selecting which implementation of a polymorphic operation (method or function) to call at run time. It is commonly employed in, and considered a prime characteristic of, object-oriented programming (OOP) languages and systems.Object-oriented systems model a problem as a set of interacting objects that enact operations referred to by name. Polymorphism is the phenomenon wherein somewhat interchangeable objects each expose an operation of the same name but possibly differing in behavior. As an example, a File object and a Database object both have a StoreRecord method that can be used to write a personnel record to storage. Their implemen…
WikipediaWikipedia
Multiple dispatch
Multiple dispatch or multimethods is a feature of some programming languages in which a function or method can be dynamically dispatched based on the run time (dynamic) type or, in the more general case, some other attribute of more than one of its arguments. This is a generalization of single dispatch polymorphism where a function or method call is dynamically dispatched based on the derived type of the object on which the method has been called. Multiple dispatch routes the dynamic dispatch to the implementing function or method using the combined characteristics of one or more arguments.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue provides Clojure examples using multimethods and protocols, but names no file, test, or specific documentation change. First clarify the intended scope and location in the repository; it is done only when the desired polymorphism content and its documentation placement are agreed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- clojure
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100