Skip to content

Instantly share code, notes, and snippets.

View joost-de-vries's full-sized avatar

Joost de Vries joost-de-vries

View GitHub Profile
@joost-de-vries
joost-de-vries / gist:7946056
Last active December 31, 2015 06:09
Workaround om Play json combinators te kunnen gebruiken bij case classes met één veld
import scala.language.higherKinds
import play.api.libs.functional.InvariantFunctor
object JsonCombinatorUtil {
/** workaround voor issue dat json combinators niet kunnen omgaan met case classes met 1 veld
* zie http://stackoverflow.com/questions/15042205/how-to-serialize-deserialize-case-classes-to-from-json-in-play-2-1 */
implicit class FormatBuilder[M[_], A](o: M[A]) {
def build[B](f1: A => B, f2: B => A)(implicit fu: InvariantFunctor[M]) =
fu.inmap[A, B](o, f1, f2)
}
@joost-de-vries
joost-de-vries / gist:8110503
Last active January 1, 2016 07:19
notes from Gilad Brachas talk wrt functor, monad, semigroup, monoid.The observation that a monoid is foldRightable is interesting.
a semigroup is addable
a monoid is foldRightable or aggregatable
(ie addable, zero)
a functor is mappable
a monad is chainable or flatmappable
strong suit of monads:
@joost-de-vries
joost-de-vries / gist:8825623
Created February 5, 2014 15:08
Compiling scala json parser combinators (from Play framework)
[error] /home/vagrant/wendwet/core/src/main/scala/annoteren/model/objectrollen.scala:78: overloaded method value format with alternatives:
[error] [T(in method format)(in method format)(in method format)(in method format)(in method format)(in method format)(in method format)(in method format)](w: play.api.libs.json.Writes[T(in method format)(in method format)(in method format)(in method format)(in method format)(in method format)(in method format)(in method format)])(implicit r: play.api.libs.json.Reads[T(in method format)(in method format)(in method format)(in method format)(in method format)(in method format)(in method format)(in method format)])play.api.libs.json.OFormat[T(in method format)(in method format)(in method format)(in method format)(in method format)(in method format)(in method format)(in method format)] <and>
[error] [T(in method format)(in method format)(in method format)(in method format)(in method format)(in method format)(in method format)(in method format)](r: play.api.libs.json.Reads[
#!/bin/bash
#
# =========================================================================
# Copyright 2014 Rado Buransky, Dominion Marine Media
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
@joost-de-vries
joost-de-vries / foldfun.sc
Last active August 29, 2015 14:02 — forked from headinthebox/gist:37c76829253388fd88e3
Erik Meijers foldleft implementation using foldright rewritten in Scala
object foldfun {
def foldr[A, B](f: A => B => B)(b: B)(s: Seq[A]): B = s match {
case Nil => b
case a :: as =>
f(a)(foldr(f)(b)(as))
} //> foldr: [A, B](f: A => (B => B))(b: B)(s: Seq[A])B
foldr { a: String => b: Int => a.toInt * b.toInt }(7)(Seq("1", "2", "3"))
//> res0: Int = 42
@joost-de-vries
joost-de-vries / gist:bf58c40d46f04dc7689a
Last active August 29, 2015 14:12
Does Java 8 obviate Scala?

Java 8 definitely took some 'oomph' out of mainstream Scala adoption. I do see a lot of Scala adoption at the moment though. Currently it's mostly in ecommerce.

For me Java 8 is too little too late. The things I really miss in Java 8 are:

  • pattern matching Not only handling the cases but more importantly deconstructing the object into variables.
  • type inferencing To prevent Javas so called type stutter SessionFactory sessionFactory = createSessionFactory();. And also to not have to type tedious generic declarations like
Map<String, Observable<JsonNode>> observableMap = stream.collect(mapCollector);

A lot of explanations of monads start from a mathematical terminology. Which is unnecesarily obscure if you just want understand why they're handy in programming.

If you have a background in OO you can see Monad as an interface. Similarly Monoid can also be viewed as an interface. Gilad Bracha said that something that supports a function like mappend should really be called Addable (instead of Semi-group). Because that's what we can do with it. And if it also supports a function like mempty it should be called Foldable (instead of Monoid); because it provides the two arguments that a fold function requires.

Following the same naming convention the name Chainable would be more intuïtive than Monad. Because the reason that we value bind (or flatMap) so much is that we can chain (or 'compose) smaller parts into bigger parts and automatically handle alternate flows or side effects.

Because that's why Chainables (or Monads) are a big deal: we can build bigger things from flatmap invocations; you can cha

@joost-de-vries
joost-de-vries / gist:398e0d448ac5f1981038
Created December 30, 2014 11:38
Haskell is object oriented

I've been thinking about the way Haskell picks out an implementation of a function that's defined on a type. F.i. the way the type of mappend is defined on the type Monoid. But the implementation is defined on an instance of Monoid. So when we use mappend the implementation is found by looking up the implementation of the runtime type of the Monoid.

glueTwice:: Monoid m => m -> m
glueTwice m = m `mappend` m

glueTwice "bla"  --  uses mappend implementation of List

This seems very much like the key point of OO: the Liskov substitution principle and like having an interface Monoid that's implemented by String.

So what's different between OO and these functions defined on types and implemented on instances?

@joost-de-vries
joost-de-vries / gist:afee20ce77ce330769a0
Last active August 29, 2015 14:12
git: cache password on windows

git config --global credential.helper wincred

@joost-de-vries
joost-de-vries / gist:201ad06918077f8a01ae
Created January 5, 2015 13:00
Windows script for switching java version
@echo off
set MY_JAVA_HOME=D:\devtools\jdk1.8.0_25
set SYMLINK_PATH=C:\ProgramData\Oracle\Java\javapath
set JAVA_VERSION=1.8
rem set environment variable for user
setx JAVA_HOME %MY_JAVA_HOME%
rem change default java version in registry
REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment" /v "CurrentVersion" /t REG_SZ /d %JAVA_VERSION% /f