Skip to content

Instantly share code, notes, and snippets.

@MarioAriasC
MarioAriasC / gist:5552074
Last active July 30, 2017 03:20
First play with Rx for Kotlin
import rx.Observable
import rx.Subscription
import rx.util.functions.Action1
/**
* Created by IntelliJ IDEA.
* @author Mario Arias
* Date: 9/05/13
* Time: 21:11
*/
@MarioAriasC
MarioAriasC / observable.kt
Last active December 17, 2015 04:39
Creating a rx.Observable from a function
import rx.Observer
import rx.Subscription
import rx.Observable
import rx.util.functions.*
fun<T> Function1<Observer<T>, Subscription>.asObservable(): Observable<T> {
return Observable.create(Func1<Observer<T>, Subscription>{
this(it!!)
})!!
@MarioAriasC
MarioAriasC / gist:6474576
Created September 7, 2013 10:45
RowMapper with KotlinPrimavera
package org.kotlinprimavera.samples.petclinic.repository.jdbc
import org.springframework.jdbc.core.RowMapper
import java.sql.ResultSet
import org.kotlinprimavera.jdbc.core.build
import org.joda.time.DateTime
/**
* Created by IntelliJ IDEA.
* @author Mario Arias
@MarioAriasC
MarioAriasC / TraderServerExample.kt
Last active August 29, 2015 14:00
Reactor TraderService Example
package reactor.quickstart
import org.slf4j.LoggerFactory
import java.util.concurrent.CountDownLatch
import kotlin.properties.Delegates
import reactor.core.Environment
import reactor.core.spec.Reactors
import reactor.event.selector.Selectors
import reactor.event.Event
import java.util.concurrent.TimeUnit
@MarioAriasC
MarioAriasC / partials.kt
Last active August 29, 2015 14:06
New funKTionale partials
[Test] fun partials() {
val sum5ints = {(a: Int, b: Int, c: Int, d: Int, e: Int) -> a + b + c + d + e }
val sum4intsTo10: (Int, Int, Int, Int) -> Int = sum5ints(p5 = 10)
val sum3intsTo15: (Int, Int, Int) -> Int = sum4intsTo10(p4 = 5)
val sum2intsTo17: (Int, Int) -> Int = sum3intsTo15(p3 = 2)
assertEquals(sum2intsTo17(1, 2), 20)
@MarioAriasC
MarioAriasC / newPartials.kt
Last active August 29, 2015 14:06
New Partials
public fun <P1, P2, R> Function2<P1, P2, R>.invoke(p1:
P1, partial2: Partial<P2> = partial()): (P2) -> R {
return {(p2: P2) -> this(p1, p2) }
}
public fun <P1, P2, R> Function2<P1, P2, R>.invoke(partial1: Partial<P1> = partial(),
p2: P2): (P1) -> R {
return {(p1: P1) -> this(p1, p2) }
}
@MarioAriasC
MarioAriasC / Partial.kt
Created September 6, 2014 19:56
Partial
/**
* Marker class to be used as the representation of a non-appliled parameter
*
* Created by IntelliJ IDEA.
* @author Mario Arias
* Date: 6/09/14
* Time: 11:07
*/
public class Partial<T> {
}
@MarioAriasC
MarioAriasC / uncurrying.kt
Created April 15, 2015 23:32
Uncurried functions
[Test] fun testUncurrying() {
val sum2ints: (Int, Int) -> Int = { x, y -> x + y }
val curried: (Int) -> (Int) -> Int = sum2ints.curried()
assertEquals(curried(2)(4), 6)
//same type as sum2ints,
val back: (Int, Int) -> Int = curried.uncurried()
assertEquals(back(2, 4), 6)
assertEquals(sum2ints(2, 4), 6)
val sum3ints: (Int, Int, Int) -> Int = { x, y, z -> x + y }
class Bar
class Foo<T>
val b: Foo<Bar> = Foo()
@MarioAriasC
MarioAriasC / spark.kt
Last active December 21, 2019 11:22
Word Count with Apache Spark and Kotlin
package org.cakesolutions.spark
import org.apache.spark.SparkConf
import org.apache.spark.api.java.JavaSparkContext
import scala.Tuple2
fun main(args: Array<String>) {
val inputFile = args[0]
val outputFile = args[1]