Summary: Lambda syntax
Collin Fagan
collin.fagan at gmail.com
Thu Mar 18 20:30:54 PDT 2010
Thank you Jesse.
I went ahead and copied the examples Peter Levart used for his latest proposal.
FunctionType:
ResultType ':('ParameterTypes_opt ')' Throws_opt
ExpressionLambda:
':(' ParameterList_opt ')' '(' Expression ')'
StatementLambda:
':(' ParameterList_opt ')' '{' Statements '}'
FunctionInvocationExpression:
FunctionTypedExpression '::(' ExpressionList_opt ')'
int:() two = (2);
assert two::() == 2;
String :(File) throws IOException readFile = :(File f){
Reader in = new FileReader(f);
try {
char[] buf = new char[8192];
StringBuilder sb = new StringBuilder();
int nread;
while ((nread = in.read(buf)) >= 0)
sb.append(buf, 0, nread);
return sb.toString();
}
finally {
try { in.close(); } catch (IOException e) { // ignore }
}
}
String text = readFile::(new File("/etc/passwd"));
R:() throws E :(R:(A) throws E) :=
:(R:(A) throws E lambda, A arg){
return lambda::(arg));
};
R:(A) throws E lambda = :(A a){
if (a == null)
throw new E();
return new R(a);
};
R:() throws E curried := lambda:(new A());
R r = curried::();
On Thu, Mar 18, 2010 at 9:05 PM, Jesse Kuhnert <jkuhnert at gmail.com> wrote:
> Going off the list I'd put this one up with #s 5 and 1 as far as being
> easy on the eyes and something that feels like it fits goes. Assuming
> there aren't any technical dragons lurking in the details.
>
> On Thursday, March 18, 2010, Collin Fagan <collin.fagan at gmail.com> wrote:
>> Hi Stefan,
>>
>> I was trying to get a little feedback on some of my ideas but I guess
>> it got missed.
>>
>> Can we use : in the lambda syntax? Off the top of my head I think it's
>> only used in the for-each loop and the ternary operator.
>>
>> If : is up for grabs then my next question is: can we infix this
>> operator instead of prefixing it?
>>
>> Examples: (borrowed from an earlier post by Rémi Forax)
>>
>> A function that takes an int and returns an int:
>>
>> int(int)
>>
>> becomes
>>
>> int:(int)
>>
>> and ...
>>
>> A function that takes an int and returns nothing:
>> void:(int)
>>
>> A function that takes two ints and returns an int:
>> int:(int, int)
>>
>> A function that throws an Exception:
>> int:(int) throws Exception
>>
>> A function that takes a function that throws an Exception:
>> int:(int:(int) throws Exception)
>>
>> Grammar:
>> ResultType :( TypeList ) throws ExceptionList
>>
>> This syntax reads left to right in a way that is similar to method
>> signatures, with the method name replaced by a ':' .
>>
>> Now when we get to the curry examples I would like to customize the
>> assignment operator. Since I chose ':' how about using ':=' as the
>> "curry operator" ?
>>
>> Examples: (borrowed from an earlier post by Bob Foster)
>>
>> class A {
>> void foo(int x) { ... }
>> void :(int) fun = { ... }
>> }
>>
>> // curry examples
>> A a = new A();
>> void:() f1 := a.foo(2);
>> void:() f2 := a.foo();
>> void:() f3 := A.fun(2);
>>
>> Finally, to stay with the theme, invoking a lambda would use '::'
>>
>> f1::();
>> f2::();
>> f3::();
>>
>> Optionally this syntax could be: f1.:(); or f1:.(); but I found
>> neither of those particularly compelling.
>>
>> I *think* the : is no harder to type then the #. (At least on the
>> English keyboards I'm used to.)
>> The := construct is used in other languages (like Pascal, Eiffel and
>> Smalltalk) but admittedly not for curring.
>> The :: construct is used in C++, but again, not in the way I plan on using it.
>>
>> I could continue to flesh this out into a real proposal if people
>> found the syntax compelling. If I've committed any egregious sin in my
>> post I apologize.
>>
>> Thank you,
>>
>> - Collin
>>
>> On Thu, Mar 18, 2010 at 6:09 PM, Stefan Schulz <schulz at the-loom.de> wrote:
>>> So I summed up the proposed lambda syntaxes, hopefully picked all of
>>> them from the list. The summary is below, and the nicer to read document
>>> version is as before at the following URL:
>>> Lambda Syntaxes <http://docs.google.com/View?id=ddhp95vd_15gqnv8xqm>
>>>
>>> In case of mistakes or missed proposals, let me know.
>>>
>>> Cheers,
>>> Stefan
>>>
>>>
>>> 0. Common syntax
>>> Block:
>>> { BlockStatements }
>>>
>>> BlockStatements:
>>> BlockStatement
>>> BlockStatements BlockStatement
>>>
>>> BlockStatement:
>>> LocalVariableDeclarationStatement
>>> ClassDeclaration
>>> Statement
>>>
>>> ParameterList:
>>> Parameter
>>> Parameter, ParameterList
>>>
>>> Parameter:
>>> Type Identifier
>>>
>>> 1. Straw-man, latest
>>> Proposes two lambda definitions: lambda expressions and statements.
>>> Return type is inferred and throws meant to be transparent.
>>>
>>> LambdaExpression:
>>> # ( ParameterListopt ) ( Expression )
>>> # ( ParameterListopt ) Block
>>>
>>> Examples:
>>> #() ( 7 );
>>> #(Event e) { handle(e); }
>>> #(File f) { return f.getName(); };
>>> #(#R(A)(throws E) l, A a) ( #() ( l.(a) ) );
>>>
>>>
>>> 2. Rémy Forax, 05 Jan 2010
>>> Proposes to use a keyword lambda instead of #.
>>>
>>> LambdaExpression:
>>> lambda ( ParameterListopt ) ( Expression )
>>> lambda ( ParameterListopt ) Block
>>>
>>> Examples:
>>> lambda() ( 7 );
>>> lambda(Event e) { handle(e); };
>>> lambda(File f) { return f.getName(); };
>>> lambda(#R(A)(throws E) l, A a) ( lambda() ( l.(a) ) );
>>>
>>> 3. Peter Levart, 26 Jan 2010
>>> LambdaExpression:
>>> # ( ParametersListopt ResultParameteropt ) Block
>>>
>>> ResultParameter:
>>> : Parameter
>>>
>>> Examples:
>>> #(: int i) { i = 7 };
>>> #(Event e) { handle(e); };
>>> #(File f : String s) { s = f.getName(); };
>>> #(#(A: R throws E) l, A a : #(: R throws E) c) { c = #(: R r) { r =
>>> l.(a); }; };
>>>
>>> 4. Peter Levart, 1 Feb 2010
>>> LambdaExpression:
>>> # ( ParameterListopt -> ReturnTypeopt ) Block
>>>
>>> Examples:
>>> #(->int) { return 7; };
>>> #(Event e -> void) { handle(e); };
>>> #(File f -> String) { return f.getName(); };
>>> #(#(A -> R throws E) l, A a
>>> -> #(-> R throws E)) { return #(->R) { return l.(a); }; };
>>>
>>> 5. Neal Gafter, 9 Feb 2010
>>> LambdaExpression:
>>> ( ParameterListopt ) -> Expression
>>> ( ParameterListopt ) -> Block
>>>
>>> Examples:
>>> () -> 7;
>>> (Event e) -> { handle(e); };
>>> (File f) -> { return f.getName(); };
>>> ((A) -> R throws E l, A a) -> () -> l.(a);
>>>
>>> 6. BGGA
>>> LambdaExpression:
>>> { ParameterListopt => BlockStatementsopt Expressionopt }
>>>
>>> Examples:
>>> { => 7 }
>>> { Event e => handle(e); };
>>> { File f => f.getName() };
>>> { { A => R throws E } l, A a => { => l.(a) } };
>>>
>>> 7. Peter Levart, 2 Mar 2010
>>> LambdaExpression:
>>> ( ParameterListopt -> BlockStatementsopt Expression )
>>>
>>> Examples:
>>> ( -> 7 );
>>> ( Event e -> handle(e); void };
>>> ( File f -> f.getName() );
>>> ( (A -> R throws E ) l, A a -> ( -> l.(a) ) );
>>>
>>> 8. Howard Lovatt, 11 Mar 2010
>>> LambdaExpression:
>>> new #< Signature > ( BlockStatementsopt Expression )
>>> Signature:
>>> ReturnType ( ParameterListopt ) Throwsopt
>>>
>>> Examples:
>>> new #< int() > (7);
>>> new #< void(Event e) > (handle(e); null);
>>> new #< String(File f) > (f.getName());
>>> #< #< R() throws E >( #< R( A ) throws E > l, A a ) >
>>> ( new #< R() throws E >()( l.( a ) );
>>>
>>> 9. C++ (via Stephen Colebourne), 14 Mar 2010
>>> (translated to lambda-use)
>>>
>>> LambdaExpression:
>>>
>
More information about the lambda-dev
mailing list