Lambdas are Available- Java 8 Snapshot

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 10426 Views - Last Post: 06 September 2013 - 02:32 PM

#1 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,479
  • Joined: 27-December 08

Lambdas are Available- Java 8 Snapshot

Post icon  Posted 02 June 2013 - 10:24 AM

Has anyone downloaded and played around with Project Lambda yet? It looks like it provides lambda support with JDK 7, in an attempt to familiarize developers for JDK 8. The Tutoracles now feature lambas as well. For those who develop with languages that already include lambdas, like C# or Functional languages, how do Java lambdas stack up (pun intended)? Are they on par with what you would expect? Other thoughts?

Quote

This page provides an Early Access of OpenJDK with Lambda (JSR 335) support. The Lambda project aims to support programming in a multicore environment by adding closures and related features to the Java language

For documentations and other details, please see the Lambda project page.

Please note:
The Lambda project has used source files that are not yet available in JDK8; therefore, these early access builds are created using the latest OpenJDK 7 source repository. This project will merge into OpenJDK 8 when the source files are available.


Is This A Good Question/Topic? 2
  • +

Replies To: Lambdas are Available- Java 8 Snapshot

#2 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,965
  • Joined: 08-June 10

Re: Lambdas are Available- Java 8 Snapshot

Posted 02 June 2013 - 01:11 PM

Honestly, I'm not a fan of the syntax they shown in that tutorial. It's basically the same one used in .NET 3.0, which I never liked either. (Much preferred the 2.0 version.)

To borrow one of the examples from the tutorial:
Block<Integer> myBlock = (y) -> 
{
	// Does things with the "y" parameter here.
};



Why not just keep it using a more familiar syntax, like:
int myBlock = int(int y) {
	// Does things with the "y" parameter here.
};


Or even do things like this, if the type ting is something to be avoided:
int myBlock = closure(y) {
	// Does things with the "y" parameter here.
};



Adding a new operator for this just seems unnecessary, really. - But then, this is just my opinion. I'm sure this syntax looks cleaner to others. Maybe it's just the years of Javascript/PHP coding that makes me see it this way :)
Was This Post Helpful? 1
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,418
  • Joined: 29-May 08

Re: Lambdas are Available- Java 8 Snapshot

Posted 02 June 2013 - 01:36 PM

The probably went with that syntax to avoid antiquities, and still let existing code compile.
You really don't the meaning of code changing.
Was This Post Helpful? 1
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,418
  • Joined: 29-May 08

Re: Lambdas are Available- Java 8 Snapshot

Posted 02 June 2013 - 01:42 PM

Also is it like .net lambdas where what they compile to, is determine by the target type.

Expression<Func<T>> or Func<T> or Expression<Action<T>> or Action<T>

This post has been edited by AdamSpeight2008: 02 June 2013 - 01:43 PM

Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,746
  • Joined: 19-March 11

Re: Lambdas are Available- Java 8 Snapshot

Posted 02 June 2013 - 02:03 PM

Can I just say, I love that "tutoracle" is catching on.


As for the syntax - I haven't looked at it very closely. That's why I learned Scala, after all. :) But from what I've seen, it really looks a little clumsy. No surprise, they're sort of hamstrung by the existing syntax. I still say it's time to break off and start fresh, but nobody agrees with me on that, so I guess I'll have to learn how the new syntax works sooner or later - if only to keep my expert badge...

From the license:

Quote

Additional Trial Programs
We may include additional trial programs with the Programs licensed under this agreement which You download or access in the hosted environment. You will have 30 days from the delivery date or hosted environment access date to evaluate these additional trial programs. Any use of these trial programs after the 30 day trial period requires You to obtain the applicable license. Any additional trial programs are provided "as is" and We do not provide technical support or any warranties for these programs.


There was some flap about some unrelated shtuff bundled in with the jdk recently - is there any weirdware in the lambda package?
Was This Post Helpful? 1
  • +
  • -

#6 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,965
  • Joined: 08-June 10

Re: Lambdas are Available- Java 8 Snapshot

Posted 02 June 2013 - 02:09 PM

Quote

The probably went with that syntax to avoid antiquities, and still let existing code compile.
You really don't the meaning of code changing.

But the syntax I suggested would not break existing code, even though the syntax is familiar. It would just add meaning to the syntax in the right context, not otherwise alter it's meaning.

It's like how PHP introduced lambdas: using the exact same syntax they used for normal functions:
$func = function($input) { }


