Skip to content

Instantly share code, notes, and snippets.

@hugolu
hugolu / sortByValue.scale
Created January 7, 2016 10:44
[Spark] implement sortByValue() with sortBy()
val fruits = List("Apple", "Banana", "Fig", "Wiki", "Pineapple", "Strawberry", "Watermelon")
val keyValues = sc.parallelize(fruits.map(fruit => (fruit, fruit.length)))
keyValues.sortByKey().foreach(print)
//output> (Apple,5)(Banana,6)(Fig,3)(Pineapple,9)(Strawberry,10)(Watermelon,10)(Wiki,4)
keyValues.sortBy(_._1).foreach(print) //as sortByKey()
//output> (Apple,5)(Banana,6)(Fig,3)(Pineapple,9)(Strawberry,10)(Watermelon,10)(Wiki,4)
keyValues.sortBy(_._2).foreach(print) //as sortByValue()
val list = List(1, 2, 3) //> list : List[Int] = List(1, 2, 3)
list.map(_.toString).mkString(", ") //> res0: String = 1, 2, 3
val map = Map(1 -> "A", 2 -> "B", 3 -> "C") //> map : scala.collection.immutable.Map[Int,String] = Map(1 -> A, 2 -> B, 3 -> C)
map.map(_ match { case (k, v) => k + ":" + v }).mkString(", ")
//> res1: String = 1:A, 2:B, 3:C
@hugolu
hugolu / isPrime.scala
Created February 24, 2016 15:25
to determine whether a number is a prime
def isPrime(i: Int) =
if (i <= 1)
false
else if (i == 2)
true
else
!(2 to i/2).exists(x => i % x == 0) //> isPrime: (i: Int)Boolean
isPrime(2) //> res0: Boolean = true
isPrime(3) //> res1: Boolean = true
#!/bin/sh
name=$1
version=$2
scalaVersion=2.11.7
mkdir -p src/{main,test}/{java,resources,scala}
mkdir lib project target
# create an initial build.sbt file
add_dict = { (0.0,0.0):0.0, (0.0,1.0):1.0, (0.0,2.0):2.0, (0.0,3.0):3.0, (0.0,4.0):4.0, (0.0,5.0):5.0, (0.0,6.0):6.0, (0.0,7.0):7.0, (0.0,8.0):8.0, (0.0,9.0):9.0, (1.0,0.0):1.0, (1.0,1.0):2.0, (1.0,2.0):3.0, (1.0,3.0):4.0, (1.0,4.0):5.0, (1.0,5.0):6.0, (1.0,6.0):7.0, (1.0,7.0):8.0, (1.0,8.0):9.0, (1.0,9.0):10.0, (2.0,0.0):2.0, (2.0,1.0):3.0, (2.0,2.0):4.0, (2.0,3.0):5.0, (2.0,4.0):6.0, (2.0,5.0):7.0, (2.0,6.0):8.0, (2.0,7.0):9.0, (2.0,8.0):10.0, (2.0,9.0):11.0, (3.0,0.0):3.0, (3.0,1.0):4.0, (3.0,2.0):5.0, (3.0,3.0):6.0, (3.0,4.0):7.0, (3.0,5.0):8.0, (3.0,6.0):9.0, (3.0,7.0):10.0, (3.0,8.0):11.0, (3.0,9.0):12.0, (4.0,0.0):4.0, (4.0,1.0):5.0, (4.0,2.0):6.0, (4.0,3.0):7.0, (4.0,4.0):8.0, (4.0,5.0):9.0, (4.0,6.0):10.0, (4.0,7.0):11.0, (4.0,8.0):12.0, (4.0,9.0):13.0, (5.0,0.0):5.0, (5.0,1.0):6.0, (5.0,2.0):7.0, (5.0,3.0):8.0, (5.0,4.0):9.0, (5.0,5.0):10.0, (5.0,6.0):11.0, (5.0,7.0):12.0, (5.0,8.0):13.0, (5.0,9.0):14.0, (6.0,0.0):6.0, (6.0,1.0):7.0, (6.0,2.0):8.0, (6.0,3.0):9.0, (6.0,4.0):10.0, (6.0,5.0):11.0, (6.0
@hugolu
hugolu / gist:ab322ff9b74843a565c6b67730d45a99
Created December 14, 2016 12:30
accumulator practice
Python 3.4.2 (default, Oct 8 2014, 10:45:20)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
16/12/14 12:27:20 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Welcome to
____ __
/ __/__ ___ _____/ /__
_\ \/ _ \/ _ `/ __/ '_/
/__ / .__/\_,_/_/ /_/\_\ version 2.0.1
/_/
@hugolu
hugolu / hexdump.c
Created January 1, 2017 04:28
hexdump in c
#include <stdio.h>
#define MIN(A,B) ((A) < (B) ? (A) : (B))
void hexdump(void *buf, int size) {
unsigned char *ptr;
int i, j, len = 0;
for (i = 0; i < size; i += len) {
ptr = buf + i;
len = MIN(16, size - i);
printf("%04x | ", i);
# init repository
rm -rf learn-git
mkdir learn-git
cd learn-git
git init
# commit changes
echo 1 >> file
git add .
git commit -m "1"
# init repository
rm -rf learn-git
mkdir learn-git
cd learn-git
git init
# commit changes @master
echo 1 >> file
git add .
git commit -m "1"
# init repository
mkdir learn-git
cd learn-git
git init
# commit changes
echo 1 >> file
git add .
git commit -m "1"