在Clojure中,何时以及为什么要使用命名的匿名函数?例如,
((fn add-five [x] (+ x 5)) 3))
Run Code Online (Sandbox Code Playgroud)
在ClojureDocs中,一个例子的评论说它在堆栈跟踪中很有用.可以举个例子吗?
在给定列表中对所有组合的乘积求和的Pythonic方法是什么,例如:
[1, 2, 3, 4]
--> (1 * 2) + (1 * 3) + (1 * 4) + (2 * 3) + (2 * 4) + (3 * 4) = 35
Run Code Online (Sandbox Code Playgroud)
(对于这个例子,我已经采用了所有的两元素组合,但它可能有所不同.)
我可以理解以下定义:
每个对象都有一个标识,一个类型和一个值。一旦创建了对象,其身份就不会改变。您可能会认为它是对象在内存中的地址。所述
is
操作者比较两个对象的身份; 该id()
函数返回一个表示其身份的整数。
我认为上面的定义在创建“某物”时起作用,例如:
>>> a = 0
>>> id(a)
1720438480
Run Code Online (Sandbox Code Playgroud)
但是我不理解:
>>> id(1)
1720438512
>>> b = 1
>>> id(b)
1720438512
Run Code Online (Sandbox Code Playgroud)
我还没有创建任何东西。那么整数“ 1”如何具有ID?这是否意味着只要我在Python Shell中“提及” 1,便立即将其分配给内存地址?另外,这是否意味着因为ID在其生命周期内从未发生变化,并且由于我的计算机内存有限,所以如果我反复询问唯一事物的id(),我最终会得到类似“内存不足”的消息吗?(它不能重新分配内存,因为其他对象的生命期尚未结束。)
或者,从相反的方向显示我的耳朵:
>>> id(something_1)
some unique memory address
>>> id(something_2)
some unique memory address
>>> ...
Run Code Online (Sandbox Code Playgroud)
在什么时候重新分配内存?那就是在那个时候
>>> my_variable = something_1
>>> id(my_variable)
Run Code Online (Sandbox Code Playgroud)
会给出一个与id(something_1)
?不同的ID ?
假设一个发生器逐个产生以下元组(从左到右)
(1, 2, 3), (2, 5, 6), (3, 7, 10), (4, 5, 11), (3, 5, 15), (4, 5, 9), (4, 6, 12)
...
Run Code Online (Sandbox Code Playgroud)
假设只要谓词为真,我就想迭代.让那个谓词成为sum(yielded_value) < 20
.然后迭代器将停止(3, 5, 15)
.我可以这样做,说:
list(itertools.takewhile(lambda x: sum(x) < 20, some_generator()))
Run Code Online (Sandbox Code Playgroud)
问题,如何用两个谓词编写类似的表达式?假设我想:
list(itertools.takewhile(lambda x: sum(x) < 20 and first_value_of_tuple > 3, some_generator()))
Run Code Online (Sandbox Code Playgroud)
(在这种情况下,停在(4, 6, 12)
.)
list_ = [(1, 2), (3, 4)]
Run Code Online (Sandbox Code Playgroud)
什么是从内元组中取有序对的总和并乘以和的Pythonic方法?对于上面的例子:
(1 + 3) * (2 + 4) = 24
Run Code Online (Sandbox Code Playgroud) 在 Clojure 中,是否有一些通用函数(类似于高阶函数map
, filter
, reduce
,例如,以具有一个元素的向量开始,将函数应用于f
元素,将元素添加到向量,然后将函数应用于新元素,并继续这样直到满足某些条件。
示例:我有 vector [1]
、 functiondouble
和我所追求的函数,将其称为f
,并且类似于:
(take-while some-pred (f double 1))
Run Code Online (Sandbox Code Playgroud)
它产生: [1 2 4 8 16 32 . . .]
为什么要学习Clojure,我有时需要看看每个步骤的功能是什么.例如:
(defn kadane [coll]
(let [pos+ (fn [sum x] (if (neg? sum) x (+ sum x)))
ending-heres (reductions pos+ 0 coll)]
(reduce max ending-heres)))
Run Code Online (Sandbox Code Playgroud)
我应该println
在这里和那里插入(在哪里,如何); 或者是否有建议的工作流程/工具?
几本书(或教程)按以下方式定义了一张卡片和一副牌:
\n\nimport random\n\nclass Card(object):\n """ A card object with a suit and rank."""\n\n RANKS = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)\n\n SUITS = (\'Spades\', \'Diamonds\', \'Hearts\', \'Clubs\')\n\n def __init__(self, rank, suit):\n """Creates a card with the given rank and suit."""\n self.rank = rank\n self.suit = suit\n\n def __str__(self):\n """Returns the string representation of a card."""\n if self.rank == 1:\n rank = \'Ace\'\n elif self.rank == 11:\n rank = \'Jack\'\n elif self.rank == …
Run Code Online (Sandbox Code Playgroud) 以下工作:
((resolve (symbol "first")) [1 2 3])
;; => 1
Run Code Online (Sandbox Code Playgroud)
为什么认为是错误的,
((read-string "first") [1 2 3])
;; => nil
Run Code Online (Sandbox Code Playgroud)
应该工作,但它没有?(我明白了nil
。)
我的尝试:
(defn inc-by-f [v]
map #(+ (first v) %) v)
Run Code Online (Sandbox Code Playgroud)
(最初的问题很愚蠢;我错过了括号。我还在留下这个问题,以便我学习一些新的方法来处理它。)
(defn inc-by-f [v]
(map #(+ (first v) %) v))
Run Code Online (Sandbox Code Playgroud)
还有什么其他很酷的“Clojure”方法可以达到预期的效果?
clojure ×5
python ×5
python-3.x ×5
class ×1
collections ×1
debugging ×1
identity ×1
list ×1
namedtuple ×1