The Bash example opens this page.
filter: keep the records where the answer is yes.
question="Is this a complaint?"
cat <<'EOF' |
Arrived a day early. Thank you!
The zipper broke the first time I used it.
Does this come in blue?
The strap snapped on day two.
EOF
thinkthen filter "$question" --batch 1The zipper broke the first time I used it. The strap snapped on day two.
You give it one yes-or-no question and many records. You get back the records that pass, byte for byte, in the order they went in.
Read the answer
No record sets the exit code. A record under the threshold is dropped.
| Exit code | What it means |
|---|---|
| 0 | the run finished |
| 2 | usage or input error |
| 4 | the backend failed or refused, as it does for evidence over the size limit |
| 5 | a local failure |
| 70 | a defect in the tool |
Call it from your language
import thinkthen as tt
question = "Is this a complaint?"
reviews = [
"Arrived a day early. Thank you!",
"The zipper broke the first time I used it.",
"Does this come in blue?",
"The strap snapped on day two.",
]
complaints = tt.filter(question, reviews).value
assert complaints == [reviews[1], reviews[3]]No Polars sample for filter.
No pandas sample for filter.
No TypeScript sample for filter.
require "thinkthen"
question = "Is this a complaint?"
reviews = [
"Arrived a day early. Thank you!",
"The zipper broke the first time I used it.",
"Does this come in blue?",
"The strap snapped on day two."
]
complaints = ThinkThen.filter(question, reviews).value
raise unless complaints == [reviews[1], reviews[3]]library(thinkthen)
question <- "Is this a complaint?"
reviews <- c(
"Arrived a day early. Thank you!",
"The zipper broke the first time I used it.",
"Does this come in blue?",
"The strap snapped on day two."
)
complaints <- tt_filter(question, reviews)$value
stopifnot(identical(complaints, reviews[c(2, 4)]))use thinkthen::{Engine, Question};
let tt = Engine::from_env()?;
let question = "Is this a complaint?";
let complaint = Question::decide(question)?.cut();
let reviews = [
"Arrived a day early. Thank you!",
"The zipper broke the first time I used it.",
"Does this come in blue?",
"The strap snapped on day two.",
];
let complaints = tt
.filter(&complaint, reviews)
.collect::<Result<Vec<_>, _>>()?;
assert_eq!(complaints, [reviews[1], reviews[3]]);Put this code inside fn main() -> Result<(), Box<dyn std::error::Error>> and end it with Ok(()). main returns a Result, so ? compiles.
#include <assert.h>
#include <json-c/json.h>
#include <thinkthen.h>
thinkthen_engine *tt = thinkthen_engine_new();
assert(tt);
const char *filter =
"{\"filter\": \"Is this a complaint?\", "
"\"records\": ["
"\"Arrived a day early. Thank you!\", "
"\"The zipper broke the first time I used it.\", "
"\"Does this come in blue?\", "
"\"The strap snapped on day two.\"]}";
const char *expected =
"[\"The zipper broke the first time I used it.\","
"\"The strap snapped on day two.\"]";
char *complaints_call = thinkthen_call(tt, filter);
assert(complaints_call);
enum json_tokener_error parse_error;
struct json_object *result =
json_tokener_parse_verbose(
complaints_call, &parse_error);
assert(parse_error == json_tokener_success);
assert(result && json_object_get_type(result)
== json_type_object);
struct json_object *value;
json_bool has_value = json_object_object_get_ex(
result, "value", &value);
assert(has_value);
struct json_object *facts;
json_bool has_facts = json_object_object_get_ex(
result, "facts", &facts);
assert(has_facts);
assert(facts && json_object_get_type(facts)
== json_type_object);
struct json_object *wanted =
json_tokener_parse_verbose(expected, &parse_error);
assert(parse_error == json_tokener_success);
assert(json_object_equal(value, wanted));
if (wanted) json_object_put(wanted);
json_object_put(result);
thinkthen_free_string(complaints_call);
thinkthen_engine_free(tt);Keep the #include lines on top. Put the rest inside int main(void) and end it with return 0;.
No C++ sample for filter.
No Objective-C sample for filter.
No COBOL sample for filter.
No Ada sample for filter.
No Java sample for filter.
No Kotlin sample for filter.
No Scala sample for filter.
No C# sample for filter.
No Go sample for filter.
No Swift sample for filter.
No Zig sample for filter.
No PHP sample for filter.
No Dart sample for filter.
No DuckDB sample for filter.
.load ./thinkthen
WITH t(body) AS (VALUES
('Arrived a day early. Thank you!'),
('The zipper broke the first time I used it.'),
('Does this come in blue?'),
('The strap snapped on day two.')),
reviews AS (
SELECT
body,
thinkthen_decide('Is this a complaint?', body)
AS is_complaint
FROM t)
SELECT body FROM reviews
WHERE is_complaint;The zipper broke the first time I used it. The strap snapped on day two.
SELECT body
FROM (
SELECT body, thinkthen_decide(
'{"decide": "Is this a complaint?"}',
body
) AS is_complaint
FROM (VALUES
('Arrived a day early. Thank you!'),
('The zipper broke the first time I used it.'),
('Does this come in blue?'),
('The strap snapped on day two.')
) AS t(body)
) AS reviews
WHERE is_complaint;body -------------------------------------------- The zipper broke the first time I used it. The strap snapped on day two. (2 rows)
Arguments, options, and more examples
Jobs that use filter
Watch filter answer questions about Beatles songs: filter keeps what clears your bar.
On GitHub: github.com/botassembly/thinkthen