2013-08-18 22 views
-1

我想要做到這一點:無,一些和條件處理

名稱爲abc的變量必須爲無,除非複雜處理的結果爲真。 我寫的和回答的一個開端,但它不工作:

def abc={ 
None 
copie.getRoot().asInstanceOf[DefaultMutableTreeNode].children() foreach ({ 
    site => <...more things after...> 
} 

在<更多的事情>後,你可以找到如結果一些(站點)

但編譯器不接受這個命令,我的意思是「無」,後面是一些條件,最終由Some(xxx)完成。如果我在條件之後加上「無」,結果當然總是「無」,並且這不是預期的結果。

你能告訴我它是否可以這樣工作,以及如何? 否則我該如何繼續?


@Robin:你看到正確的:我想就像如果是在Java中:結果應該是沒有或者一些(XXX),但令人驚訝的,如果我把「無」在塊的開始,後「無」條件語句,它偶爾返回Some(xxx),eclipse的編譯器不接受我的代碼。 所以第一個問題可能是:這個數量級(無其次是一些conditionnal治療最終返回一些(XXX)右 例如:

def abc():Option[DefaultMutableTreeNode]={ 
    None 
    MyTree.getRoot().children() foreach{site=> 
     if (site.toBeImported()) Some(site) 
      else site.children() foreach {type=> 
       if (type.toBeImported()) Some(type) 
     } 
    } 
} 

這裏這個函數返回None,有些(網站)如果一個網站是「toBeImported」(如果很多,最後一個將被返回),或者一些(類型)如果類型是「toBeImported」(也是最後一個)。 這不是我在我的程序中所具有的,但它總結得很好。想法

+0

爲什麼你需要'None'?無論如何,你的'abc'是'Unit'返回類型,所以你不希望它返回任何東西。 'abc'的目的是什麼? – tkroman

+1

@lolvely您正在考慮Java返回關鍵字,這是一個立即停止執行當前方法的命令。 Scala的返回概念是「在執行函數結束時在表達式中獲得的任何值,是該函數的返回值」。 –

+0

@Robin:你看到了正確的,但是如果我在塊的開始處和「無」之後放置「無」,條件語句常常返回Some(xxx),這是錯誤的。 這個訂單是正確的嗎? 例如: 'ABC DEF():選項[DefaultMutableTreeNode] = { 無 MyTree.getRoot()的兒童()的foreach {站點=> 如果(site.toBeImported())一些(站點) 其他站點。 children()foreach {type => if(type.toBeImported())Some(type) } } }' – lolveley

回答

1

我不知道這是你的意思,但在這裏是我的嘗試:

object CondSO extends App { 
    def condition(site: String): Boolean = site.contains("flow") 

    def complexCalc(db: List[String]) = db.filter(condition) 

    // abc is not a variable (as addressed in original Q), but rather a method 
    def abc(db: List[String]): Option[String] = 
    // orig. Q was a bit confusing what result is - boolean or something else? 
    // so, here it's returning a list of results 
    complexCalc(db).headOption 

    // second version - the "for" approach 
    def abc2(db: List[String]): Option[String] = (
    for (site <- db if condition(site)) yield site 
    ).headOption 

    // third version - using return 
    // probably fastest option. IMO other options could be 
    // similarly fast if they would be rewritten to use stream 
    // (they construct auxiliary list with all matching sites, not only first one) 
    def abc3(db: List[String]): Option[String] = { 
    for (site <- db if condition(site)) return Some(site) 
    None 
    } 

    // last version - custom foreach 
    implicit class IterablePimps[A](val i: Iterable[A]) { 
    def foreachWithReturn[B](f: A => Option[B]): Option[B] = { 
     while (i.iterator.hasNext) 
     f(i.iterator.next()) match { 
      case a: Some[B] => return a 
      case _ => 
     } 
     None 
    } 
    } 

    def abc4(db: List[String]): Option[String] = 
    db.foreachWithReturn(s => if (condition(s)) Some(s) else None) 

    // testing section 
    val dbs = Map[String, List[String]](
    "empty " -> List(), 
    "present" -> List("google.com", "stackoverflow.com"), 
    "absent " -> List("root.cz", "abclinuxu.cz") 
) 

    val funcs = Map[String, (List[String]) => Option[String]](
    "filter" -> abc, 
    "for " -> abc2, 
    "return" -> abc3, 
    "pimp " -> abc4 
) 

    for { 
    db <- dbs 
    f <- funcs 
    } println(s"Applying ${f._1} on list ${db._1}: ${f._2(db._2)}") 
} 

輸出:

Applying filter on list empty : None 
Applying for on list empty : None 
Applying return on list empty : None 
Applying pimp on list empty : None 
Applying filter on list present: Some(stackoverflow.com) 
Applying for on list present: Some(stackoverflow.com) 
Applying return on list present: Some(stackoverflow.com) 
Applying pimp on list present: Some(stackoverflow.com) 
Applying filter on list absent : None 
Applying for on list absent : None 
Applying return on list absent : None 
Applying pimp on list absent : None 

編輯:修改方法返回一個結果,在沒有選項的列表。添加更多可能的解決方案(基於新信息frmo提問者)