Skip to content

Instantly share code, notes, and snippets.

View kevinmeredith's full-sized avatar

Kevin Meredith kevinmeredith

  • https://twitter.com/Gentmen
View GitHub Profile
@kevinmeredith
kevinmeredith / Monoid.scala
Created October 24, 2013 02:27
Exercise from FP in Scala
// 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
}
@kevinmeredith
kevinmeredith / MonoidTest.scala
Created November 14, 2013 03:10
Example of composing Monoids from the book, "Functional Programming in Scala"
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)
}
}
@kevinmeredith
kevinmeredith / Bag.scala
Created November 14, 2013 03:55
Creating a "bag" in Scala with foldLeft
// 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)
@kevinmeredith
kevinmeredith / AssertEx.html
Last active January 1, 2016 11:09
"Secrets of the JavaScript Ninja" home-made assert
<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));
@kevinmeredith
kevinmeredith / MemoizeWClosure.js
Created December 27, 2013 16:52
Power of closures (from Secrets of the JavaScript Ninja)
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() {
@kevinmeredith
kevinmeredith / Applicative.scala
Last active January 2, 2016 08:29
Question wrt Applicative#apply from FP in Scala
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,
@kevinmeredith
kevinmeredith / map3.scala
Created January 6, 2014 16:24
for comprehension trying to use map (rather than flatMap)
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
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
@kevinmeredith
kevinmeredith / PersonUtil.scala
Created February 11, 2014 16:01
PersonReads
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))
@kevinmeredith
kevinmeredith / Test.scala
Last active August 29, 2015 13:56
Why does this work if Writes[Parent] should only be implicit?
// Parent.java
public class Parent {
private String name;
private Child child;
public Parent(String name, Child child) {
this.name = name;
this.child = child;
}