combinators.scm 3.7 KB

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