Skip to content

Instantly share code, notes, and snippets.

object DependentTypedLength extends App {
sealed trait Nat
sealed trait Z extends Nat
sealed trait S[A <: Nat] extends Nat
trait NList[A <: Nat, +B] {
@Kornel
Kornel / NewtonRaphsonSquareRoot.scala
Last active August 29, 2015 14:14
Square root calculated using the Newton - Raphson method using infinite Streams in Scala
import scala.annotation.tailrec
object NewtonRaphsonSquareRoot extends App {
@tailrec
def within(epsilon: Double, s: Stream[Double]): Double = s match {
case x0 #:: x1 #:: xs => if (math.abs(x0 - x1) < epsilon) x1 else within(epsilon, x1 #:: xs)
}
def sqrt(n: Double)(implicit epsilon: Double): Double = {
@Kornel
Kornel / nrSqrt.hs
Created January 30, 2015 19:01
Square root calculated using the Newton - Raphson method in Haskell
module NewtonRaphson.Sqrt (nrSqrt) where
nrSqrt :: Double -> Double
nrSqrt n = within 0.001 (approximations init n)
where init = n / 2
approximations :: Double -> Double -> [Double]
approximations x0 n = iterate (next n) x0
next :: Double -> Double -> Double
@Kornel
Kornel / Types.scala
Created February 9, 2015 21:56
Infinite (?) Least Upper Bound in scala
trait A { type T <: A }
trait B { type T <: B }
trait C extends A with B { type T <: C }
trait D extends A with B { type T <: D }
val o: A with B {
type T <: A with B {
type T <: A with B {
type T <: A with B {
type T <: A with B {
package org.kornel.collatz
object CollatzScala extends App {
import StreamTakeWhileInclusive.StreamTWI
val start = 15
val collatz = Stream.iterate(15) {
case n if n % 2 == 0 => n / 2
module Collatz where
collatz :: (Integral a) => a -> [a]
collatz a = takeWhileIncl (/= 1) $ collatzSeq a
nextCollatz :: (Integral a) => a -> a
nextCollatz n = if (even n) then n `div` 2 else (3 * n + 1)
collatzSeq :: (Integral a) => a -> [a]
collatzSeq = iterate nextCollatz
@Kornel
Kornel / build.sbt
Created June 3, 2015 07:36
minimal flink sbt?
name := "flink-start"
version := "1.0"
scalaVersion := "2.11.6"
libraryDependencies ++=
Seq("org.apache.flink" % "flink-clients" % "0.8.1",
"org.apache.flink" % "flink-scala" % "0.8.1")
#!/bin/bash
MAHOUT_HOME=$PWD/mahout-mahout-0.11.0
#export MAHOUT_LOCAL=$PWD/mahout-mahout-0.11.0
SPARK_HOME=/usr/lib/spark1.5/
INPUT=/projects/reco_prod/data/sessions-cf/second/
OUTPUT=/projects/reco_prod/data/sessions-cf/similarity
EXECUTOR_MEM=14g
#!/bin/sh
SSHELL=~/spark-1.5.0-bin-2.5.0-cdh5.3.1/bin/spark-shell
#SSHELL=~/spark-1.4.0-bin-2.5.0-cdh5.3.1/bin/spark-shell
#SSHELL=spark-shell
SSHELL=spark-shell1.5
JAR=./lib/reco-analyzer-assembly-1.12.1-SNAPSHOT.jar
#SPARK_HOME=~/spark-1.5.0-bin-2.5.0-cdh5.3.1 $SSHELL --master yarn --driver-memory 3g --jars $JAR --queue reco_prod --num-executors 20 --driver-java-options "-Dspark.executor.memory=4g"
package com.allegrogroup.reco.analyzer.spark.session
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
class SessionsSimilarity {
case class SessionEvent(sessionId: String, itemId: String)
def run(sc: SparkContext) {