Created
July 2, 2018 02:39
-
-
Save araghuraman1/ae723b07285991df6af129650822a465 to your computer and use it in GitHub Desktop.
src/main/kotlin/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlin.properties.Delegates | |
import io.reactivex.rxkotlin.* | |
import io.reactivex.Observer | |
import io.reactivex.disposables.Disposable | |
import io.reactivex.Observable | |
import java.util.concurrent.Callable | |
import java.util.concurrent.Future | |
import java.util.concurrent.TimeUnit | |
import kotlin.io.println | |
import arrow.syntax.function.andThen | |
import arrow.syntax.function.compose | |
import arrow.syntax.function.forwardCompose | |
import arrow.syntax.function.partially2 | |
import arrow.syntax.function.partially3 | |
import arrow.syntax.function.curried | |
import arrow.syntax.function.pipe | |
import arrow.syntax.function.reverse | |
import arrow.syntax.function.uncurried | |
import java.util.* | |
import java.math.BigDecimal | |
import arrow.optics.Lens | |
import arrow.optics.optics | |
typealias Exec<R> = ()->R | |
typealias Exec2<T,R> = (T)->R | |
data class Result<R>(val result:R) | |
infix fun <R,R1> Result<R>.success(exec:Exec2<R,R1>):Result<R1> = Result(exec(this.result)) | |
infix fun <R,R1> Result<R>.then(exec:Exec2<R,R1>):R1 = exec(this.result) | |
fun <R> task(exec:Exec<R>) = Result(exec()) | |
fun String.println() = println(this) | |
fun <A,B> Pair<A,B>.reverse():Pair<B,A> = Pair(this.second,this.first) | |
/////////Arrow framework////////////////// | |
val p:(String)->String = {body -> "<p>${body}</p>"} | |
val span:(String)->String = {span -> "<span>${span}</span>"} | |
val div:(String)->String = {div -> "<div>${div}</div>"} | |
val strong:(String)->(String) = {strong -> "<strong>${strong}</strong>"} | |
val randomNames:()->String = { | |
if(Random().nextInt()%2==0) "foo" else "bar" | |
} | |
data class Bill(val amount:BigDecimal) | |
data class PickingOrder(val name:String) | |
fun Bill.println()=println(this.amount) | |
fun PickingOrder.println()=println(this.name) | |
fun partialSplitter(billAndOrder: Pair<Bill,PickingOrder>?,warehouse:(PickingOrder)->Unit,accounting: (Bill)->Unit){ | |
if(billAndOrder!=null) { | |
warehouse(billAndOrder.second) | |
accounting(billAndOrder.first) | |
} | |
} | |
typealias GB = Int | |
//@optics data class Memory(val size: GB) | |
//@optics data class Motherboard(val brand: String, val memory: Memory) | |
//@optics data class Laptop(val price: Double, val motherBoard: Motherboard) | |
fun main(args:Array<String>) { | |
task { | |
"Ananth" | |
} success { | |
it.toString() + " Raghuraman" | |
} then{ | |
it + ", Mr" | |
} | |
val myList:MutableList<Int> = mutableListOf(1,3,5,7,9) | |
val observable = myList.toObservable() | |
observable.subscribeBy(onNext={println(it)},onError={it.printStackTrace()},onComplete={println("Done")}) | |
myList.add(11) | |
val observer:Observer<String> = object : Observer<String> { | |
override fun onComplete(){ | |
println("Completed") | |
} | |
override fun onNext(item:String) { | |
println("Received: $item") | |
} | |
override fun onError(e: Throwable) { | |
println("Error Occured: ${e.message}") | |
} | |
override fun onSubscribe(d:Disposable) { | |
println("Subscription") | |
} | |
} | |
val list = listOf("Str 1","Str 2","Str 3","Str 4") | |
val observableFromIterable:Observable<String> = Observable.fromIterable(list) | |
observableFromIterable.subscribe(observer) | |
val callable = {"Iam from Callable"} | |
val observableFromCallable:Observable<String> = Observable.fromCallable(callable) | |
observableFromCallable.subscribe(observer) | |
var future:Future<String> = object:Future<String>{ | |
val retStr = "I'am from the Future" | |
override fun get() = retStr | |
override fun get(timeout:Long, unit: TimeUnit?) = retStr | |
override fun isDone():Boolean = true | |
override fun isCancelled():Boolean = false | |
override fun cancel(mayInterruptIfRunning:Boolean):Boolean = false | |
} | |
var observableFromFuture:Observable<String> = Observable.fromFuture(future) | |
observableFromFuture.subscribe(observer) | |
//////////////////////////////////////////////////////// | |
listOf(1,2,3).map{i->i*2}.map(Int::toString).forEach(::println) | |
println(Option.Some("Kotlin").map(String::toUpperCase)) | |
println(Option.None.map(String::toUpperCase)) | |
//////Arrow framework/////////////////// | |
val divStrong:(String)->(String) = div compose strong | |
val spanP:(String)->String = p forwardCompose span | |
val randomStrong:()->String = randomNames andThen strong | |
randomStrong().println() | |
spanP("Ananth").println() | |
divStrong("Raghuraman").println() | |
val strog:(String,String,String)->String = {body,id,style->"<strong id=\"$id\" style=\"$style\">$body</strong>"} | |
val redStrong:(String,String) -> String = strog.partially3("font: red") | |
redStrong("A","B").println() | |
///////////////////////////////////////// | |
val splitter: (Pair<Bill,PickingOrder>?)->Unit = ::partialSplitter.partially2 { pickingOrder ->pickingOrder.println() }.partially2 { bill -> bill.println() } | |
splitter(Pair(Bill(BigDecimal.TEN),PickingOrder("Ananth's picking order"))) | |
///////////////////////////////////////// | |
////Currying////////////////// | |
val strong1:(String,String,String)->String = {a,b,c->"$a;$b;$c"} | |
val p:(Int)->(Int) ->String = {it->{a-> | |
println("it is $it from the outer function. a is $a") | |
"${it*2} is the inner function" | |
} | |
} | |
p(3)(4).println() | |
val curriedStrong:(String)->(String)->(String)->String = strong1.curried() | |
val aStrong:(String)->(String)->String = curriedStrong("a") | |
val abStrong:(String)->String = aStrong("b") | |
val abcStrong:String = abStrong("c") | |
abcStrong pipe ::println | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildscript{ | |
ext.kotlin_version = '1.2.21' | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version' | |
} | |
} | |
plugins { | |
id 'org.jetbrains.kotlin.jvm' version '1.2.21' | |
id 'org.jetbrains.kotlin.kapt' version '1.2.21' | |
} | |
apply plugin: 'idea' | |
apply plugin: 'eclipse' | |
apply plugin: 'java' | |
apply plugin: 'kotlin' | |
apply plugin: 'kotlin-kapt' | |
apply plugin: 'application' | |
mainClassName="AppKt" | |
defaultTasks 'run' | |
//apply from: rootProject.file('gradle/generated-kotlin-sources.gradle') | |
repositories{ | |
mavenCentral() | |
jcenter() | |
} | |
allprojects { | |
repositories { | |
jcenter() | |
mavenCentral() | |
} | |
} | |
dependencies{ | |
compile 'org.jetbrains.kotlin:kotlin-stdlib-jdk8' | |
compile 'org.jetbrains.kotlin:kotlin-reflect' | |
compile 'io.reactivex.rxjava2:rxkotlin:2.2.0' | |
compile 'io.arrow-kt:arrow-core:0.7.2' | |
compile 'io.arrow-kt:arrow-syntax:0.7.2' | |
compile 'io.arrow-kt:arrow-typeclasses:0.7.2' | |
compile 'io.arrow-kt:arrow-data:0.7.2' | |
compile 'io.arrow-kt:arrow-instances-core:0.7.2' | |
compile 'io.arrow-kt:arrow-instances-data:0.7.2' | |
kapt 'io.arrow-kt:arrow-annotations-processor:0.7.2' | |
compile 'io.arrow-kt:arrow-free:0.7.2' //optional | |
compile 'io.arrow-kt:arrow-mtl:0.7.2' //optional | |
compile 'io.arrow-kt:arrow-effects:0.7.2' //optional | |
compile 'io.arrow-kt:arrow-effects-rx2:0.7.2' //optional | |
//compile 'io.arrow-kt:arrow-effects-reactor:0.7.3' //optional | |
compile 'io.arrow-kt:arrow-effects-kotlinx-coroutines:0.7.2' //optional | |
compile 'io.arrow-kt:arrow-optics:0.7.2' //optional | |
compile 'io.arrow-kt:arrow-generic:0.7.2' //optional | |
//compile 'io.arrow-kt:arrow-recursion:0.7.3' //optional | |
} | |
configurations{ | |
compile.exclude group:'org.jetbrains.kotlin', module:'kotlin-stdlib-jre7' | |
} | |
kotlin{ | |
experimental { | |
coroutines 'enable' | |
} | |
} | |
kapt{ | |
useBuildCache=true | |
} | |
compileKotlin { | |
kotlinOptions.jvmTarget="1.8" | |
} | |
compileTestKotlin{ | |
kotlinOptions.jvmTarget="1.8" | |
} | |
sourceSets { | |
main{ | |
kotlin { | |
srcDirs += "${build}/generated/source/kapt/main" | |
srcDirs += "${build}/generated/source/kaptKotlin/main" | |
srcDirs += "${build}/tmp/kapt/main/kotlinGenerated" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment