Search Results


8 matches found for 'Scala'

Forall and Exists in Scala

... } iter(bound) // bound is some integer we plug in, e.g. 2000 } The above shows Scala code for the forall function, which takes in a predicate function and a set, and checks every value in the set if they satisfy the predicate.


A quick look at Scala and the syntax

The syntax in Scala can look confusing very fast.. def exists(s: Set, p: Int => Boolean): Boolean = !forall(s, (x: Int) => !p(x)) p here is a function parameter; okay, I get that.


Square each element of a Scala list

There are two practical ways to square each element of a list, and return the new list. For example, if we input a List(5, 10), we should have a List(25, 100). Method 1: Pattern Matching def squareList(xs: List[Int]): List[Int] = xs match { case Nil => xs case y :: ys => y * y :: squareList(ys) } If xs is empty, case Nil will trigger, simply returning the list that was passed in to squareList.


Scala Anonymous Functions

Material: https://class.coursera.org/progfun-005/ A type is basically an alias. It allows for neater code, and simplified readability. Since we can have function types, the code can get really messy when left raw.


Coin Change Denominations

... this coin and instead, choose the next coin at index \(i+1\). Solution With the power of Scala and recursions, we can make a function that calculates the total number of money denominations, given 2 parameters -- the amount of money and the list of allowed coins to use.


Summation with rationals

Given some lower bound a = na/da and upper bound nb/db, write a function that calculates the summation of f(nk/dk), where k is the index, n represents numerator, and d represents denominator. An example: if a =1/2 and b = 4/5, then you should calculate f(1/2)+f(2/3)+f(2/4)+ f(3/4)+ f(3/5)+f(4/5) def sum (f: Rational => Rational)(a: Rational, b: Rational) : Rational = { val k = a val total = new Rational(0, 1) def innerSum (f: Rational => Rational) ( k: Rational, total : Rational) : Rational = { if (a <= k && k <= b && k.


Phone Number Mnemonics in Python

... of simplicity and organization - like many algorithms with functional programming languages like Scala, recursive functions in general look neater, nicer, and easier to categorize. That is not to say that recursive functions aren't more difficult to write, but thinking about algorithms recursively will undoubtedly strengthen your toolset for solving more complicated problems.


DataFrames (a software engineer's perspective)

... is still pretty young and early in development. Spark DataFrames Spark is primarily Java / Scala based, which might be difficult to work with when passing datasets over from Python.