Giter Site home page Giter Site logo

functional-scala's Introduction

My solutions + playground for the exercises of the book "Functional Programming in Scala" by Runár (first through version 10 of the book, since chapter 7 through the final edition).

Summary of cool stuff

Forcing tailrecursion by compiler

 @annotation.tailrec
    def FibAcc(n : Int, a : Int, b: Int) : Int = 

Reversing a List by using a fold

def reverse[A](as : List[A]) : List[A] = foldLeft(as,Nil : List[A])((b,a) => Cons(a,b))

Generating streams with unfold. Think of a state machine which outputs values of the streams. Once the machine halts, the stream ends.

  def unfold[A, S](z: S)(f: S => Option[(A, S)]): Stream[A] = f(z) match {
    case some((a, z2)) => cons(a, unfold(z2)(f))
    case data.none => Stream.empty
  }

The method unfold allows to give simple implementations of some methods, i.e. zipAll, zip, and tails (a stream of streams that are tails of a fixed stream).

   // in trait Stream[A]
   def zip[B](bs: Stream[B]): Stream[(A, B)] = unfold[(A, B), (Stream[A], Stream[B])]((this, bs))(sAsB => {
    val sa = sAsB._1
    val sb = sAsB._2
    if (sa.isEmpty || sb.isEmpty) data.none
    else some(((sa.head, sb.head), (sa.tail, sb.tail)))
  })

  def tails: Stream[Stream[A]] = Stream.append(unfold(this)(s => {
    if (s.isEmpty) data.none
    else some((s, s.tail))
  }), Stream(Stream.empty))
  
  // on companion object Stream
  def zipAll[A, B](as: Stream[A], bs: Stream[B]): Stream[(Option[A], Option[B])] = unfold((as, bs))(sAsB => {
    val sa = sAsB._1
    val sb = sAsB._2
    if (sa.isEmpty && sb.isEmpty) data.none
    else some(((if (sa.isEmpty) data.none else some(sa.head), if (sb.isEmpty) data.none else some(sb.head)), (sa.tail, sb.tail)))
  })

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.