multimethod

What are the reasons that protocols and multimethods in Clojure are less powerful for polymorphism than typeclasses in Haskell?

◇◆丶佛笑我妖孽 提交于 2019-12-29 04:30:18
问题 More broadly this question is about various approaches to the expression problem. The idea is that your program is a combination of a datatype and operations over it. We want to be able to add new cases without recompiling the old classes. Now Haskell has some really awesome solutions to the expression problem with the TypeClass. In particular - we can do: class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool member :: (Eq a) => a -> [a] -> Bool member y [] = False member y (x:xs) =

flask-restful having a get/<id> and post with json in the same class

一曲冷凌霜 提交于 2019-12-22 10:27:04
问题 The get method on user works if the # api.add_resource(User, '/user/') line is uncommented, and the other api.add_resource is. The inverse of that is true to make the post method work. How can I get both of these paths to work? from flask import Flask, request from flask.ext.restful import reqparse, abort, Api, Resource import os # set the project root directory as the static folder, you can set others. app = Flask(__name__) api = Api(app) class User(Resource): def get(self, userid): print

Clojure - dispatch on return type? (As expressive as Haskell Typeclasses)

∥☆過路亽.° 提交于 2019-12-22 04:26:19
问题 This is a question about the expressiveness of Clojure vs other languages such as Haskell. The broader issue is solutions to the Expression Problem This question reached the conclusion that in general Clojure protocols (and multimethods) were less expressive than Haskell typeclasses because protocols dispatched on first argument, and Haskell typeclasses could dispatch on return type. (Now I think this reasoning is really interesting, and have no interest in starting a language war. I'm just

How does a Perl 6 object find a multi method that might be in a parent class or role?

…衆ロ難τιáo~ 提交于 2019-12-22 03:24:27
问题 Consider this example where a subclass has a multi method with no signature and one with a slurpy parameter: class Foo { multi method do-it { put "Default" } multi method do-it ( Int $n ) { put "Int method" } multi method do-it ( Str $s ) { put "Str method" } multi method do-it ( Rat $r ) { put "Rat method" } } class Bar is Foo { multi method do-it { put "Bar method" } multi method do-it (*@a) { put "Bar slurpy method" } } Foo.new.do-it: 1; Foo.new.do-it: 'Perl 6'; Foo.new.do-it: <1/137>; Foo

Can I use Clojure's derive to create a hierarchy of my defrecord class types?

青春壹個敷衍的年華 提交于 2019-12-10 13:32:02
问题 I would like to do something like: (defrecord Base []) (defrecord Person []) (defrecord Animal []) (derive Person Base) (derive Animal Base) (isa? Animal Person) Is this possible? Update: I've since realized that this is not possible so I am doing something like this: (defmulti type class) (defmethod type Base [_] ::base ) (defmethod type Animal [_] ::animal ) (defmethod type Person [_] ::person ) Does this make sense or is there a better way? 回答1: No. Records are Java classes. As the

How to set and get multimethod metadata in clojure?

随声附和 提交于 2019-12-09 01:40:34
问题 I'm using multimethods to parse command line commands and their arguments. (defmulti run (fn [command args] command)) (defmethod run :default [& _] ...) ^{:args "[command]"} (defmethod run "help" [_ & [args]] "Display command list or help for a given command" ...) ^{:args ""} (defmethod run "version" [_ & [args]] "Print program's version" ...) (defn -main [& args] (run (first args) (next args))) When I try to access the metadata, for a specific method, clojure returns nil : (meta ((methods

why there are no multimethods in c++? [closed]

筅森魡賤 提交于 2019-12-06 19:49:16
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 8 years ago . I read many article about how to implement multimethod in c++: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1529.html http://www.codeproject.com/KB/recipes/mmcppfcs.aspx http://lambda-the-ultimate.org

Why are Clojure's multimethods better than 'if' or 'case' statements

99封情书 提交于 2019-12-06 17:10:51
问题 I've spent some time, trying to understand Clojure multimethods. The main "pro" multimethod argument, as far as I understand, is their flexibility, however, I'm confused with the argumentation of why multimethods are better than a simple if or case statement. Could someone, please, explain, where is the line between polymorphism and an overglorified case statement drawn? EDIT: I should have been clearer in the question, that I'm more interested in comparison with the 'if' statement. Thanks a

flask-restful having a get/<id> and post with json in the same class

前提是你 提交于 2019-12-05 22:37:15
The get method on user works if the # api.add_resource(User, '/user/') line is uncommented, and the other api.add_resource is. The inverse of that is true to make the post method work. How can I get both of these paths to work? from flask import Flask, request from flask.ext.restful import reqparse, abort, Api, Resource import os # set the project root directory as the static folder, you can set others. app = Flask(__name__) api = Api(app) class User(Resource): def get(self, userid): print type(userid) if(userid == '1'): return {'id':1, 'name':'foo'} else: abort(404, message="user not found")

Clojure - dispatch on return type? (As expressive as Haskell Typeclasses)

元气小坏坏 提交于 2019-12-05 02:42:56
This is a question about the expressiveness of Clojure vs other languages such as Haskell. The broader issue is solutions to the Expression Problem This question reached the conclusion that in general Clojure protocols (and multimethods) were less expressive than Haskell typeclasses because protocols dispatched on first argument, and Haskell typeclasses could dispatch on return type. (Now I think this reasoning is really interesting, and have no interest in starting a language war. I'm just interested in clarity of thought). As part of breaking this reasoning down - my question is - can't we