]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/src-isv/JavaVsSCL.html
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.runtime / src-isv / JavaVsSCL.html
1 <?xml version='1.0' encoding='utf-8' ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>Lists</title><link type="text/css" rel="stylesheet" href="wikistyle.css"/></head><body>\r
2 \r
3 <h2 id="Comparison_of_Java_and_SCL">Comparison of Java and SCL</h2>\r
4 \r
5 <h3>Language constructions and patterns</h3>\r
6 \r
7 <table border="1">\r
8 <tr>\r
9 <td><pre>0.5 + x * (2e-3 + x * 2)</pre></td>\r
10 <td><pre>0.5 + x * (2e-3 + x * 2)</pre></td>\r
11 </tr>\r
12 \r
13 <tr>\r
14 <td><pre>x &gt;= 0 &amp;&amp; x &lt;= 1</pre></td>\r
15 <td><pre>x &gt;= 0 &amp;&amp; x &lt;= 1</pre></td>\r
16 </tr>\r
17 \r
18 <tr>\r
19 <td><pre>Math.sin(a1) - Math.cos(a2)</pre></td>\r
20 <td><pre>sin a1 - cos a2</pre></td></tr>\r
21 \r
22 <tr>\r
23 <td><pre>cond ? value : alternative</pre></td>\r
24 <td><pre><b>if</b> cond <b>then</b> value <b>else</b> alternative</pre></td>\r
25 </tr>\r
26 \r
27 <tr>\r
28 <td><pre>"I am " + age + " years old."</pre></td>\r
29 <td>\r
30 <pre>"I am " + show age + " years old."</pre>\r
31 \r
32 In future:\r
33 \r
34 <pre>"I am ${age} years old."</pre>\r
35 </td>\r
36 </tr>\r
37 \r
38 <tr><td><pre>\r
39 <b>final</b> int sum = a + b;</pre>\r
40 </td><td><pre>\r
41 sum = a + b</pre>\r
42 </td></tr>\r
43 \r
44 <tr><td><pre>\r
45 public static String decorate(String x) {\r
46     return "(" + x + ")"; \r
47 }</pre>\r
48 </td><td><pre>\r
49 decorate :: String -> String\r
50 decorate x = "(" + x + ")"</pre>\r
51 (the first line is optional)\r
52 </td></tr>\r
53 \r
54 <tr><td><pre>\r
55 {\r
56     statement1;\r
57     statement2;\r
58     statement3;\r
59 }</pre>\r
60 </td><td><pre>\r
61 <b>do</b>\r
62     statement1\r
63     statement2\r
64     statement3\r
65 </pre></td></tr>\r
66 \r
67 <tr><td><pre>\r
68 <b>if</b>(cond1)\r
69     statement1;\r
70 <b>else if</b>(cond2)\r
71     statement2;\r
72 <b>else</b> \r
73     statement3;\r
74 </pre></td><td><pre>\r
75 <b>if</b> cond1\r
76 <b>then</b> statement1\r
77 <b>else if</b> cond2\r
78 <b>then</b> statement2\r
79 <b>else</b> statement3\r
80 </pre></td></tr>\r
81 \r
82 <tr><td><pre>\r
83 <b>for</b>(T x : l)\r
84     statement;\r
85 </pre></td><td><pre>\r
86 for l (\x -> \r
87     statement\r
88 )\r
89 </pre></td></tr>\r
90 \r
91 <tr><td><pre>\r
92 <b>while</b>(cond) statement;\r
93 </pre></td><td><pre>\r
94 while (\() -> cond) (\() -> statement)\r
95 </pre>\r
96 In future with improvements to macros:\r
97 <pre>\r
98 while cond statement\r
99 </pre>\r
100 </td></tr>\r
101 \r
102 <tr><td><pre>\r
103 int var;\r
104 <b>switch</b>(value) {\r
105 <b>case</b> Label1: \r
106     var = case1; \r
107     break;\r
108 <b>case</b> Label2: \r
109     var = case2; \r
110     break;\r
111 <b>default</b>:\r
112     var = case3;        \r
113 }</pre>\r
114 </td><td><pre>\r
115 var = <b>match</b> value <b>with</b>\r
116     Label1 -> case1\r
117     Label2 -> case2\r
118     _      -> case3\r
119 </pre>\r
120 </td></tr>\r
121 \r
122 <tr><td><pre>\r
123 <b>break</b>;\r
124 <b>continue</b>;\r
125 <b>return</b> x; // In the middle of the code\r
126 </pre></td><td>No exact correspondence. However, every control sequence can be written in the following form:\r
127 <pre>label1 initialState\r
128   where\r
129     label1 state = \r
130         ...\r
131         then label2 modifiedState1\r
132         else label3 modifiedState2\r
133     label2 state = \r
134         ...\r
135     label3 state = \r
136         ...\r
137         label1 modifiedState</pre>\r
138 </td></tr>\r
139 \r
140 <tr><td><pre>\r
141 int a, b;\r
142 <b>if</b>(cond) {\r
143     a = aExp1;\r
144     b = bExp1;\r
145 }\r
146 <b>else</b> {\r
147     a = aExp2;\r
148     b = bExp2;\r
149 }\r
150 </pre>\r
151 </td><td><pre>\r
152 (a, b) = <b>if</b> cond\r
153           <b>then</b> (aExp1, bExp1)\r
154           <b>else</b> (aExp2, bExp2)         \r
155 </pre>\r
156 </td></tr>\r
157 \r
158 <tr><td><pre>\r
159 int a = 1;\r
160 a = 2;\r
161 a = 3;\r
162 return a;\r
163 </pre>\r
164 </td><td><pre>\r
165 a = ref 1\r
166 a := 2\r
167 a := 3\r
168 getRef a\r
169 </pre>\r
170 \r
171 The name of the function <code>getRef</code> will change in the future and is\r
172 probably replaced by some special syntax. For example in ML <code>getRef a</code>\r
173 would be written as <code>!a</code>.\r
174 \r
175 </td></tr>\r
176 \r
177 <tr><td><pre>\r
178 public static String f(int x) {\r
179     if(x >= 0)\r
180         return Integer.toString(x);\r
181     else\r
182         return null;\r
183 }</pre>\r
184 </td><td><pre>\r
185 f :: Integer -> Maybe String\r
186 f x | x >= 0    = Just (show x)\r
187      | otherwise = Nothing\r
188 </pre>\r
189 </td></tr>\r
190 \r
191 <tr><td><pre>\r
192 public static int invF(String s) {\r
193     if(s == null)\r
194         return -1;\r
195     else\r
196         return Integer.parseInt(s);\r
197 }</pre>\r
198 </td><td><pre>\r
199 invF :: Maybe String -> Integer\r
200 invF Nothing  = -1\r
201 invF (Just s) = read s\r
202 </pre>\r
203 </td></tr>\r
204 \r
205 <tr><td><pre>\r
206 Arrays.asList(new String[] {"x", "y", "z"})\r
207 </pre>\r
208 </td><td><pre>\r
209 ["x", "y", "z"]\r
210 </pre>\r
211 </td></tr>\r
212 \r
213 <tr><td><pre>\r
214 l.get(3)\r
215 </pre>\r
216 </td><td><pre>\r
217 l!3\r
218 </pre>\r
219 </td></tr>\r
220 \r
221 <tr><td><pre>\r
222 List&lt;String&gt; decorated = new ArrayList&lt;String&gt;();\r
223 for(String s : undecorated)\r
224     decorated.add("(" + s + ")");\r
225 </pre>\r
226 </td><td><pre>\r
227 decorated = ["(" + s + ")" | s &lt;- undecorated]\r
228 </pre>\r
229 or\r
230 <pre>\r
231 decorated = map (\s -> "(" + s + ")") undecorated\r
232 </pre>\r
233 </td></tr>\r
234 \r
235 <tr><td><pre>\r
236 List&lt;String&gt; nonempty = new ArrayList&lt;String&gt;();\r
237 for(String s : l)\r
238     if(!s.isEmpty())\r
239         nonempty.add(l);\r
240 </pre>\r
241 </td><td><pre>\r
242 decorated = [s | s &lt;- l, s != ""]\r
243 </pre>\r
244 or\r
245 <pre>\r
246 decorated = filter (\s -&gt; s != "") l\r
247 </pre>\r
248 </td></tr>\r
249 \r
250 <tr><td><pre>\r
251 List&lt;List&lt;String&gt;&gt; listOfLists;\r
252 List&lt;String&gt; flattened \r
253     = new ArrayList&lt;String&gt;();\r
254 for(List&lt;String&gt; l : listOfLists)\r
255     for(String s : l)\r
256         flattened.add(s);\r
257 </pre>\r
258 </td><td><pre>\r
259 flattened = [s | l &lt;- listOfLists, s &lt;- l]\r
260 </pre>\r
261 or\r
262 <pre>\r
263 flattened = sum listOfLists\r
264 </pre>\r
265 or\r
266 <pre>\r
267 flattened = join listOfLists\r
268 </pre>\r
269 </td></tr>\r
270 \r
271 <tr><td><pre>\r
272 throw new RuntimeException("Unhandled case.");\r
273 </pre>\r
274 </td><td>\r
275 <pre>fail "Unhandled case."</pre>\r
276 </td></tr>\r
277 \r
278 \r
279 <tr><td><pre>\r
280 <b>try</b> {\r
281     ...\r
282     <b>throw</b> new ExceptionX(...);\r
283     ...\r
284 } <b>catch</b>(ExceptionX e) {\r
285     ...\r
286 } <b>finally</b> {\r
287     ...\r
288 }\r
289 </pre>\r
290 </td><td>\r
291 Currently no correspondence. Exceptions need to be thrown and catched in Java code.\r
292 </td></tr>\r
293 \r
294 <tr><td><pre>\r
295 <b>synchronized</b>(obj) {\r
296     ...\r
297 }\r
298 </pre>\r
299 </td><td>\r
300 Currently no correspondence. Synchronization must be done in Java code.\r
301 </td></tr>\r
302 \r
303 <tr><td><pre>\r
304 <b>final static class</b> Vec3 {\r
305     public final double x;\r
306     public final double y;\r
307     public final double z;\r
308     \r
309     public Vec3(double x, \r
310                 double y, \r
311                 double x) {\r
312         this.x = x;\r
313         this.y = y;\r
314         this.z = z;\r
315     }\r
316 }\r
317 </pre>\r
318 </td><td><pre>\r
319 <b>data</b> Vec3 = Vec3 Double Double Double\r
320 </pre>\r
321 \r
322 In future also:\r
323 \r
324 <pre>\r
325 <b>data</b> Vec3 = Vec3 {\r
326   x :: Double,\r
327   y :: Double,\r
328   z :: Double\r
329 }\r
330 </pre>\r
331 </td></tr>\r
332 \r
333 <tr><td><pre>\r
334 public static interface Num {}\r
335 public static class Int implements Num {\r
336     public static final int value;\r
337     public Int(int value) {\r
338         this.value = value;\r
339     }  \r
340 }\r
341 public static class Real implements Num {\r
342     public static final double value;\r
343     public Real(double value) {\r
344         this.value = value;\r
345     } \r
346 }\r
347 </pre>\r
348 </td><td><pre>\r
349 data Num = Int Integer\r
350           | Real Double\r
351 </pre></td></tr>\r
352 \r
353 <tr><td><pre>\r
354 public static Num add(Num a_, Num b_) {\r
355     if(a_ instanceof Int) {\r
356         Int a = (Int)a_;\r
357         if(b_ instanceof Int) {\r
358             Int b = (Int)b_;\r
359             return new Int(a.value + b.value);\r
360         }\r
361         else if(b_ instanceof Real) {\r
362             Real b = (Real)b_;\r
363             return new Real(a.value + b.value);\r
364         }\r
365     }\r
366     else if(a_ instanceof Real) {\r
367         Real a = (Real)a_;\r
368         if(b_ instanceof Int) {\r
369             Int b = (Int)b_;\r
370             return new Real(a.value + b.value);\r
371         }\r
372         else if(b_ instanceof Real) {\r
373             Real b = (Real)b_;\r
374             return new Real(a.value + b.value);\r
375         }\r
376     }\r
377     throw new IllegalArgumentException();\r
378 }\r
379 </pre>\r
380 </td><td><pre>\r
381 add :: Num -> Num -> Num\r
382 add (Int a)  (Int b)  = Int (a + b)\r
383 add (Int a)  (Real b) = Real (fromInteger a + b)\r
384 add (Real a) (Int b)  = Real (a + fromInteger b)\r
385 add (Real a) (Real b) = Real (a + b)\r
386 </pre></td></tr>\r
387 \r
388 <tr><td><pre>\r
389 public static interface Bijection&lt;A,B&gt; {\r
390     B forward(A v);\r
391     A backward(B v);\r
392 }\r
393 \r
394 Bijection&lt;Integer,Integer> inc = \r
395 new Bijection&lt;Integer,Integer&gt; {\r
396    public Integer forward(Integer v) {\r
397        return v + 1;\r
398    }\r
399    public Integer backward(Integer v) {\r
400        return v - 1;\r
401    }\r
402 }\r
403 </pre>\r
404 </td><td><pre>\r
405 data Bijection a b = \r
406     Bijection (a -> b) (b -> a)\r
407     \r
408 forward (Bijection f _) v = f v \r
409 backward (Bijection _ f) v = f v\r
410 \r
411 inc = Bijection (\v -> v + 1) (\v -> v - 1)\r
412 </pre>\r
413 \r
414 In future:\r
415 \r
416 <pre>\r
417 data Bijection a b = Bijection {\r
418     forward :: a -> b,\r
419     backward :: b -> a\r
420 }\r
421 \r
422 inc = Bijection {\r
423     forward v = v + 1,\r
424     backward v = v - 1\r
425 }\r
426 </pre>\r
427 </td></tr>\r
428 </table>\r
429 \r
430 <h3>Longer examples</h3>\r
431 \r
432 <table border="1">\r
433 <tr><td><pre>\r
434 publid static int findIndex(List&lt;String&gt; strings, String target) {\r
435     int low=0, high=xs.size();\r
436     while(high-low &gt; 1) {\r
437         int middle = (low + high)/2;\r
438         int cmp = xs.get(middle).compareTo(target);\r
439         if(cmp &lt; 0)\r
440             low = middle;\r
441         else if(cmp &gt; 0)\r
442             high = middle;\r
443         else\r
444             return middle;\r
445     }\r
446 }\r
447 </pre>\r
448 </td><td><pre>\r
449 findIndex :: [String] -> String -> Integer\r
450 findIndex strings target = loop 0 (length strings) \r
451   where\r
452     loop low high \r
453     | high - low > 1 = do\r
454         middle = (low + high) `div` 2\r
455         cmp = compare (xs!middle) target\r
456         if cmp &lt; 0\r
457         then loop middle high\r
458         else if cmp &gt; 0\r
459         then loop low middle\r
460         else middle\r
461     | otherwise = low \r
462 </pre>\r
463 </td></tr>\r
464 </table>\r
465 \r
466 </body></html>