git @ Cat's Eye Technologies Robin / master stdlib / take-while.robin
master

Tree @master (Download .tar.gz)

take-while.robin @masterraw · history · blame

;'<<SPEC'

<!--
Copyright (c) 2012-2024, Chris Pressey, Cat's Eye Technologies.
This file is distributed under a 2-clause BSD license.  See LICENSES/ dir.
SPDX-License-Identifier: LicenseRef-BSD-2-Clause-X-Robin
-->

### `take-while` ###

    -> Tests for functionality "Evaluate Robin Expression (with List)"

`take-while` evaluates its first argument to obtain a predicate and its
second argument to obtain a list.  It then evaluates to the longest prefix
of the list whose elements all satisfy the predicate.

    |   (take-while (fun (x) (symbol? x)) (literal (one two 3 4 five 6 seven)))
    = (one two)

    |   (take-while (fun (x) (symbol? x)) (literal (1 2 3 4 five six)))
    = ()

    |   (take-while (fun (x) (number? x)) (literal (1 2 3 4 5 6)))
    = (1 2 3 4 5 6)

    |   (take-while (fun (x) (symbol? x)) ())
    = ()

    |   (take-while (fun (x) (symbol? x)) #f)
    ? abort (expected-list #f)

'<<SPEC'

(define take-while (fun (pred li)
  (bind take-while-r (fun (self pred li)
    (if (empty? li)
      ()
      (if (pred (head li))
        (prepend (head li) (self self pred (tail li)))
        ())))
    (take-while-r take-while-r pred li))))