(weitergeleitet von Main.ClojureStreams)
Clojure
FunctionalProgramming
LazyEvaluation
- Sequences in clojure are lazy, i.e. only generated as needed and as much of it as needed.
- The results are cached -> immutability even of sequences from java
(def it (iterate #(% %) 0))
(time (take 100000000000000000000 it)) ; "Elapsed time: 0.555378 msecs"
(time (take 100000000000000000000 it)) ; "Elapsed time: 0.025212 msecs"
(def whole-numbers (iterate inc 0))
(defn whole-numbers [] (iterate inc 0))
- RichHickey
- The sequence abstraction in Clojure is the list abstraction of Lisp.
- Making seqs 'non-caching' makes no sense because they are no longer the same abstraction. Lazy != ephemeral. Seqs and streams are different abstractions.
- In a world with lots of processing power and not that much bandwidth (not to mention the overhead of garbage collection and various other things) I never cache anything until I know it will provide a distinct benefit.
Streams
- http://clojure.org/streams - not cached - one by one
- Streams work
- streams are fully lazy. Seqs could be made fully lazy, but currently are not.
- Implementing SICP-style streams in Clojure
- Caching, lazy sequences, and streams
- The Adventures of a Pythonista in Schemeland/18
- functional streams: providing something like
next() would mean being state-ful. Instead something like car and cdr should be used.
Side-effects in streams are problematic.
- SRFI-41: Streams
- Streams, sometimes called lazy lists, are a sequential data structure containing elements computed only on demand. A stream is either null or is a pair with a stream in its cdr. Since elements of a stream are computed only when accessed, streams can be infinite. Once computed, the value of a stream element is cached in case it is needed again.
Streams without memoization were first described by Peter Landin in 1965. Memoization became accepted as an essential feature of streams about a decade later. Today, streams are the signature data type of functional programming languages such as Haskell.
- Purely Functional Data Structures