This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // EXERCISE 9: Implement a foldMap for IndexedSeq, a common supertype | |
| // for various data structures that provide efficient random access including Vector. | |
| // Your implementation should use the strategy of splitting the sequence in two,/ | |
| // recursively processing each half, and then adding the answers together with the | |
| // monoid. | |
| def foldMapV[A,B](v: IndexedSeq[A], m: Monoid[B])(f: A => B): B = { | |
| def go(seq: List[B], acc: B): B = seq match { | |
| case x :: xs => go(xs, m.op(acc, x)) | |
| case Nil => acc | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object ComposingMonoids { | |
| // Beginning of monoid compoisition (from FP in Scala) | |
| // https://github.com/pchiusano/fpinscala/.../.../17.answer.scala | |
| def productMonoid[A, B](A: Monoid[A], B: Monoid[B]): Monoid[(A, B)] = { | |
| new Monoid[(A, B)] { | |
| def op(x: (A,B), y: (A,B) ) = (A.op(x._1, y._1) , B.op(x._2, y._2)) | |
| val zero = (A.zero, B.zero) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Given a list of items, return a Map[A -> Int] where A is an element and Int is how many times it appears | |
| val result = List("a", "b", "a").foldLeft(Map[String, Int]())( (acc, el) => | |
| if (acc.contains(el) ) { | |
| val x = acc.get(el).get | |
| acc + (el -> (x+1)) | |
| } | |
| else { acc + (el -> 1) } ) | |
| assert(result == Map("a" -> 2, "b" -> 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <title>Test Suite</title> | |
| <script> | |
| (function() { | |
| var results; | |
| this.assert = function assert(value, desc) { | |
| var li = document.createElement("li"); | |
| li.className = value ? "pass" : "fail"; | |
| li.appendChild(document.createTextNode(desc)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Function.prototype.memoized = function(key) { | |
| this._values = this._values || {}; | |
| return this._values[key] !== undefined ? | |
| this._values[key] : | |
| this._values[key] = this.apply(this, arguments); | |
| } | |
| Function.prototype.memoize = function() { | |
| var fn = this; | |
| return function() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait Applicative[F[_]] extends Functor[F] { | |
| def map2[A,B,C](fa: F[A], fb: F[B])(f: (A,B) => C): F[C] | |
| def apply[A,B](fab: F[A => B])(fa: F[A]): F[B] = | |
| map2(fa, fab)((x, f) => f(x)) | |
| // [code elided] | |
| // Your comment says, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object TestForComprehensionMap3 { | |
| def map3(a: Option[Int], b: Option[Int], c: Option[Int])(f: (Int, Int, Int) => Option[Int]): Option[Int] = { | |
| a.flatMap(x => b.flatMap(y => c.flatMap(z => f(x,y,z) ) ) ) | |
| } | |
| def map3ForExpr(a: Option[Int], b: Option[Int], c: Option[Int])(f: (Int, Int, Int) => Option[Int]): Option[Int] = { | |
| for { | |
| x <- a | |
| y <- b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| scala> els | |
| res4: Int = 222 | |
| scala> opt | |
| res5: Some[Int] = Some(1) | |
| scala> opt.foldLeft(els)((_,elem) => identity(elem)) | |
| res9: Int = 1 | |
| scala> val optNone: Option[Int] = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package common | |
| import play.api.libs.json._ | |
| import play.api.libs.functional.syntax._ | |
| object PersonUtil { | |
| implicit val PersonReads: Reads[Person] = ( | |
| (JsPath \ "name").read[String] and | |
| (JsPath \ "age").read[Int] | |
| )((x,y) => new Person(x,y)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Parent.java | |
| public class Parent { | |
| private String name; | |
| private Child child; | |
| public Parent(String name, Child child) { | |
| this.name = name; | |
| this.child = child; | |
| } |