askrobots / dbbasic Public

Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.

0 downloads

dbbasic/docs/paradigms/FUNCTIONAL_REACTIVE_IN_CONFIG.yaml
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add DBBasic crate engine logo and update README 0d628db 1 year, 2 months ago History
FUNCTIONAL_REACTIVE_IN_CONFIG.yaml · 6.0 KB · 252 lines
1# Functional Reactive Programming in Config
2# Streams, observables, pure functions
3# What MVC couldn't express elegantly
4
5name: "Reactive Application"
6paradigm: "Functional Reactive Programming"
7why_mvc_failed: "Imperative state mutations don't compose"
8
9# ==========================================
10# REACTIVE STREAMS → CONFIG
11# ==========================================
12streams:
13 # RxJS, Bacon.js, Akka Streams style
14
15 user_activity:
16 source: "websocket://events"
17
18 pipeline:
19 - filter: "event.type === 'click'"
20 - throttle: "1000ms"
21 - map: "event => ({...event, timestamp: Date.now()})"
22 - buffer: "5 seconds"
23 - flat_map: "events => analytics.batch(events)"
24
25 sink: "database"
26
27 price_updates:
28 source: "kafka://prices"
29
30 operators:
31 - distinct_until_changed: "price"
32 - debounce: "500ms"
33 - scan: "(acc, price) => [...acc.slice(-10), price]"
34 - map: "calculate_moving_average"
35
36 subscribers:
37 - update_ui
38 - send_alerts
39 - log_to_database
40
41# ==========================================
42# PURE FUNCTIONS → CONFIG
43# ==========================================
44functions:
45 # No side effects, composable
46
47 calculate_tax:
48 inputs: [subtotal, state]
49 pure: true
50 memoize: true
51 logic: |
52 match state:
53 "CA" => subtotal * 0.0725
54 "TX" => subtotal * 0.0625
55 _ => subtotal * 0.05
56
57 compose_validators:
58 inputs: [validators]
59 returns: "value => validators.every(v => v(value))"
60
61 lens:
62 # Functional lenses for immutable updates
63 user_email:
64 get: "user => user.email"
65 set: "(email, user) => {...user, email}"
66
67# ==========================================
68# OBSERVABLES → CONFIG
69# ==========================================
70observables:
71 # Everything is a stream
72
73 mouse_position:
74 source: "fromEvent(document, 'mousemove')"
75 transform:
76 - map: "e => ({x: e.clientX, y: e.clientY})"
77 - sample: "100ms"
78
79 search_suggestions:
80 source: "fromEvent(input, 'input')"
81 transform:
82 - map: "e => e.target.value"
83 - debounce: "300ms"
84 - distinct_until_changed
85 - switch_map: "query => api.search(query)"
86
87 websocket_reconnect:
88 source: "websocket"
89 retry:
90 strategy: "exponential_backoff"
91 max_attempts: 5
92
93# ==========================================
94# STATE MANAGEMENT (REDUX STYLE) → CONFIG
95# ==========================================
96state:
97 # Immutable, time-travel debugging
98
99 store:
100 initial:
101 users: []
102 posts: []
103 ui:
104 loading: false
105 error: null
106
107 actions:
108 FETCH_USERS: {type: "FETCH_USERS"}
109 USERS_LOADED: {type: "USERS_LOADED", payload: "users"}
110 USERS_ERROR: {type: "USERS_ERROR", payload: "error"}
111
112 reducers:
113 users:
114 USERS_LOADED: "(state, action) => action.payload"
115 USER_ADDED: "(state, action) => [...state, action.payload]"
116
117 ui:
118 FETCH_USERS: "(state) => {...state, loading: true}"
119 USERS_LOADED: "(state) => {...state, loading: false}"
120 USERS_ERROR: "(state, action) => {...state, loading: false, error: action.payload}"
121
122 epics:
123 # Redux-Observable style
124 fetch_users_epic:
125 trigger: "FETCH_USERS"
126 effect: |
127 action$.pipe(
128 switchMap(() =>
129 ajax.getJSON('/api/users').pipe(
130 map(users => ({type: 'USERS_LOADED', payload: users})),
131 catchError(error => ({type: 'USERS_ERROR', payload: error}))
132 )
133 )
134 )
135
136# ==========================================
137# SIGNAL/COMPUTED VALUES → CONFIG
138# ==========================================
139signals:
140 # Like SolidJS, Vue 3 reactivity
141
142 count:
143 value: 0
144
145 doubled:
146 computed: "count() * 2"
147
148 tripled:
149 computed: "count() * 3"
150
151 total:
152 computed: "doubled() + tripled()"
153
154 effect:
155 watch: [count]
156 run: "console.log('Count changed:', count())"
157
158# ==========================================
159# MONADS & FUNCTORS → CONFIG
160# ==========================================
161monads:
162 # Maybe, Either, IO
163
164 Maybe:
165 of: "value => value != null ? Some(value) : None()"
166 map: "fn => maybe.isNone ? None() : Some(fn(maybe.value))"
167 chain: "fn => maybe.isNone ? None() : fn(maybe.value)"
168
169 Either:
170 try:
171 attempt: "api.fetchUser(id)"
172 on_success: "Right(user)"
173 on_error: "Left(error)"
174
175 IO:
176 # Lazy evaluation
177 chain:
178 - "() => readFile('config.yaml')"
179 - "content => parseYAML(content)"
180 - "config => validateConfig(config)"
181
182# ==========================================
183# TRANSDUCERS → CONFIG
184# ==========================================
185transducers:
186 # Composable transformations
187
188 process_orders:
189 compose:
190 - filter: "order => order.total > 100"
191 - map: "order => applyDiscount(order)"
192 - take: 10
193
194 apply_to:
195 - array: "orders"
196 - stream: "order_stream"
197 - channel: "order_channel"
198
199# ==========================================
200# DATAFLOW PROGRAMMING → CONFIG
201# ==========================================
202dataflow:
203 # Nodes and edges, like Node-RED
204
205 nodes:
206 input:
207 type: "http_request"
208 output: "request"
209
210 validate:
211 type: "function"
212 input: "request"
213 function: "validate_request"
214 output: "validated_data"
215
216 transform:
217 type: "function"
218 input: "validated_data"
219 function: "transform_data"
220 output: "transformed"
221
222 save:
223 type: "database"
224 input: "transformed"
225 operation: "insert"
226 output: "result"
227
228 response:
229 type: "http_response"
230 input: "result"
231
232 edges:
233 - from: "input", to: "validate"
234 - from: "validate", to: "transform"
235 - from: "transform", to: "save"
236 - from: "save", to: "response"
237
238# ==========================================
239# WHY THIS MATTERS
240# ==========================================
241# MVC assumes:
242# - Request → Controller → Model → View
243# - Imperative updates
244# - Mutable state
245#
246# FRP assumes:
247# - Everything is a stream
248# - Declarative transformations
249# - Immutable data
250#
251# DBBasic unifies both paradigms
252# ==========================================