drym-org / qi

An embeddable flow-oriented language.
58 stars 12 forks source link

Transform inputs "only if" a condition is met #30

Open countvajhula opened 2 years ago

countvajhula commented 2 years ago

[Continuing a discussion with @benknoble and @TrueQueenBee from Racket Discord]

There may be cases where we want to transform inputs if a condition is met, and pass them through unchanged, otherwise.

Here are some considerations in designing the solution:

To help identify a good solution, we need examples! There are a few patterns here to look out for which may be relevant to consider:

  1. "Only when" pattern -- modify the inputs only when a condition is met, otherwise pass them through
  2. "Admit and disbar" pattern -- if a condition is met, allow the inputs through, otherwise block them (or produce false, etc.)
  3. "Pick" / "choose" pattern -- choose the value that in the leading position makes the predicate return true (e.g. "pick the lesser of two values" (if (< a b) a b)
  4. Anything else that's remotely like this 🙂
benknoble commented 2 years ago

I'm back from my honeymoon :) hopefully I have time & energy to do some analysis of the Qi I have floating around this weekend/next week. It's too bad GH doesn't let me put a reminder here of some kind…

It looks like most of the code I need to dig through is in the public https://github.com/benknoble/advent2021, but I have a fair bit of (newer? more experienced?) Qi in a private project, too.

benknoble commented 2 years ago

Preliminary gathering of code. LMK what questions you have.

Transformation & Condition Analysis

Advent of Code 2021

day4/solution.rkt

day10/solution.rkt

day12/solution.rkt

day13/solution.rkt

day15/solution.rkt

day17/solution.rkt

day18/solution.rkt

day19/solution.rkt

day20/solution.rkt

day22/solution.rkt

day23/solution.rkt

day25/solution.rkt


Random observation: occasionally the structure of some flows I have written is not the most efficient (for example: (~> (-< car cdr) (== (~> add1 (modulo x)) _) cons)). But the structure is suggestive: the written example is clearly destructuring the data, transforming its components, and reconstructing them. The more efficient version is (~> (-< (~> car add1 (modulo x)) cdr) cons), which may even be less to type, but loses to my eyes the structure. I happened to see this type of pattern a lot in my Advent of Code, I think because I was working with pairs so often.

Private Project