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