- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在静态语言中,我可以 replace conditional with polymorphism .
在像 erlang 这样的语言中,我可以使用模式匹配而不是 if else。
我可以在 clojure 中使用什么?
最佳答案
模式匹配和多态分派(dispatch)都可用。
多态调度的两种形式是多方法和协议(protocol)。
leontalbot 给出了一个不错的例子 多种方法根据参数的某些特定属性(可以是类型,但它们可以使用不同的调度函数)调度到特定的实现。换句话说,要决定使用哪个实现,多方法对参数执行一个函数并将其与调度表进行比较。
这是另一个例子:
; Declare multimethod:
(defmulti get-length class)
; Provide implementations for concrete types:
(defmethod get-length java.lang.String [str] (.length str))
(defmethod get-length java.lang.Number [num] (.length (str num)))
; Call it for String:
(get-length "Hello") ;=> 5
; Call it for a Number (works because Long is a subtype of Number):
(get-length 1234) ;=> 4
(defn choose-impl [in]
(cond
(is-sorted? in) :sorted
(< (count in) 10) :bubble
:else :quicksort))
(defmulti my-sort choose-impl)
(defmethod my-sort :sorted [in] in)
(defmethod my-sort :bubble [in] (my-bubble-sort in))
(defmethod my-sort :quicksort [in] (my-quicksort in))
; Protocol specification:
(defprotocol my-length (get-length [x]))
; Records can implement protocols:
(defrecord my-constant-record [value]
my-length
(get-length [x] value))
; We can "extend" existing types to support the protocol too:
(extend-protocol my-length
java.lang.String
(get-length [x] (.length x))
java.lang.Long
(get-length [x] (.length (.toString x))))
; Now calling get-length will find the right implementation for each:
(get-length (my-constant-record. 15)) ;=> 15
(get-length "123") ;=> 3
(get-length 1234) ;=> 4
(doseq [n (range 1 101)]
(println
(match [(mod n 3) (mod n 5)]
[0 0] "FizzBuzz"
[0 _] "Fizz"
[_ 0] "Buzz"
:else n)))
关于clojure - 模式匹配或多态分派(dispatch)是否可用作 clojure 中的条件结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24680322/
在使用 dispatch 更新 useReducer 状态后,我需要能够立即执行操作。但是 dispatch 运行异步所以当我运行我的下一段代码时,它在 dispatch 应该更新它之前使用旧状态。
我是 Redux 新手。我必须调度一个操作来更新应用程序的状态,然后使用更新状态来调用我的后端。我使用 thunkMiddleware。 const mapDispatchToProps = disp
我是 Redux 新手。我必须调度一个操作来更新应用程序的状态,然后使用更新状态来调用我的后端。我使用 thunkMiddleware。 const mapDispatchToProps = disp
当我单击 InspectorOption 组件之一时,我的 redux 记录器显示已分派(dispatch)操作并且状态按预期更新。 我的 InspectorSelect 和子 InspectorOp
我有一个模块,其中包含一组函数,实现为带有辅助函数的分派(dispatch)哈希: my $functions = { 'f1' => sub { my %args = @_;
Apple 的 GCD 文档说明如下: GCD provides and manages FIFO queues to which your application can submit tasks
所以我正在获取此 UIImage 数据并将其转换为 base64 中的字符串。问题是它在转换时卡在 UI 线程上,我不确定为什么。 - (void)processImage:(UIImage*)ima
我有一个从后台线程调用的函数 func getValue() -> Bool。这是有意的,也是必须的。现在,getValue() 需要在主线程上执行一些操作,在这种情况下它需要访问 UIApplica
我有一个带有表单的组件,可以将项目添加到列表中。成功将项目添加到列表后,我想使用 form.resetForm(); ,但我想不出一个简单的方法来知道该操作何时成功。我希望我可以订阅 Action 调
我正在努力在 Laravel 6.x 中使用 cursor() 方法获取 3M+ 记录时降低内存。 我有一个 artisan 命令运行以下代码: Product::cursor()->each(fun
好的,所以我一直在尝试通过并发编辑设置电子表格应用程序。我走了laravel回声,redis,套接字路由。 (任何有关仅使用推动器的建议都会被驳回)。现在大多数情况下,我已经开始工作了,我可以从修补匠
假设我有这个布局 span 我将无处不在的点击转换为自定义事件,并使用委托(delegate)的非捕获处理程序将其分派(dispatch)到其原始目标: document.ad
页面加载后,我将在我的 index.js 中调度一个操作 store.dispatch(getWeatherReports()); 来访问天气 API。此操作通过 redux 过程,最终将返回的数据添
我有一个抽象父类 Parent 和六个子类 ChildA though ChildF。 另一个类 Other 有六个(静态)重载方法 olmeth(),六个子类中的每一个。 我怎么写: Parent
这里描述了类似的问题:GWT IllegalArgumentException: encodedRequest cannot be empty 我的GWT应用程序部署在Tomcat6中,该Tomcat
我正在尝试通过 iOS 上的 GCD 将一些代码分派(dispatch)到主队列,但即使是最简单的测试也总是失败。最后归结为: static const int TICK_INTERVAL = 1;
在某些情况下,覆盖扩展中的方法签名似乎会产生不可预知的结果。以下示例演示了具有相似模式的两个不同结果。 class A: UIViewController { func doThing() {
这个问题在这里已经有了答案: Is self retained within this Objective-C block? (1 个回答) 8年前关闭。 假设我有一个简单的电话 dispatch_a
React 中的上下文和 reducers 非常新。我目前正在尝试使用 Context 从折线图上的事件中获取日期字符串。我使用的折线图来自 react-chartjs-2。 我的上下文已设置并提供如
我有一个项目,其中nodejs服务器通过socket.io将推送事件传递到react仪表板,我正在使用Redux。当收到新数据时,会触发一个操作来更新所有相关组件,尽管我不确定我这样做的方式是否正确。
我是一名优秀的程序员,十分优秀!