combinators.scm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix combinators)
  20. #:use-module (ice-9 match)
  21. #:use-module (ice-9 vlist)
  22. #:export (fold2
  23. fold-tree
  24. fold-tree-leaves
  25. compile-time-value))
  26. ;;; Commentary:
  27. ;;;
  28. ;;; This module provides useful combinators that complement SRFI-1 and
  29. ;;; friends.
  30. ;;;
  31. ;;; Code:
  32. (define fold2
  33. (case-lambda
  34. ((proc seed1 seed2 lst)
  35. "Like `fold', but with a single list and two seeds."
  36. (let loop ((result1 seed1)
  37. (result2 seed2)
  38. (lst lst))
  39. (if (null? lst)
  40. (values result1 result2)
  41. (call-with-values
  42. (lambda () (proc (car lst) result1 result2))
  43. (lambda (result1 result2)
  44. (loop result1 result2 (cdr lst)))))))
  45. ((proc seed1 seed2 lst1 lst2)
  46. "Like `fold', but with a two lists and two seeds."
  47. (let loop ((result1 seed1)
  48. (result2 seed2)
  49. (lst1 lst1)
  50. (lst2 lst2))
  51. (if (or (null? lst1) (null? lst2))
  52. (values result1 result2)
  53. (call-with-values
  54. (lambda () (proc (car lst1) (car lst2) result1 result2))
  55. (lambda (result1 result2)
  56. (fold2 proc result1 result2 (cdr lst1) (cdr lst2)))))))))
  57. (define (fold-tree proc init children roots)
  58. "Call (PROC NODE RESULT) for each node in the tree that is reachable from
  59. ROOTS, using INIT as the initial value of RESULT. The order in which nodes
  60. are traversed is not specified, however, each node is visited only once, based
  61. on an eq? check. Children of a node to be visited are generated by
  62. calling (CHILDREN NODE), the result of which should be a list of nodes that
  63. are connected to NODE in the tree, or '() or #f if NODE is a leaf node."
  64. (let loop ((result init)
  65. (seen vlist-null)
  66. (lst roots))
  67. (match lst
  68. (() result)
  69. ((head . tail)
  70. (if (not (vhash-assq head seen))
  71. (loop (proc head result)
  72. (vhash-consq head #t seen)
  73. (match (children head)
  74. ((or () #f) tail)
  75. (children (append tail children))))
  76. (loop result seen tail))))))
  77. (define (fold-tree-leaves proc init children roots)
  78. "Like fold-tree, but call (PROC NODE RESULT) only for leaf nodes."
  79. (fold-tree
  80. (lambda (node result)
  81. (match (children node)
  82. ((or () #f) (proc node result))
  83. (else result)))
  84. init children roots))
  85. (define-syntax compile-time-value ;not quite at home
  86. (syntax-rules ()
  87. "Evaluate the given expression at compile time. The expression must
  88. evaluate to a simple datum."
  89. ((_ exp)
  90. (let-syntax ((v (lambda (s)
  91. (let ((val exp))
  92. (syntax-case s ()
  93. (_ #`'#,(datum->syntax s val)))))))
  94. v))))
  95. ;;; combinators.scm ends here