This file contains 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
(defn primes [n] nil | |
(reverse | |
(loop [candidate 2 current n acc []] | |
(if (zero? current) | |
acc | |
(if (every? #(not= 0 (rem candidate %)) acc) | |
(recur (inc candidate) (dec current) (cons candidate acc)) | |
(recur (inc candidate) current acc)))))) | |
(fact |
This file contains 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 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) => |
This file contains 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
public Boolean hasStraightFlush_plainJava() { | |
Suit expectedSuit = cards.get(0).getSuit(); | |
int minValue = cards.get(0).getValue().ordinal(); | |
for (int i = 1; i < 5; i++) { | |
Card card = cards.get(i); | |
if (card.getValue().ordinal() != minValue + i) { | |
return false; | |
} | |
if (card.getSuit() != expectedSuit) { | |
return false; |
This file contains 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
// Missing support libraries | |
object MissingLibraries { | |
case class State[S, +A](run: S => (A, S)) { | |
def map[B](f: A => B): State[S, B] = | |
State(s => { | |
val (a, t) = run(s) | |
(f(a), t) | |
}) | |
def flatMap[B](f: A => State[S, B]): State[S, B] = |
This file contains 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 my.util; | |
import java.util.Collections; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.NoSuchElementException; | |
import java.util.function.Function; | |
public abstract class Option<T> implements Iterable<T> { |
This file contains 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
StringBuilder whereConstraints = new StringBuilder(); | |
if (!isEmpty(typesActu)) { | |
whereConstraints.append(" AND a.type_id IN (?) "); | |
} | |
if (!isEmpty(newsletters)) { | |
whereConstraints.append(" AND (ap.profils_id IN (?) OR ap.profils_id IS NULL)"); | |
} | |
if (!isNullOrEmpty(term)) { | |
whereConstraints.append(" AND (titre LIKE (?) OR texte_complet LIKE(?) ) "); | |
} |
This file contains 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
import org.specs2.mutable._ | |
class ImplicitiSpec extends Specification { | |
type Store=Map[String,String] | |
object Value { | |
def apply(k:String)(implicit map:Store):String={ | |
println(s"reads value for ${k}") | |
map.get(k).getOrElse("") | |
} |
This file contains 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 Point { | |
trait PointAdder[P1, P2] { | |
def add(p1: P1, p2: P2): Point3D | |
} | |
case class Point2D(x: Int, y: Int) | |
case class Point3D(x: Int, y: Int, z: Int) | |
implicit object Point2DAdder extends PointAdder[Point2D, Point2D] { |
This file contains 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 Point extends App { | |
trait Semigroup[T] { | |
def append(p1: T, p2: T): T | |
} | |
case class Point2D(x: Int, y: Int) | |
case class Point3D(x: Int, y: Int, z: Int) | |
object Point2D { |
This file contains 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 Point { | |
trait PointAdder[P2] { | |
def add(p2: P2): Point3D | |
} | |
case class Point2D(x: Int, y: Int) | |
case class Point3D(x: Int, y: Int, z: Int) | |
implicit class Point2Adder(p1:Point2D) extends PointAdder[Point2D] { |
OlderNewer