English 中文(简体)
*. 操作员
原标题:Groovy *. Operators
  • 时间:2012-05-23 07:05:19
  •  标签:
  • groovy

我最近在《行动》中读到Groovy。在第七章中,它引入了“*.aperor ” 。当我运行关于这个操作员的代码时,我犯了一些错误。

class Invoice {                                          
    List    items                                        
    Date    date                                         
}                                                        
class LineItem {                                         
    Product product                                      
    int     count                                        
    int total() {                                        
        return product.dollar * count                    
    }                                                    
}                                                        
class Product {                                          
    String  name                                         
    def     dollar                                       
}                                                        

def ulcDate = new Date(107,0,1)
def ulc = new Product(dollar:1499, name: ULC )           
def ve  = new Product(dollar:499,  name: Visual Editor ) 

def invoices = [                                         
    new Invoice(date:ulcDate, items: [                   
        new LineItem(count:5, product:ulc),              
        new LineItem(count:1, product:ve)                
    ]),                                                  
    new Invoice(date:[107,1,2], items: [                 
        new LineItem(count:4, product:ve)                
    ])                                                   
]                                                        

//error
assert [5*1499, 499, 4*499] == invoices.items*.total()  

The last line will throw a exception. At first, i can explain why this error happend. The invocies is a List, and the element s type is Invoice. So directly using items will make a error. I attempt to fix it by using invoices.collect{it.items*.total()}

但还是有一个失败的主张。因此,我如何使主张成功以及为什么发票*.items*.tems*.tall ()会提出一个例外。

最佳回答

invoices* 的结果。 运算符是一个列表, 因此 invoices* 的结果是 invoices*. items 的结果是列表。 blackten() 可以应用到列表中, 并返回一个统一列表, 这样您就可以使用它从您 Listemists 列表中绘制一个 listems 列表列表中的 < code > listemts 。 然后您可以使用扩展运算符对元素应用 commont()

assert [5*1499, 499, 4*499] == invoices*.items.flatten()*.total()
问题回答

这并不能回答你的问题,但是,在你的发票类中也有一个总的方法,也许更好一些,就像这样:

int total() {
  items*.total().sum()
}                        

然后您可以查看 :

assert [5*1499 + 499, 4*499] == invoices*.total()  




相关问题
Groovy - how to exit each loop?

I m new to Grails/Groovy and am trying to find a node in a an xml file; I ve figured out how to iterate over all of them, but I want to exit the loop when the target node is found. I ve read that ...

Eclipse Spring Builder set properties with Groovy beans

I typically use groovy to construct simple bean but the Spring IDE plugin to eclipse fails to build when I try to set a property that is generated by groovy without an explicit setter. For example, ...

How can I get this snippet to work?

I d like to port a little piece of code from Ruby to Groovy, and I m stuck at this: def given(array,closure) { closure.delegate = array closure() } given([1,2,3,4]) { findAll { it > 4} ...

Changing the value in a map in Groovy

This is about a very basic program I m writing in Groovy. I have defined a map inside a method: def addItem() { print("Enter the item name: ") def itemName = reader.readLine() print(...

Is functional Clojure or imperative Groovy more readable?

OK, no cheating now. No, really, take a minute or two and try this out. What does "positions" do? Edit: simplified according to cgrand s suggestion. (defn redux [[current next] flag] [(if flag ...

热门标签