-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.clj
175 lines (114 loc) · 3.62 KB
/
demo.clj
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
(ns demo
(:require
[clojuratica.core :as wl :refer [WL math-intern math-evaluate kernel-link wl->clj clj->wl]]
[clojuratica.tools.graphics :as graphics :refer [make-app! make-math-canvas! show!]]))
;; * Clojure a 1min intro
;; ** Interactive developement
;; *** Evaluating forms from a text editor connected to a REPL
(+ 1 1)
;; ** Syntax a.k.a. data structures
;; *** Interesting scalar datatypes
;; - symbols: a b c d
;; unqoted
;; x
;; (def x "hello")
;; quoted
'a
;; - keywords
:hello
;; namespaced
:project.media/image
;; *** Interesting compund datatypes
;; vector
[1 3 :a :b 'foo 'bar]
;; hash map
{:a 23
:b 73}
;; * Wolframite (nee Clojuratica) -- name is a WIP
(WL (Dot [1 2 3] [4 5 6]))
(WL '"{1 , 2, 3} . {4, 5, 6}")
(WL (Plus 23 '"{1 , 2, 3} . {4, 5, 6}"))
(math-intern math-evaluate Plus)
#_{:clj-kondo/ignore true}
(Plus [1 2] [3 4])
(def greetings
(WL
(Function [x] (StringJoin "Hello, " x "! This is a Mathematica function's output."))))
(greetings "Stephen")
;; clojure threading fun -- thread first macro
(-> "Ocean"
Entity
(GeoNearest Here)
WL)
;; - more interesting examples:
(WL (WolframAlpha "number of moons of Saturn" "Result"))
;; WL syntactic sugar (originally postfix notation)
(WL @(a b c d))
(WL (TextStructure "The cat sat on the mat."))
(WL (TextStructure "You can do so much with the Wolfram Language." "ConstituentGraphs"))
;; - Bidirectional translation
(wl->clj "GridGraph[{5, 5}]" math-evaluate)
(clj->wl '(GridGraph [5 5]) {:kernel-link kernel-link
:output-fn str})
;; ** Graphics
;; *** Init math canvas & app
(let [sf 1.3]
(def canvas (make-math-canvas! kernel-link :scale-factor sf))
(def app (make-app! canvas :scale-factor sf)))
;; *** Draw Something!
(show! canvas (clj->wl '(GridGraph [5 5]) {:output-fn str}))
(show! canvas (clj->wl '(ChemicalData "Ethanol" "StructureDiagram") {:output-fn str}))
;; *** Make it easier (Dev Helper: closing over the canvas)
(defn quick-show [clj-form]
(show! canvas (clj->wl clj-form {:output-fn str})))
;; *** Some Simple Graphics Examples
(quick-show '(ChemicalData "Ethanol" "StructureDiagram"))
(quick-show '(GridGraph [5 5]))
(quick-show '(GeoImage (Entity "City" ["NewYork" "NewYork" "UnitedStates"])))
;; Rule 30
(quick-show '(ArrayPlot (CellularAutomaton 30 [[1] 0] 50)))
;; Multiway Graph
;;https://writings.stephenwolfram.com/2021/09/even-beyond-physics-introducing-multicomputation-as-a-fourth-general-paradigm-for-theoretical-science/
(quick-show '(Graph
((ResourceFunction "MultiwaySystem")
[(-> "A" "BBB") (-> "BB" "A")]
"A"
7
"StatesGraph")
(-> AspectRatio 1.5)))
;; ** Some Similarities and Next Steps
;; it's in the family
;; RulePlot[CellularAutomaton[30]]
;; (RulePlot (CellularAutomaton 30))
;; - implementing clojure hash map -> WL associations
(-> (Association (-> "b" 2)
(-> "c" 3)
(-> "a" 1))
(Keys)
(Take 2)
(->> (Map StringLength) WL))
;; not working yet...
;; (-> {"a" 1
;; "b" 2
;; "c" 3}
;; (Keys)
;; (Take 2)
;; (->> (Map StringLength) WL))
(->> {"a" 1
"b" 2
"c" 3}
(keys)
(take 2)
(map count))
(comment ;; WIP
;; TODO: understand scopes
(math-intern math-evaluate :scopes)
;; TODO Fix this one
;; prnc: need to understand this scoping construct better, seemed to work :/
#_{:clj-kondo/ignore true}
(Let [t (-> ["Text" "AliceInWonderland"]
ExampleData
ContentObject
Snippet)]
(WordFrequency ~t (TextWords ~t)))
:end-comment)