Skip to content

Instantly share code, notes, and snippets.

View mzimecki's full-sized avatar

Marcin Zimecki mzimecki

  • Gdańsk, Poland
View GitHub Profile
@mzimecki
mzimecki / FunctionsCall.scala
Last active December 19, 2015 19:48
[Scala] Many ways of passing x < 0 function into exists function (verbose version as first one). Each of them has the same meaning.
def containsNeg1(nums: List[Int]) = nums.exists((x: Int) => x < 0)
def containsNeg2(nums: List[Int]) = nums.exists((x) => x < 0)
def containsNeg3(nums: List[Int]) = nums.exists(x => x < 0)
def containsNeg4(nums: List[Int]) = nums.exists(_ < 0)
@mzimecki
mzimecki / EnumSetTest.java
Last active December 19, 2015 22:39
[Java] EnumSet order
import java.util.EnumSet;
/**
* EnumSet uses natural order which means the order in which the enum constants are declared.
*/
public class EnumSetTest {
private enum MyEnum {A, B, C, D}
public static void main(String [] args) {
@mzimecki
mzimecki / AndOr.scala
Last active December 23, 2015 06:09
[Scala] Functions and(x,y) and or(x,y)
def and(x: Boolean, y: =>Boolean) = if (x) y else false
def or(x: Boolean, y: =>Boolean) = if (x) true else y
@mzimecki
mzimecki / HashCodeEqualsExample.scala
Last active December 25, 2015 07:09
[Scala] hashCode and equals of mutable object
import scala.collection.immutable.HashMap
/**
* hashCode and equals of mutable object (not working well because of object mutability)
* the only way to make it working is to use immutable object
*/
class Point2(var x: Int, var y: Int) {
def move(mx: Int, my: Int): Unit = {
x = x + mx
y = y + my
}
@mzimecki
mzimecki / InsertionSort.scala
Created October 12, 2013 19:09
[Scala] Insertion sort
object InsertionSort {
def sort(list: List[Int]): List[Int] = {
def insert(x: Int, xs: List[Int]): List[Int] = xs match {
case List() => List(x)
case y :: ys => if (x <= y) x :: xs else y :: insert(x, ys)
}
list match {
case List() => List()
@mzimecki
mzimecki / MergeSort.scala
Created October 16, 2013 09:20
[Scala] Merge sort with implicit parameter used for comparing elements in a list. Making prameter implicit will cause compiler will discover type of elements in a list to be sorted.
object MergeSort {
def msort[T](xs: List[T])(implicit ord: Ordering[T]): List[T] = {
def merge(xs: List[T], ys: List[T]): List[T] = (xs, ys) match {
case (Nil, ys) => ys
case (xs, Nil) => xs
case (x :: xs1, y :: ys1) =>
if (ord.lt(x, y)) x :: merge(xs1, ys) else y :: merge(xs, ys1)
}
@mzimecki
mzimecki / Max.scala
Created October 26, 2013 19:43
[Scala] generic max function
def max[T](xs: T*)(implicit ord: Ordering[T]): T = xs.reduceLeft((a, b) => if (ord.gteq(a, b)) a else b)
@mzimecki
mzimecki / FileWrapper.scala
Created November 1, 2013 10:22
[Scala] Views example from Scala in Depth book
import FileWrapper.wrap
/**
* Views example: converting one type to other.
*/
class FileWrapper(val file: java.io.File) {
def /(next: String) = new FileWrapper(new java.io.File(file, next))
override def toString = file.getCanonicalPath()
}
@mzimecki
mzimecki / ExecutorsExample.java
Created November 1, 2013 15:54
[Java] Callable, Future, Executors example
package com.zimek.executors;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

The introduction to Reactive Programming you've been missing

(by @andrestaltz)

So you're curious in learning this new thing called (Functional) Reactive Programming (FRP).

Learning it is hard, even harder by the lack of good material. When I started, I tried looking for tutorials. I found only a handful of practical guides, but they just scratched the surface and never tackled the challenge of building the whole architecture around it. Library documentations often don't help when you're trying to understand some function. I mean, honestly, look at this:

Rx.Observable.prototype.flatMapLatest(selector, [thisArg])

Projects each element of an observable sequence into a new sequence of observable sequences by incorporating the element's index and then transforms an observable sequence of observable sequences into an observable sequence producing values only from the most recent observable sequence.