-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
255 lines (208 loc) · 6.27 KB
/
index.html
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>RamdaScript</title>
<style type="text/css">
body {
font-family: monospace;
margin: 0px;
background-color: #FFFDE7;
}
.container {
margin: 0px 20px;
}
#source,
#output {
width: 48%;
height: 300px;
display: inline;
background-color: white;
overflow: auto;
}
.header {
padding: 3px;
background-color: #FDD835;
margin-bottom: 10px;
border-bottom: 2px solid black;
}
.header > .container > * {
vertical-align: middle;
}
.toolbar {
margin-bottom: 3px;
}
h1 {
display: inline;
}
@media screen and (max-width:560px) {
#source,
#output {
display: block;
width: 100%;
}
}
</style>
</head>
<body>
<div class="header">
<div class="container">
<img src="cover-small.png">
<h1>RamdaScript</h1>
<a href="https://github.com/yosbelms/ramdascript">GitHub</a> |
<a href="https://github.com/yosbelms/ramdascript/blob/master/docs.md">Docs</a>
</div>
</div>
<div class="container">The Lisp that compiles to <a href="http://ramdajs.com">Ramda.js</a></div>
</br>
<div class="container editor">
<div class="toolbar">
<select id="snippets">
<option name="hello">Hello World</option>
<option name="fibonacci">Fibonacci</option>
<option name="fizzbuzz">FizzBuzz</option>
<option name="objects">Objects</option>
<option name="interop">JS Interop</option>
</select>
<a id="runbtn" href="javascript:void(0)">Run</a> |
<a id="linkbtn" href="javascript:void(0)">Link</a>
</div>
<textarea id="source"></textarea>
<textarea id="output" disabled></textarea>
</div>
<script type="text/javascript" src="./browser.js"></script>
<script type="text/ramdascript">
(def log console.log)
(def hash window.location.hash)
(def rhash /^#link:/)
(def source (document.getElementById 'source'))
(def output (document.getElementById 'output'))
(def snippets (document.getElementById 'snippets'))
(def runbtn (document.getElementById 'runbtn'))
(def linkbtn (document.getElementById 'linkbtn'))
// Overide console.log
(alter console.log (fn []
(log.apply console arguments)
(alter output.value
(join '\n'
(concat [output.value] (Array.from arguments))))))
// Run
(alter runbtn.onclick (fn []
(alter output.value '')
(RamdaScript.run source.value)))
// Output JS code
(alter outputJs (fn []
((bind linkbtn.setAttribute linkbtn) 'href'
(concat '#link:' (encodeURIComponent source.value)))
(alter output.value (prop 'js' (RamdaScript.compile source.value)))))
// Select snippet
(def setSource (fn [src]
(alter source.value src)))
(def getSelectedOption (fn []
(nth snippets.selectedIndex snippets.options)))
(def getSelectedOptionName (fn []
((invoker 1 'getAttribute') 'name' (getSelectedOption))))
(alter snippets.onchange (fn []
(alter output.value '')
(call
(pipe
(bind document.getElementById document)
(prop 'innerHTML')
setSource)
(getSelectedOptionName))
(outputJs)))
(alter source.onkeyup outputJs)
// initialize
((ifElse
(fn [] (test rhash hash))
(fn []
((pipe
(replace rhash '')
decodeURIComponent
(alter source.value)) hash)
(outputJs))
snippets.onchange))
</script>
<script id="none" type="text/snippet"></script>
<script id="hello" type="text/snippet">
(console.log 'Hello World!!')
</script>
<script id="fibonacci" type="text/snippet">
(def fib (fn [n]
(call (ifElse
(lte _ 1)
(always 1)
(converge add
[(compose fib (subtract _ 1))
(compose fib (subtract _ 2))]))
n)))
(console.log (times fib 10))
</script>
<script id="fizzbuzz" type="text/snippet">
(def isFizzBuzz (fn [a]
(call (pipe
(modulo _ 15)
(ifElse
(equals 0)
(fn [] (console.log 'FizzBuzz'))
(fn [] (isBuzz a))))
a)))
(def isBuzz (fn [a]
(call (pipe
(modulo _ 5)
(ifElse
(equals 0)
(fn [] (console.log 'Buzz'))
(fn [] (isFizz a))))
a)))
(def isFizz (fn [a]
(call (pipe
(modulo _ 3)
(ifElse
(equals 0)
(fn [] (console.log 'Fizz'))
(fn [] (console.log a))))
a)))
(def fizzBuzz (fn [a b]
(call (ifElse
(fn [] (lt a (inc b)))
(fn []
(isFizzBuzz a)
(fizzBuzz (inc a) b))
(fn [] (console.log 'end'))))))
// run
(fizzBuzz 1 15)
</script>
<script id="objects" type="text/snippet">
(def person {
:name 'Yosbel',
:age 30
})
(console.log person.name)
// mutate
(alter person.name 'Yosbel Marín')
(console.log person.name)
// factory
(def Person (fn [name, age]
{
:name name
:age age
}))
// create object
(def obj (Person 'Yosbel Marín' 30))
(console.log obj.name obj.age)
</script>
<script id="interop" type="text/snippet">
// importing external modules
(import 'react' React)
// instantiate js classes
(def obj (new SomeClass a b c))
// mutate
(alter document.someProp 'some value')
// partially applied alter
((alter document.someProp) 'updated!!')
</script>
</body>
</html>