File: higherOrder.nice

package info (click to toggle)
nice 0.9.10-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,260 kB
  • ctags: 6,867
  • sloc: java: 42,604; xml: 3,205; lisp: 1,079; makefile: 652; sh: 342; cpp: 21; awk: 3
file content (46 lines) | stat: -rw-r--r-- 917 bytes parent folder | download | duplicates (4)
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
package regtest.basic;

/** Tests higher order functions */

<T, U> U apply(T->U f, T x) = f(x);

<T, U> U apply2(T->U f, T x);
apply2(f, x) = f(x);

class High
{
  alike high( ()->void );

  String->void fieldFunction;
}

high(h@High, f)  { f(); return h; }

void test_parsingExp(High h) = (h.fieldFunction)("");
void test_parsingStmt(High h)
{
  (h.fieldFunction)("");
}

void test_higher_order()
{
  println(id("ID"));
  println((<T>(T x)=>x)("LAMBDA"));
  println(apply(<T>(T x)=>x, "LAMBDA"));
  println(apply(nativeToString, "Using native methods as first class values"));
  println(apply(id(intIdentity), 42));
}

String nativeToString(String) = native String Object.toString();

int intIdentity(int x) = id(x);

// Using int -> long implicit conversion in the returned value
long intWidening(int x) = x;

void unboundVariable(String v)
{
  throw new Error();
}

void passAsValue() = println(unboundVariable);