Skip to content

Instantly share code, notes, and snippets.

@fsarradin
fsarradin / SortTest.java
Created April 11, 2014 07:27
Sort algo
import org.junit.Ignore;
import org.junit.Test;
import java.util.*;
public class SortTest {
@Test
public void testCollectionsSort() {
List<Integer> values = getValues();
@fsarradin
fsarradin / functor.py
Last active August 29, 2015 14:00
Attempt of typeclass in Python
from maybe import Maybe
from validation import Validation
def Typeclass():
class _Typeclass(object):
registered_class = {}
@classmethod
def instance_for(cls, object_type):
@fsarradin
fsarradin / gist:0f4b16938e0c010ce40c
Created August 5, 2014 04:31
IntelliJ IDEA live template for enum with by-name behavior
private static final Map<String, $CLASS_NAME$> $VAR_CLASS_NAME$sByName = create$CLASS_NAME$sByName();
private final String name;
$CLASS_NAME$() {
this(null);
}
$CLASS_NAME$(String name) {
this.name = name;
@fsarradin
fsarradin / FunctorModule.scala
Created December 9, 2014 15:18
Reader functor in Scala (the typeclass way)
object FunctorModule {
trait Functor[F[_]] {
def map[A, B](f: A => B): F[A] => F[B]
}
implicit class ReaderFunctor[E](r: E => _) extends Functor[({ type l[a] = E => a })#l] {
override def map[A, B](f: A => B): (E => A) => (E => B) = { r => r andThen f }
}
@fsarradin
fsarradin / GenericsTest.java
Last active August 29, 2015 14:21
Java generics confusion
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
public class GenericsTest {
@Test
public void should_convert___well_huh() throws Exception {
package me;
import java.util.Collection;
import java.util.function.Function;
public interface ClassToRefactor_Copy {
default void sendMessages(Collection<ParseResult<InputMessage>> parsedMessages) {
parsedMessages.stream()
.map(parsedMessage ->
import java.util.function.Function;
public interface Continuation<R, A> extends Function<Function<A, R>, R> {
@Override
R apply(Function<A, R> f);
default <B> Continuation<R, B> map(Function<A, B> f) {
return continuation(g -> apply(g.compose(f)));
}
@fsarradin
fsarradin / build.sbt
Created August 13, 2015 13:29
SBT vs MAVEN
libraryDependencies ++= Seq(
"org.springframework.data" % "spring-data-mongodb" % "1.7.2.RELEASE",
"org.springframework.boot" % "spring-boot-autoconfigure" % "1.2.5.RELEASE"
)
import com.google.common.base.Optional;
public abstract class Validation<E, T> {
// avoid subclasses outside of this class
private Validation() {}
public abstract boolean isSuccess();
public abstract Optional<T> toOptional();
public abstract Validation<T, E> swap();
@fsarradin
fsarradin / Isin.scala
Created December 11, 2011 23:06
Check if a given code is a consistent with the ISIN standard
package isin
object Isin {
// based on http://en.wikipedia.org/wiki/International_Securities_Identification_Number
def isIsin(code: String): Boolean = {
val digits: String = code.init.map(c => BigInt(c.toString, 36)).mkString
val parity = digits.size & 1
val total: Int = digits.zipWithIndex.map {
case (c: Char, i: Int) =>