[Racket] Ferti hydroponic nutrient solver, redux.
Introduce nutrient-value module.
Changed files
models/nutrient-value.rkt
@@ -0,0 +1,39 @@
1
Added:
#lang racket
2
Added:
3
Added:
(provide nutrient-value?
4
Added:
maybe-nutrient-value?
5
Added:
nutrient-value-hash/c
6
Added:
(contract-out [insert-nutrient-values
7
Added:
(-> connection? db-id? nutrient-value-hash/c (listof (cons/c symbol? any/c)))]
8
Added:
[residuals->nutrient-value-hash
9
Added:
(-> (listof residual-vector/c) nutrient-value-hash/c)]))
10
Added:
11
Added:
(require db
12
Added:
sql
13
Added:
"nutrient.rkt"
14
Added:
"utils.rkt")
15
Added:
16
Added:
(define nutrient-value? (and/c real? (>=/c 0)))
17
Added:
(define maybe-nutrient-value? (or/c nutrient-value? #f))
18
Added:
(define nutrient-value-hash/c (hash/c nutrient? nutrient-value? #:immutable #t))
19
Added:
20
Added:
;; vector/c id, canonical name, french name, nutrient formula, value (ppm)
21
Added:
(define residual-vector/c (vector/c db-id? string? string? string? real?))
22
Added:
23
Added:
(define (insert-nutrient-values conn nvs-id nutrient-values)
24
Added:
(define nv-rows
25
Added:
(for/list ([(n v) (in-hash nutrient-values)])
26
Added:
(map value->scalar-expr-ast (list nvs-id (nutrient-id n) v))))
27
Added:
(define result
28
Added:
(query conn
29
Added:
(insert #:into nutrient_values
30
Added:
#:columns value_set_id
31
Added:
nutrient_id
32
Added:
value_ppm
33
Added:
#:from (TableExpr:AST ,(make-values*-table-expr-ast nv-rows)))))
34
Added:
(simple-result-info result))
35
Added:
36
Added:
(define (residuals->nutrient-value-hash residuals)
37
Added:
(for/hash ([r (in-list residuals)])
38
Added:
(match-define (vector n-id n-canonical-name n-french-name n-formula value-ppm) r)
39
Added:
(values (nutrient n-id n-canonical-name n-french-name n-formula) value-ppm)))
models/nutrient.rkt
@@ -6,9 +6,6 @@
6
6
nutrient-canonical-name
7
7
nutrient-french-name
8
8
nutrient-formula
9
Removed:
nutrient-value?
10
Removed:
maybe-nutrient-value?
11
Removed:
nutrient-value-hash/c
12
9
(contract-out [create-nutrient! (-> string? string? string? nutrient?)]
13
10
[get-nutrients (-> (listof nutrient?))]
14
11
[get-nutrient
@@ -21,31 +18,18 @@
21
18
(->* (nutrient?)
22
19
(#:name (or/c #f string?) #:formula (or/c #f string?))
23
20
(or/c nutrient? #f))]
24
Removed:
[delete-nutrient! (-> nutrient? void?)]
25
Removed:
[residuals->nutrient-value-hash
26
Removed:
(-> (listof residual-vector/c) nutrient-value-hash/c)]))
21
Added:
[delete-nutrient! (-> nutrient? void?)]))
27
22
28
23
(require racket/contract
29
24
db
30
25
sql
31
Removed:
"../db/conn.rkt")
26
Added:
"../db/conn.rkt"
27
Added:
"utils.rkt")
32
28
33
29
(struct nutrient (id canonical-name french-name formula)
34
30
#:transparent
35
31
#:property prop:custom-write
36
32
(λ (v out _) (fprintf out "#<~a ~a>" (nutrient-id v) (nutrient-canonical-name v))))
37
Removed:
38
Removed:
(define nutrient-value? (and/c real? (>=/c 0)))
39
Removed:
(define maybe-nutrient-value? (or/c nutrient-value? #f))
40
Removed:
(define nutrient-value-hash/c (hash/c nutrient? nutrient-value? #:immutable #t))
41
Removed:
42
Removed:
;; vector/c id, canonical name, french name, nutrient formula, value (ppm)
43
Removed:
(define residual-vector/c (vector/c exact-nonnegative-integer? string? string? string? real?))
44
Removed:
45
Removed:
(define (residuals->nutrient-value-hash residuals)
46
Removed:
(for/hash ([r (in-list residuals)])
47
Removed:
(match-define (vector n-id n-canonical-name n-french-name n-formula value-ppm) r)
48
Removed:
(values (nutrient n-id n-canonical-name n-french-name n-formula) value-ppm)))
49
33
50
34
;; CREATE
51
35