Skip to content

Instantly share code, notes, and snippets.

@hinjolicious
Created June 20, 2026 16:21
Show Gist options
  • Select an option

  • Save hinjolicious/3b044f64744fabd851859832db1d9275 to your computer and use it in GitHub Desktop.

Select an option

Save hinjolicious/3b044f64744fabd851859832db1d9275 to your computer and use it in GitHub Desktop.
FILTER - Filter a list based on a condition
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]
}
@hinjolicious

hinjolicious commented Jun 20, 2026

Copy link
Copy Markdown
Author

Example code using map and filter:

#include %pipe-map.red
#include %filter.red

data: [[1 2] [3 4] [5 6] [7 8]]

result: data 
    || [[a b] a + b > 5]            ; 1. Filter out matrices summing <= 5
    ||> [[a b] reduce [b a]]        ; 2. Swap the columns of the survivors

probe result
; == [[4 3] [6 5] [8 7]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment