working on it ...

Filters

Explore Public Snippets

Sort by

Found 4 snippets

    public by marcelom  2174  0  6  0

    Guards within a comprehension

    Using predicates in a comprehension
    for(  x  <-  ( 1 to 10 ); if x % 2 == 0  ) yield x*x

    public by apellizzaro  3359  0  6  1

    Case class constructor

    Best practice on how to have a overloaded constructor for case classes
    Case Class Friend (michele:String, antonio: Int);
    
    Object Friend {
    
    	public Friend (i:Int, s:String) = Friend (s,i)
    }
    
    

    public by micurs  2207  0  7  0

    Extracting even numbers from an array in Scala

    Using yield to extract the even numbers from an array.
    val a = Array(3, 23, 12, 17, 20)
    val result = for (elem <- a if elem % 2 == 0 ) yield elem
    // result is Array(12, 20)
    
    // As alternative using filter you get the same result
    a.filter( a => a % 2 == 0)
    // or ...
    a.filter( _ % 2 == 0)
    

    public by micurs  3680  1  6  0

    Scala recursive function computing the products of all letters in a string

    Recursive scala function that compute the product of the Unicode codes of all letters in a string.
    def mUni( word: String ) : BigInt  = {
      if ( word.length <= 1 ) word(0) 
      else word(0) * mUni( word.drop(1) )
    }

    List Complete ( 4 snippets total )

    • Public Snippets
    • Channels Snippets