任何优点,懒惰的juxt函数? [英] Any merit to a lazy-ish juxt function?

查看:112
本文介绍了任何优点,懒惰的juxt函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回答关于映射多个函数的函数的问题时,相同的参数(A:juxt),我想出了一个基本上采用与juxt相同的形式,但使用map的函数:

In answering a question about a function that maps over multiple functions with the same arguments (A: juxt), I came up with a function that basically took the same form as juxt, but used map:

(defn could-be-lazy-juxt
  [& funs]
  (fn [& args]
    (map apply funs (repeat args))))

=> ((juxt inc dec str) 1)
[2 0 "1"]
=> ((could-be-lazy-juxt inc dec str) 1)
(2 0 "1")

=> ((juxt * / -) 6 2)
[12 3 4]
=> ((could-be-lazy-juxt * / -) 6 2)
(12 3 4)


$ b b

我对于懒惰或性能没有多少线索,但是REPL中的时间确实暗示着懒惰的行为。

I have little clue about the laziness or performance of it, but timing in the REPL does suggest something lazy-ish is going on.

=> (time (apply (juxt + -) (range 1 100)))
"Elapsed time: 0.097198 msecs"
[4950 -4948]
=> (time (apply (could-be-lazy-juxt + -) (range 1 100)))
"Elapsed time: 0.074558 msecs"
(4950 -4948)

=> (time (apply (juxt + -) (range 10000000)))
"Elapsed time: 1019.317913 msecs"
[49999995000000 -49999995000000]
=> (time (apply (could-be-lazy-juxt + -) (range 10000000)))
"Elapsed time: 0.070332 msecs"
(49999995000000 -49999995000000)

我相信这个函数并不是那么快速(结果的打印在两者中都有效)。对函数做一个'take x'只限制被评估的函数的数量,这可能限制了它的适用性,并且通过'take'限制其他参数应该像在正常juxt中一样迟钝。

I'm sure this function is not really that quick (the print of the outcome 'feels' about as long in both). Doing a 'take x' on the function only limits the amount of functions evaluated, which probably is limited in it's applicability, and limiting the other parameters by 'take' should be just as lazy in normal juxt.

这个juxt真的懒吗?一个懒惰的juxt对表有什么用处,例如作为其他懒惰函数之间的合成步骤?什么是性能(mem / cpu /对象计数/编译)的影响?
为什么Clojure juxt实现了一个reduce并返回一个向量,打破了懒惰?

Is this juxt really lazy ? Would a lazy juxt bring anything useful to the table, for instance as a compositing step between other lazy functions ? What are the performance (mem / cpu / object count / compilation) implications ? Why is the Clojure juxt implemented with a reduce and returns a vector, which breaks laziness ?

推荐答案

juxt是懒惰的,因为它只是调用map是懒惰的。

Yes your implementation of juxt is lazy by virtue of only calling map which is lazy.

很难调用它的现在形式与实现调用者的参数。所以我改变了一点,以采取一系列的功能:

it's hard to call it in it's present form with out realizing the arguments in the caller (by using apply. so I changed it a bit to take a sequence of functions:

user> (defn could-be-lazy-juxt
  [funs]
  (fn [& args]
    (map #(apply %1 %2) funs (repeat args))))
#'user/could-be-lazy-juxt

然后定义一个惰性的函数序列, / p>

then define a lazy sequence of functions that make a lot of output when it's realized

user> (defn loud-seq [len] (take len (map #(do (println "produced a function") %) (cycle [inc dec]))))
#'user/loud-seq

然后使用juxt从这个惰性函数序列中创建一个函数

then use juxt to make a function out of this lazy sequence of functions

user> (def f (could-be-lazy-juxt (loud-seq 50)))
#'user/f

你可以看到列表仍然是惰性的,它实现它的函数列表,当它的结果函数被调用。

as you can see the list is still lazy, it realizes it's list of functions when it's resulting function is called.

所以让它调用它:

user> (f 1)
(produced a function
produced a function
2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 produced a function
produced a function
0 2 0)
user> 

我将这样做的理由留给你;)

I leave the reasons for doing so up to you ;)

这篇关于任何优点,懒惰的juxt函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