继续在Clojure [英] Continuations in Clojure
问题描述
我读的地方有丰富的hickey说:
I read somewhere where rich hickey said:
我认为续约在理论上可能是整洁的
,练习
"I think continuations might be neat in theory, but not in practice"
我不熟悉clojure。
1. clojure有延续吗?
2.如果不是,你不需要继续吗?我看到很多很好的例子,特别是来自这个家伙。什么是替代方法?
3.如果是,是否有文档?
I am not familiar with clojure.
1. Does clojure have continuations?
2. If no, don't you need continuations? I have seen a lot of good examples especially from this guy. What is the alternative?
3. If yes, is there a documentation?
推荐答案
你必须区分两种不同的类型:
When talking about continuations, you’ll have to distinguish between two different kinds of them:
-
一类继承 - 继续支持深深地融入了语言(Scheme或Ruby)。
First-class continuations – Continuation-support that is deeply integrated in the language (Scheme or Ruby). Clojure does not support first-class continuations.
继续传递风格(CPS) - CPS只是一种编码风格,任何支持匿名函数的语言允许这种风格(也适用于Clojure)。
Continuation-passing-style (CPS) – CPS is just a style of coding and any language supporting anonymous functions will allow this style (which applies to Clojure too).
示例:
-- Standard function
double :: Int -> Int
double x = 2 * x
-- CPS-function – We pass the continuation explicitly
doubleCPS :: Int -> (Int -> res) -> res
doubleCPS x cont = cont (2 * x)
; Call
print (double 2)
; Call CPS: Continue execution with specified anonymous function
double 2 (\res -> print res)
$ b b
阅读维基百科上的续篇。
我不认为继续对于一个好的语言是必要的,但是特别是在Haskell这样的功能语言中的第一类继承和CPS可能是非常有用的(智能回溯示例)。
I don’t think that continuations are necessary for a good language, but especially first-class continuations and CPS in functional languages like Haskell can be quite useful (intelligent backtracking example).
这篇关于继续在Clojure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!