1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: pipes-examples.lisp
6 ;;;; Purpose: Pipe examples
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Apr 2000
10 ;;;; $Id: pipes-example.lisp,v 1.1 2002/11/02 17:49:10 kevin Exp $
12 ;;;; This file, part of pipes, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; *************************************************************************
16 (in-package #:pipes-user)
19 (defun integers (&optional (start 0) end)
20 "a pipe of integers from START to END."
21 (if (or (null end) (<= start end))
22 (make-pipe start (integers (+ start 1) end))
26 (make-pipe a (fibgen b (+ a b))))
32 (defun divisible? (x y)
37 (pipe-filter #'(lambda (x) (not (divisible? x 7))) (integers)))
45 (not (divisible? x (pipe-head stream))))
46 (pipe-tail stream)))))
54 (defun scale-pipe (factor pipe)
55 (pipe-map #'(lambda (x) (* x factor)) pipe))
57 (defun sum-pipe (sum s)
59 (sum-pipe (+ sum (pipe-head s))
62 (defun partial-sums (s)
63 (make-pipe (pipe-head s) (sum-pipe 0 s)))
65 (defun pi-summands (n)
67 (pipe-map #'- (pi-summands (+ n 2)))))
70 (scale-pipe 4d0 (partial-sums (pi-summands 1))))
75 (defun euler-transform (s)
76 (let ((s0 (pipe-elt s 0))
80 (if (eql s1 s2) ;;; series has converged
82 (make-pipe (- s2 (/ (square (- s2 s1))
84 (euler-transform (pipe-tail s))))
88 (defun ln2-summands (n)
90 (pipe-map #'- (ln2-summands (1+ n)))))
93 (partial-sums (ln2-summands 1)))
95 (defun make-tableau (transform s)
97 (make-tableau transform
98 (funcall transform s))))
100 (defun accelerated-sequence (transform s)
101 (pipe-map #'pipe-head
102 (make-tableau transform s)))
105 (defun run-examples ()
106 (let ((*print-length* 20))
107 (format t "~&pi-stream: ~S"
108 (pipe-display (pi-stream) 10))
109 (format t "~&euler-transform: ~S"
110 (pipe-display (euler-transform (pi-stream)) 10))
111 (format t "~&accelerate-sequence: ~S"
113 (accelerated-sequence #'euler-transform (pi-stream)) 10))))