Which follows Javascript syntax exactly. Those compilers have no trouble deducing the differences between "normal" function declarations and lambdas, even though the keywords and syntax for both is more or less identical. Why would the Java interpreter not be capable of that as well?
Was This Post Helpful? 1
  • +
  • -

#7 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3253
  • View blog
  • Posts: 10,908
  • Joined: 12-December 12

Re: Lambdas are Available- Java 8 Snapshot

Posted 02 June 2013 - 02:14 PM

View Postjon.kiparsky, on 02 June 2013 - 09:03 PM, said:

Can I just say, I love that "tutoracle" is catching on.

I couldn't remember this word and came up with "doracles", which I also like :bigsmile:
Was This Post Helpful? 2
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,418
  • Joined: 29-May 08

Re: Lambdas are Available- Java 8 Snapshot

Posted 02 June 2013 - 02:44 PM

Time to complain was when they were making the design decisions.
Have a look here JSR 335 see if there are any information why other syntax were unsuitable.

or here

This post has been edited by AdamSpeight2008: 02 June 2013 - 02:45 PM

Was This Post Helpful? 1
  • +
  • -

#9 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,030
  • Joined: 11-December 07

Re: Lambdas are Available- Java 8 Snapshot

Posted 03 June 2013 - 01:33 AM

I've only glanced at the links but I think I'm quite happy with the syntax. If I can do something like this:

button.addActionListener(e -> System.out.println("Button pressed"));


or more likely

button.addActionListener(e -> confirmSelection());


then that seems nice and concise to me.

The syntax actually reminds me of SML. I guess most Java programmers won't be familiar with it but it won't take long for people to get accustomed to it.

Interestingly, the tutorial calls the -> a token, not an operator.
Was This Post Helpful? 2
  • +
  • -

#10 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Lambdas are Available- Java 8 Snapshot

Posted 03 June 2013 - 07:22 PM

I'm a little confused. How is the type of a lambda expression defined? Even C++11 with it's weird anonymously typed lambdas has std::function<>. C# has Func<>, Delegate, and Action, Action<> and a means to define your own via the 'delegate' syntax. How is this done in Java?

read the tutorial, interface with only one method.

Quote

Interestingly, the tutorial calls the -> a token, not an operator.


all operators are tokens...with a few exceptions(the converse is not necessarily true however). you could say operator is a subtype of token or that operator implies token. That said I think i can explain why they chose to not be as specific as 'operator'; I think it's not really the best fit. operators are single tokens and are either unary or binary and left or right associative...again with a few exceptions. In this case there is a much more complex production than just an operator; you wouldn't call the 'if ... then ... else ... end' syntax of functional languages an operator would you(even though that is exactly what the ternary operator is)? Most importantly operators generate a new value from other values they take as arguments; this is true without exception. lambdas are more like constants really in that they don't take any values as arguments, constants that implicitly capture variables. '->' doesn't take two values as arguments, it takes two productions(one of which *can* be an expression).

now for those exceptions:
*ternary operator takes 3 arguments, consists of multiple characters and is a slightly more complex production than +, -, etc...
*the [] operator consists of multiple characters and is a more complex production than +, -, etc... it is however binary and depending on how you look at it it could have an associativity
*the call operator is just like the [] operator with the respect to my comments about syntax(semantically it is quite different)

This post has been edited by ishkabible: 03 June 2013 - 08:38 PM

Was This Post Helpful? 1
  • +
  • -

#11 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,030
  • Joined: 11-December 07

Re: Lambdas are Available- Java 8 Snapshot

Posted 04 June 2013 - 04:44 PM

Enough about syntax. Is this going to change the way we work in Java?

Swing

I'm really looking forward to being able to define Swing event handlers more concisely. That's nice but it's not earth-shattering.

Collections

Some new methods are:

Iterable.forEach(Consumer<? super T> action)
List.replaceAll(UnaryOperator<E> operator)
Collection.removeIf(Predicate<? super E> filter)
// and several other interesting things in the Map interface.



replaceAll is a bit like the map function in functional languages, except that it changes the underlying list instead of returning a new one. It's trivial to copy a list but I don't think you could use this to convert types. For example, I don't think this will compile, which is a shame:

listOfStrings.replaceAll(s -> Integer.parseInt(s));


removeIf fills the needs of filter! (or !filter)

