filter(3scm)

πŸ“¦ version 0-unstable-2024-02, in nixpkgs unstable

✨ filter(3scm) β€” filter a list ✨

Okay so bestie, this is filter(3scm), the manual page for filter and partition β€” two procedures that are literally the main characters of list filtering. No cap. πŸ’…

πŸ“š LIBRARY

You can import them from either of these, depending on your setup:

(import (rnrs))                     ;R6RS
(import (rnrs lists))               ;R6RS

🧾 SYNOPSIS

(filter proc list)
(partition proc list)

πŸ’– DESCRIPTION

filter returns a list containing the elements in list for which proc returned a true value. It's giving "only the good ones make the cut." ✨

partition is exactly like filter, except it also returns a second list containing the elements that were eliminated by proc. So you get both the yeses and the nos. Iconic.

The elements of the returned lists are in the same order as they appear in list. The proc procedure is called in an unspecified order. (So like, don't assume any order, bestie. πŸ™ˆ)

Proc should accept one argument and return a single value. Proc should not mutate list. Proc is always called in the same dynamic environment as filter or partition itself. That's a key detail, iykyk. πŸ’œ

🎁 RETURN VALUES

filter returns a single list. partition returns two lists. (Yes, two values. Not one. Two.) πŸ’…

If this procedure returns twice (e.g. by use of call/cc) then the lists returned earlier are not mutated. No cap.

🌟 EXAMPLES

(filter even? '(3 1 4 1 5 9 2 6))
           => (4 2 6)
(partition even? '(3 1 4 1 5 9 2 6))
           => (4 2 6) (3 1 1 5 9) ; two values

πŸ› οΈ APPLICATION USAGE

These procedures are common in functional code that works with lists. If you're doing functional stuff, you'll literally see them everywhere. Periodt. πŸ”₯

πŸ”„ COMPATIBILITY

These procedures are absent from reports other than R6RS. See SRFI-1 for alternatives. So if you're on a different report, check SRFI-1, okay? πŸ¦‹

⚠️ ERRORS

This procedure can raise exceptions with the following condition types:

  • &assertion (R6RS) β€” The wrong number of arguments was passed or an argument was outside its domain. Examples of this are a procedure proc that does not accept and return a single value, or a list which is circular or improper. πŸ’€
So like, if your proc is weird or your list is circular or improper, expect an exception. Be careful. 🫢

πŸ‘€ SEE ALSO

map(3scm), remp(3scm)

πŸ“ STANDARDS

R6RS

✍️ AUTHORS

This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://github.com/schemedoc/manpages/. 🧚

πŸ› BUGS

The procedure proc should avoid side-effects, because the order of calls is unspecified. So like, don't be messy with side effects, bestie. It's giving "undefined order" energy. πŸ’…


That's the whole vibe check, bestie. This page is from the scheme-manpages project, and this particular doc is dated 2021-03-03. Slay. πŸ’–βœ¨

πŸ“¦ where this page comes from