Skip to content

Instantly share code, notes, and snippets.

View Krasnyanskiy's full-sized avatar

Sasha Krasnyanskiy

  • Cupertino, California
View GitHub Profile
@Krasnyanskiy
Krasnyanskiy / Application.scala
Last active April 21, 2016 11:44
-scala: a flip function like in Haskell
def flip[X, Y, Z](f: X => Y => Z): Y => X => Z = {
x => y => f(y)(x)
}
def foo(a: Int)(b: Long): Boolean = a + b > 10
flip { foo } { 1L } { 10 }
@Krasnyanskiy
Krasnyanskiy / Sort.java
Last active April 23, 2016 23:06
-java: merge
public int[] merge(int[] left, int[] right) {
int i = 0;
int j = 0;
int k = 0;
int leftLen = left.length;
int rightLen = right.length;
int[] merged = new int[leftLen + rightLen];

Explaining Miles's Magic

Miles Sabin recently opened a pull request fixing the infamous SI-2712. First off, this is remarkable and, if merged, will make everyone's life enormously easier. This is a bug that a lot of people hit often without even realizing it, and they just assume that either they did something wrong or the compiler is broken in some weird way. It is especially common for users of scalaz or cats.

But that's not what I wanted to write about. What I want to write about is the exact semantics of Miles's fix, because it does impose some very specific assumptions about the way that type constructors work, and understanding those assumptions is the key to getting the most of it his fix.

For starters, here is the sort of thing that SI-2712 affects:

def foo[F[_], A](fa: F[A]): String = fa.toString
@Krasnyanskiy
Krasnyanskiy / solutions.js
Last active May 22, 2016 18:02
-js: solutions for chapter #3
function min(x, y) {
return x >= y ? y : x;
}
function isEven(n) {
if (n < 0) n = Math.abs(n); // alternative: n = n * -1
if (n == 0) return true;
if (n == 1) return false;
return isEven(n - 2);
}

The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming Workshop for All!

John A. De Goes — @jdegoes


Agenda

  • Functions
  • Types, Kinds, & More Functions
  • FP Toolbox
@Krasnyanskiy
Krasnyanskiy / Java8.java
Last active May 31, 2016 20:21
-java: stream api
int sum = asList("abc", "de", "fghij")
.stream()
.map(String::length)
.reduce(0, (x, y) -> x + y);
List<String> list = asList("lbc", "ed", "ighfjka")
.parallelStream()
.flatMap(s -> asList(s.split("")).stream())
.sorted()
@Krasnyanskiy
Krasnyanskiy / App.java
Last active June 5, 2016 12:14
-java: switch (default)
public int foo(int v) {
int counter = 0;
switch (v) {
default: counter += 1;
case 1: counter += 2;
case 2: counter += 3;
}
return counter;
}
@Krasnyanskiy
Krasnyanskiy / A.scala
Created June 11, 2016 19:43
-scala: aux pattern
class User
object User {
type Aux[I, A] = User {
type IdType = I
type Address = A
}
}
@Krasnyanskiy
Krasnyanskiy / TimestampMap.java
Last active June 11, 2016 22:51
-java: my questions to Java interns (Design + Correctness)
public class TimestampMap<V> extends HashMap<Timestamp, V> {
public static <T> TimestampMap<T> of(T... ts) {
return new TimestampMap<T>() {
@Override
public T put(Timestamp k, T v) {
for (T t : ts) {
this.put(now(), t); // potential SO (!) + useless initialization | will super.* fix the SO error?
}
return null;
@Krasnyanskiy
Krasnyanskiy / findPrime.js
Created June 13, 2016 18:20
-js: find prime
function findPrime(array) {
var sieve = [], i, j, max = 1000000;
for (i = 2; i <= max; ++i) {
if (!sieve[i]) {
var parsed = ('' + i).split('').map(function (e) {
return parseInt(e, 10);
});
if (JSON.stringify(parsed) === JSON.stringify(array)) {
return i;
}