Skip to content

A curated collection of useful little scala functions that you can understand quickly.

Notifications You must be signed in to change notification settings

seompark/30-seconds-of-scala

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

30-seconds-of-scala Build Status

Curated collection of useful little scala functions that you can understand quickly.

Table of Contents

Array

View contents

Math

View contents

I/O

View contents

Array

chunk

Chunks an Vector into smaller Vector of specified size.

def chunk[T](xs: Vector[T], size: Int): Vector[Vector[T]] = {
    if (xs.lengthCompare(size) <= 0) xs +: Vector()
    else (xs take size) +: chunk(xs drop size, size)
}

indexOf

Find first index of element in the Vector.

def indexOf[T](xs: Vector[T], el: T): Option[Int] = {
    val indexes = for ((v, i) <- xs.zipWithIndex if v equals el) yield Some(i)
    if(indexes.isEmpty) None else indexes.head
}

Math

gcd

Calculates the greatest common denominator (gcd) of an Vector of numbers.

def gcd(numbers: Vector[Int]): Int = numbers reduce gcd

def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)

lcm

Calculates the lowest common multiple (lcm) of an array of numbers.

def lcm(numbers: Vector[Int]): Int = {
    def lcm(a: Int, b: Int) = {
        val g = gcd(a, b)
        if (g == 0) 0 else a * b / g
    }
    numbers reduce lcm
}

binomialCoefficient

Compute the bionomial coefficient, n C k.

def binomialCoefficient(n: Int, k: Int): Int = {
    if (k < 0 || k > n) return 0
    if (k == 0 || k == n) return 1

    val j = k min (n - k)
    var c = 1

    for (i <- 0 until j) c = c * (n - i) / (i + 1)
    c
}

I/O

convertInputStreamToString

Converts InputStream to String.

def convertInputStreamToString(is: InputStream): String = Source.fromInputStream(is).mkString

readFileAsString

Reads string from file.

def readFileAsString(file: File): String = Source.fromFile(file).getLines.mkString

Thanks

This project is inspired by shekhargulati's little-java-functions, and his project is started as a Java fork of 30-seconds-of-code.

I'm planning to start implementing functions in little-java-functions project in Scala style. After that, I'll write my own functions. This project is just for my study and welcome PR and Issue for my bad implementation.

About

A curated collection of useful little scala functions that you can understand quickly.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages