File: assoc-test2.thp

package info (click to toggle)
theme-d 7.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,036 kB
  • sloc: lisp: 9,625; sh: 5,321; makefile: 715; ansic: 477
file content (42 lines) | stat: -rw-r--r-- 1,234 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
;; -*-theme-*-

;; Copyright (C) 2015, 2021, 2024  Tommi Höynälänmaa
;; Distributed under GNU Lesser General Public License version 3,
;; see file doc/LGPL-3.


(define-proper-program (examples assoc-test2)


  (import (standard-library core)
	  (standard-library string-utilities)
	  (standard-library basic-math)
	  (standard-library console-io)
	  (examples assoc-sgn)
	  (examples hash-table))

  (define-param-method my-proc (%key %value)
		       (((al (:assoc %key %value)) (obj-key %key))
			<none> nonpure)
    (console-display-line (gen-assoc al obj-key)))


  (add-method char->integer
    (unchecked-prim-proc char->integer (<character>) <integer> pure))


  (define-simple-method string-hash (((str <string>) (i-size <integer>))
				     <integer> pure)
    (let ((i-len (string-length str)))
      (let-mutable ((i-sum <integer> 0))
	(do ((i <integer> 0 (+ i 1))) ((>= i i-len))
	  (set! i-sum (+ i-sum (char->integer (string-ref str i)))))
	(remainder i-sum i-size))))


  (define-main-proc (() <none> nonpure)
    (let ((al2 (make-hash-table string-hash string=? 10 0.0)))
      (gen-assoc-set! al2 "abc" 1.2)
      (gen-assoc-set! al2 "abc2" 1.5)
      (gen-assoc-set! al2 "abc3" 2.0)
      (my-proc al2 "abc"))))