任意Clojure形式的随机替换函数

问题描述 投票:0回答:1

给定Clojure表达式中的任意形式,我想设计一个函数,该函数返回随机选择的相同类型签名的形式。

该函数,我将其命名为rand-form-sub,其行为应如下所示:

(rand-form-sub 2) => 8
(rand-form-sub 'or) => and 
  ;; both and and or obey the same haskellian type signature:
  ;; f :: a -> a -> bool, where as are comparable for equality
(rand-form-sub '+) => - ;; both + and - obey f :: int -> int -> int

我怀疑该解决方案涉及1)将函数的范围限制为Clojure的子集(例如,标准库)2)最高级别的cond结构3)表示语义上有效替换的数组映射。

最后,我知道Clojure缺少类型签名可能存在限制,但我愿意妥协以找到可行的最佳方法。

如果Clojure没有类型签名,我怎样才能找到任意Clojure表单的等效类型签名的形式?

注意:请随意编辑以纠正我的帖子中任何滥用行话的行为:我不确定我是否正确使用术语表格。

clojure
1个回答
0
投票

不是直接的答案,但你可能会在clojure.spec生成器中找到灵感:https://clojure.org/guides/spec#_generators

© www.soinside.com 2019 - 2024. All rights reserved.