Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Understanding lambdas
Understanding lambdas [message #1822841] Sun, 15 March 2020 14:14 Go to next message
Mohan Radhakrishnan is currently offline Mohan RadhakrishnanFriend
Messages: 19
Registered: July 2009
Junior Member
I have been trying to make this code read a text file line by line by calling 'lineIterator.next()' . Is that how it should be used ? I see that it stops after the first line.

val lineReader = new LineReader(r);
val AbstractIterator<String> lineIterator = [|
val result = lineReader.readLine
if (result==null)
self.endOfData
return result
]

The file is

Text
Red
Blue
Text
Red
Blue

and the call is like this.

do {
line = lineIterator.next()
println('File contents [' + line + ']')
}while( line != null )

It prints

File contents [Text]
File contents [null]

I may be missing something here. I can use plain Java but I think this Guava library should work too. Right ?

Thanks,
Mohan
Re: Understanding lambdas [message #1822842 is a reply to message #1822841] Sun, 15 March 2020 14:20 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13637
Registered: July 2009
Senior Member
Hi, i cannot reproduce this. can you please provide a complete example.

package x

import com.google.common.collect.AbstractIterator
import com.google.common.io.LineReader
import java.io.InputStreamReader
import java.io.ByteArrayInputStream
import java.nio.charset.StandardCharsets

class Y {

	def static void main(String[] args) {
		val lineReader = new LineReader(new InputStreamReader(
			new ByteArrayInputStream('''
				Text
				Red
				Blue
				Text
				Red
				Blue
			'''.toString.getBytes(StandardCharsets.UTF_8))
		));
		val AbstractIterator<String> lineIterator = [|
			val result = lineReader.readLine
			if (result == null)
				self.endOfData
			return result
		]
		var line = ""
		do {
			line = lineIterator.next()
			println('File contents [' + line + ']')
		} while (line != null)
	}

}



Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Understanding lambdas [message #1822843 is a reply to message #1822842] Sun, 15 March 2020 14:39 Go to previous messageGo to next message
Tamas Miklossy is currently offline Tamas MiklossyFriend
Messages: 114
Registered: February 2016
Senior Member
Hello Mohan!

You can get some inpiration from the Xtend Movies example:

https://www.eclipse.org/xtend/documentation/102_moviesexample.html#parsing-the-data

Hope that helps: Tamás
Re: Understanding lambdas [message #1822845 is a reply to message #1822843] Sun, 15 March 2020 15:05 Go to previous message
Mohan Radhakrishnan is currently offline Mohan RadhakrishnanFriend
Messages: 19
Registered: July 2009
Junior Member
Thanks.

Test code took shape like this because I thought a simple fluent API could help.
This could have a lurking bug though.

package com.test.generator

import com.google.common.collect.AbstractIterator
import com.google.common.io.LineReader
import java.io.FileReader
import java.util.ArrayList
import java.util.List
import java.util.Optional
import java.util.regex.Pattern

class DefinitionBuilderTest {
   
  IteratorBuilderTest builder = null
  
  static String line = null
  
  
  def DefinitionBuilderTest buildFileIterator(){
  	
  	//try(		
  	    val reader = new FileReader('D:/eclipse-jee-2019-12-M1-win32-x86_64/text.txt')//){

        val AbstractIterator<String> lineIterator = [|
        val lineReader = new LineReader(reader)
			val result = lineReader.readLine 
			if (result == null)
				self.endOfData
			return result
		  ]
  	     builder = new IteratorBuilderTest(Optional.of(lineIterator));
  	    //}  		
  	    return this
  }
  
  def String next(){
  		return builder.next();
  }
  
  	def static void main(String[] args) {
		val definitionBuilder = new DefinitionBuilderTest
		val dataIterator = definitionBuilder.buildFileIterator()
		do {
			line = dataIterator.next()
 		    println('File contents [' + line + ']')
		}while( line != null )
  
    }
 }
    

   class IteratorBuilderTest{
  	
   var AbstractIterator<String> lineIterator = null;
  	
  	new ( Optional<AbstractIterator<String>> fileIterator ){
  		this.lineIterator = fileIterator.get();
  	}
  	
  	 def String next(){
  	 	if( lineIterator.hasNext()){
  	 		
	  	   return lineIterator.next()
  	 	}
  	}
				
  }
Previous Topic:Token Splitting via custom lexer?
Next Topic:Get textual representation from model
Goto Forum:
  


Current Time: Sun Oct 25 16:09:39 GMT 2020

Powered by FUDForum. Page generated in 0.02441 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top