Fold is conspicuous in its absence. That seems strange to me since it is the more fundamental functional operation of the three. (You can define map and filter in terms of fold). Does LinkedList come to the rescue? No. I think Oracle has missed a trick here.

Short of writing a ConsCell<T> class with foldr, foldl, map and filter methods, Java programmers are going to miss out on a lot of the fun they could have had with lambdas. I really hope that proper map and fold methods get added or that I have simply missed them.

Encapsulation

How many times have you written a private method that was only ever called from one other method? Now there is the option to define it within the calling method using a lambda. How about the following snippet. There are a few advantages. The helper function doesn't clutter the local namespace and it has fewer parameters, making it more readable and maintainable.

int binarySearch(final int needle, final int[] haystack) {
  BinaryFunction<Integer> doSearch = (min, max) -> {
    // Binary search algorithm (recursive or iterative) goes here.
  };
  doSearch.apply(0, haystack.length - 1);
}


Ternary Functions

I don't think there is an interface to do something like this:

TernaryFunction<A,B,C> something = (x, y, z) -> {/* do some stuff */};


or this

NaryFunction5<A,B,C,D,E> something = (v, w, x, y, z) -> {/* stuff here*/};


Neither is pretty and I don't know what a sensible cutoff for the number of arguments would be. However, I think most people will find the occasional use for 3 or even 4 arguments, and I think a lot of people will roll their own generic interfaces for stuff like this.

That's all I have just now. Is there anything you've been itching to do with lambdas? Will it be possible in Java 8?
Was This Post Helpful? 3
  • +
  • -

#12 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: Lambdas are Available- Java 8 Snapshot

Posted 08 June 2013 - 05:10 PM

View Postcfoley, on 04 June 2013 - 11:44 PM, said:

Enough about syntax. Is this going to change the way we work in Java?

Swing

I'm really looking forward to being able to define Swing event handlers more concisely.


Swing. Eww. Gross. :) I wouldn't use Swing for any new projects, and I most certainly wouldn't modify existing code to use lambdas, but I am looking forward to ->'ing in Java instead of =>'ing in C#.

I wonder if Google is going to update Dalvik so we can use them in Android.
Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,418
  • Joined: 29-May 08

Re: Lambdas are Available- Java 8 Snapshot

Posted 08 June 2013 - 05:44 PM

View Postcfoley, on 05 June 2013 - 12:44 AM, said:

listOfStrings.replaceAll(s -> Integer.parseInt(s));


Cos it won't work the return type is different to the input type. String[] -> Int[]

You need to do a projection. C# this would be a .Select
var numbers = listOfStrings.Select( s => Integer.Parse(s));



but this code has an issue. What if the contains some string that fails to parse into an integer.

Quote

removeIf fills the needs of filter! (or !filter)

Fold is conspicuous in its absence. That seems strange to me since it is the more fundamental functional operation of the three. (You can define map and filter in terms of fold). Does LinkedList come to the rescue? No. I think Oracle has missed a trick here.

Is there an .Aggregate ?

This post has been edited by AdamSpeight2008: 08 June 2013 - 05:50 PM

Was This Post Helpful? 0
  • +
  • -

#14 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2091
  • View blog
  • Posts: 3,186
  • Joined: 21-June 11

Re: Lambdas are Available- Java 8 Snapshot

Posted 09 June 2013 - 03:50 AM

View PostAdamSpeight2008, on 09 June 2013 - 02:44 AM, said:

View Postcfoley, on 05 June 2013 - 12:44 AM, said:

listOfStrings.replaceAll(s -> Integer.parseInt(s));


Cos it won't work the return type is different to the input type. String[] -> Int[]


I believe that was his point.

Quote

You need to do a projection. C# this would be a .Select


But there is no Select/map-like function for Lists, which is why cfoley was complaining about the limitations of only having a mutable version in the first place.

What cfoley might have missed though is that there is a map method for streams and any iterable can be turned into a stream through the stream method.

Quote

Quote

Fold is conspicuous in its absence.

Is there an .Aggregate ?


No, but there is a fold for streams.

This post has been edited by sepp2k: 09 June 2013 - 03:51 AM

Was This Post Helpful? 2
  • +
  • -

#15 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,418
  • Joined: 29-May 08

Re: Lambdas are Available- Java 8 Snapshot

Posted 09 June 2013 - 08:56 AM

I thought it be implemented on Iterable<T>, so that anything that is iterable can be used.
Like LINQ uses IEnumerable<T>
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2