Skip to content

Instantly share code, notes, and snippets.

@ranraj
Last active March 22, 2017 04:22
Show Gist options
  • Save ranraj/36888c71ba21f10ebdfc305ed5b77c2b to your computer and use it in GitHub Desktop.
Save ranraj/36888c71ba21f10ebdfc305ed5b77c2b to your computer and use it in GitHub Desktop.
bigdata

Scala library - A view in the Functional programming perspective

Here is the Monad from scala library

implicit class MonadOps[+A](trav: TraversableOnce[A]) {
    def map[B](f: A => B): TraversableOnce[B] = trav.toIterator map f
    def flatMap[B](f: A => GenTraversableOnce[B]): TraversableOnce[B] = trav.toIterator flatMap f
    def withFilter(p: A => Boolean) = trav.toIterator filter p
    def filter(p: A => Boolean): TraversableOnce[A] = withFilter(p)
  }

Lets look some transfermations and actions available in scala

Transformation

map,flatMap, filter

map

maping to another type in collection

Scala doc says

Creates a new iterator that maps all produced values of this iterator to new values using a transformation function.

def map[B](f: A => B): Iterator[B] = new AbstractIterator[B]

flatMap

mapping to another type and flatterning it

Scala doc says

Creates a new iterator by applying a function to all values produced by this iterator and concatenating the results.

def flatMap[B](f: A => GenTraversableOnce[B]): Iterator[B]

filter

filtering to same type with applied function

Scala doc says

Returns an iterator over all the elements of this iterator that satisfy the predicate p. The order of the elements is preserved.

def filter(p: A => Boolean): Iterator[A]

Action

fold,reduce and aggregate

fold

Returns to single unit It takes the default parameter to accumulate the resulting value

Scala doc says

folds the elements of this traversable or iterator using the specified associative binary operator.

def fold[A1 >: A](z: A1)(op: (A1, A1) => A1): A1 

Usage

list.map(_.budget).fold(0)((a,b)=> a+b)

reduce

It do the same like fold but does not accept the default value The accumulater is the first value of the traversable

Scala doc says

Reduces the elements of this traversable or iterator using the specified associative binary operator.

def reduce[A1 >: A](op: (A1, A1)  A1): A1

aggregate

It works as like reduce and fold, but in parallel

Scala doc says

Aggregates the results of applying an operator to subsequent elements.

def aggregate[B](z:  B)(seqop: (B, A)  B, combop: (B, B)  B): B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment