wtd
|
Posted: Thu Mar 16, 2006 6:27 pm Post subject: Closure demos in many languages |
|
|
In Perl:
code: | sub foo {
my $a = shift @_;
return sub {
my $b = shift @_;
return $a * $b;
};
}
my $bar = foo(4);
print $bar->(3) . "\n"; |
Or:
code: | sub foo {
my $a = shift;
sub {
my $b = shift;
$a * $b
}
}
my $bar = foo(4);
print $bar->(3) . "\n" |
And in Ruby:
code: | def foo(a)
lambda { |b| a * b }
end
bar = foo(4)
puts bar.call(3) |
Or:
code: | foo = lambda { |a| lambda { |b| a * b } }
bar = foo.call(4)
puts bar.call(3) |
In Io:
code: | foo := method(a, block(b, a * b))
bar := foo(4)
bar(3) println |
In O'Caml:
code: | let foo a = fun b -> a * b
let bar = foo 4;;
Printf.printf "%d\n" (bar 3) |
Or:
code: | let foo a b = a * b
let bar = foo 4;;
Printf.printf "%d\n" (bar 3) |
In GNU Smalltalk:
code: | !Object methodsFor: 'closure demo'!
foo: a
^ [ :b | a * b ]
!!
| bar |
bar := Object foo: 4.
(bar value: 3) printNl ! |
Or:
code: | | foo bar |
foo := [ :a | [ :b | a * b ] ].
bar := foo value: 4.
(bar value: 3) printNl ! |
In Haskell:
code: | foo a = \b -> a * b
bar = foo 4
main = print $ bar 3 |
Or:
code: | foo a b = a * b
bar = foo 4
main = print $ bar 3 |
In Java 5.0:
code: | interface Function<In, Out> {
public Out run(In input);
}
public class Test {
public static Function<Integer, Integer> foo(final Integer a) {
return new Function<Integer, Integer>() {
public Integer run(Integer b) {
return a * b;
}
};
}
public static void main(String[] args) {
Function<Integer, Integer> bar = foo(4);
System.out.println(bar.run(3));
}
} |
In SML/NJ:
code: | open Format;
fun foo a = fn b => a * b;
val bar = foo 4;
print (format "%d\n" [INT (bar 3)]); |
Or:
code: | open Format;
fun foo a b = a * b;
val bar = foo 4;
print (format "%d\n" [INT (bar 3)]); |
In Scala:
code: | object Test {
def foo(a : Int) = b : Int => a + b;
val bar = foo(4);
def main(args: Array[String]) =
Console println bar(3)
} |
-------------------------------------------------------------------------
In Perl:
code: | sub paid_more {
my $amount = shift;
sub {
my $employee = shift;
$employee->{salary} > $amount
}
}
my $highly_paid = paid_more 150;
print "Whoa!\n" if $highly_paid->({salary => 234}) |
In Ruby:
code: | paid_more = lambda { |amount| lambda { |employee| employee["salary"] > amount } }
highly_paid = paid_more.call(150)
puts "Whoa!" if highly_paid.call({"salary" => 234}) |
In Io:
code: | paidMore := method(amount, block(employee, employee salary > amount))
highlyPaid := paidMore(150)
if(highlyPaid(Object clone do(salary := 234)), "Whoa!" println) |
In O'Caml:
code: | type employee = {salary:int}
let paid_more amount {salary=s} = s > amount
let highly_paid = paid_more 150;;
if highly_paid {salary=234} then print_endline "Whoa!" |
In GNU Smalltalk:
code: | Object subclass: #Employee
instanceVariableNames: 'salary'
classVariableNames: ''
poolDictionaries: ''
category: nil.
!Salary class methodsFor: 'instance creation'!
newWithSalary: amount
| r |
r := super new.
r initWithSalary: amount.
^ r
!!
!Salary methodsFor: 'initialization'!
initWithSalary: amount
salary := amount
!!
!Salary methodsFor: 'accessors'!
salary
^ salary
!
setSalary: amount
salary := amount.
^ self
!!
| paidMore highlyPaid |
paidMore := [ :amount |
[ :employee | employee salary > amount ]
].
highlyPaid = paidMore value: 150.
(highlyPaid value: (Employee newWithSalary: 234)) ifTrue: [
'Whoa!' printNl
] ! |
In Haskell:
code: | data Employee = Employee Int
paidMore amount (Employee salary) = salary > amount
highlyPaid = paidMore 150
main =
if highlyPaid (Employee 234) then
putStrLn "Whoa!" |
In Java 5.0:
code: | interface Function<In, Out> {
public Out run(In input);
}
class Employee {
private int salary;
public Employee(int salary) {
this.salary = salary;
}
public int getSalary() {
return salary;
}
}
public class Test {
public static Function<Employee, Boolean> paidMore(final Integer amount) {
return new Function<Employee, Boolean>() {
public Boolean run(Employee emp) {
return emp.getSalary() > amount;
}
};
}
public static void main(String[] args) {
Function<Employee, Boolean> highlyPaid = paidMore(150);
System.out.println(highlyPaid.run(new Employee(234)));
}
} |
In SML/NJ:
code: | datatype employee = Employee of {salary:int};
fun paidMore amount (Employee {salary=s}) = s > amount;
val highlyPaid = paidMore 150;
if highlyPaid (Employee {salary=234}) then
print "Whoa!\n"
else (); |
In Scala:
code: | object Test {
case class Employee(salary : Int);
def paidMore(amount : Int) =
employee : Employee => employee.salary > amount;
val highlyPaid = paidMore(150);
def main(args: Array[String]) =
if (highlyPaid(Employee(salary: 234)))
Console println "Whoa!"
} |
|
|
|