Lazy-sequences
Lazy (evaluated) sequences are sequences whose elements are generated on demand. Many languages have them built in or available as libraries.
If you don't know what this is then here is an example:
(take 5 (range :from 100))
(100 101 102 103 104)
take
takes the first 5 elements from the a generator range
which starts counting at 100. Each 'take' makes the range
generator compute a new value rather then computing 5 elements up-front.
That's why it is called 'lazy'. The elements of the sequence are computed when needed. In a very simple form lazy evaluated sequences can be implemented using a generator that we call range
and a set of consumers, like take
. The generator can be implemented in a stateful way using a 'let over lambda', like this:
(defun range (&key (from 0))
(let ((n from))
(lambda () (prog1
n
(incf n)))))
The range
function returns a lambda which has bound the n
variable (this is also called 'closure'). When we now call the the lambda function it will return n
and increment as a last step. (The prog1
form returns the first element and continues to evaluate the rest)
So we can formulate a take
function like this:
(defun take (n gen)
(loop :repeat n
:collect (funcall gen)))
take
has two arguments, the number of elements to 'take' and the generator, which is our lambda from range
. This is a very simple example but effectively this is how it works.
If you are looking for good libraries for Common Lisp then I can recommend the following two:
-
[Polymorphism and Multimethods]
02-03-2023 -
[Global Day of CodeRetreat - recap]
07-11-2022 -
[House automation tooling - Part 4 - Finalized]
01-11-2022 -
[House automation tooling - Part 3 - London-School and Double-Loop]
02-07-2022 -
[Modern Programming]
14-05-2022 -
[House automation tooling - Part 2 - Getting Serial]
21-03-2022 -
[House automation tooling - Part 1 - CL on MacOSX Tiger]
07-03-2022 -
[Common Lisp - Oldie but goldie]
18-12-2021 -
[Functional Programming in (Common) Lisp]
29-05-2021 -
[Patterns - Builder-make our own]
13-03-2021 -
[Patterns - Builder]
24-02-2021 -
[Patterns - Abstract-Factory]
07-02-2021 -
[Lazy-sequences - part 2]
13-01-2021 -
[Lazy-sequences]
07-01-2021 -
[Thoughts about agile software development]
17-11-2020 -
[Test-driven Web application development with Common Lisp]
04-10-2020 -
[Wicket UI in the cluster - the alternative]
09-07-2020 -
[TDD - Mars Rover Kata Outside-in in Common Lisp]
03-05-2020 -
[MVC Web Application with Elixir]
16-02-2020 -
[Creating a HTML domain language in Elixir with macros]
15-02-2020 -
[TDD - Game of Life in Common Lisp]
01-07-2019 -
[TDD - classicist vs. London Style]
27-06-2019 -
[Wicket UI in the cluster - reflection]
10-05-2019 -
[Wicket UI in the Cluster - know how and lessons learned]
29-04-2019 -
[TDD - Mars Rover Kata classicist in Scala]
23-04-2019 -
[Burning your own Amiga ROMs (EPROMs)]
26-01-2019 -
[TDD - Game of Life in Clojure and Emacs]
05-01-2019 -
[TDD - Outside-in with Wicket and Scala-part 2]
24-12-2018 -
[TDD - Outside-in with Wicket and Scala-part 1]
04-12-2018 -
[Floating Point library in m68k Assembler on Amiga]
09-08-2018 -
[Cloning Compact Flash (CF) card for Amiga]
25-12-2017 -
[Writing tests is not the same as writing tests]
08-12-2017 -
[Dependency Injection in Objective-C... sort of]
20-01-2011