Created
June 20, 2026 16:21
-
-
Save hinjolicious/3b044f64744fabd851859832db1d9275 to your computer and use it in GitHub Desktop.
FILTER - Filter a list based on a condition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Red [ | |
| title: "FILTER - Filter a list based on a condition" | |
| author: "hinjolicious" | |
| usage: { | |
| a: [1 2 3 4 5 6 7 8 9 10] | |
| print mold filter a [[x] x % 2 = 0] | |
| a: [[1 2] [3 6] [5 6] [7 14] [9 10]] | |
| print mold filter a [[x y] (x * 2) = y] | |
| print mold a || [[x y] (x * 2) = y] | |
| } | |
| resource: "Gemini AI" | |
| ] | |
| ; version 4 | |
| FILTER: function [x [series!] f [block!]] [ | |
| a: f/1 ; arguments must present and at least has one | |
| ff: function a next f ; construct a func | |
| either (length? a) = 1 [ ; one arg | |
| collect [foreach e x [ | |
| if ff e [keep/only e] | |
| ]] | |
| ][ ; multiple args | |
| collect [foreach e x [ | |
| if apply :ff e [keep/only e] | |
| ]] | |
| ] | |
| ] | |
| ||: make op! :FILTER | |
| comment { | |
| ; test: | |
| a: [1 2 3 4 5 6 7 8 9 10] | |
| print mold filter a [[x] x % 2 = 0] | |
| a: [[1 2] [3 6] [5 6] [7 14] [9 10]] | |
| print mold filter a [[x y] (x * 2) = y] | |
| print mold a || [[x y] (x * 2) = y] | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example code using map and filter: