Skip to content

Instantly share code, notes, and snippets.

View javipacheco's full-sized avatar

Javier Pérez Pacheco javipacheco

View GitHub Profile
@javipacheco
javipacheco / RESTrungGetExample.java
Created November 8, 2012 13:19
RESTrung GET example
RestClientFactory.getClient().getAsync(new ContextAwareAPIDelegate<Target>(context, Target.class) {
@Override
public void onResults(Target response) {
//handle results here in the main thread
}
@Override
public void onError(Throwable e) {
//handle errors here in the main thread
@javipacheco
javipacheco / gist:5462320
Created April 25, 2013 19:13
Week 3: Data and abstraction
package objsets
import common._
import TweetReader._
/**
* A class to represent tweets.
*/
class Tweet(val user: String, val text: String, val retweets: Int) {
override def toString: String =
@javipacheco
javipacheco / gist:1704760e53859bfda63d
Created August 6, 2014 09:43
Find Installed Apps
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
final int countApps = apps.size();
for (int i = 0; i < countApps; i++) {
try {
ResolveInfo info = apps.get(i);
@javipacheco
javipacheco / Java.java
Last active August 29, 2015 14:25
Paranoia
List<String> list = Arrays.asList("a", "b", "c", "d");
match(list)
.when(nil()).then(() -> System.out.println("Empty List"))
.when(headNil(eq("b"))).then(() -> System.out.println("Singleton List of 'b'"))
.when(headNil(any())).then(head -> System.out.println("Singleton List of " + head))
.when(headTail(any(), any())).then(
(head, tail) -> System.out.println("head: " + head + " Remaining: " + tail))
.doMatch();
@javipacheco
javipacheco / serial_cancelable.scala
Last active November 23, 2016 16:18
Issue with SerialCancelable in Monix 2.1.1
import monix.eval.Task
import monix.execution.Scheduler.Implicits.global
import monix.execution.cancelables.SerialCancelable
val ref = SerialCancelable()
val t1 = Task {
Thread.sleep(10000)
println("Task 1")
}
@javipacheco
javipacheco / CallToService.kt
Created November 9, 2017 11:18
retrofit Call to Service
package akme
import retrofit2.Call
fun <T> Call<T>.toService(): Service<T> {
val response= this.execute()
return if (response.isSuccessful) {
ServiceRight { response.body() }
} else {
@javipacheco
javipacheco / NewsUiService.kt
Created November 9, 2017 13:36
NewsUiService
package rekkit.ui.news
import akme.Service
import rekkit.models.States
import kategory.ListKW
interface NewsUiService {
fun showLoading(): Service<Unit>
@javipacheco
javipacheco / Service.kt
Created November 9, 2017 13:39
Service
typealias Service<A> = Either<AkmeException, A>
sealed class AkmeException {
data class UiException(val msg: String) : AkmeException()
data class ApiException(val msg: String) : AkmeException()
data class CallException(val msg: String) : AkmeException()
@javipacheco
javipacheco / EitherMonad.kt
Last active November 9, 2017 14:09
EitherMonad
Either.monad<AkmeException>().binding {
uiService.showLoading().bind()
val news = apiService.getNews().bind()
uiService.showNews(news).bind()
yields(Unit)
}
@javipacheco
javipacheco / NewsActor.kt
Created November 9, 2017 16:09
NewsActor
class NewsActors(val apiService: ApiService, val uiService: NewsUiService) {
val mainActor = actor<Commands> {
var newsState: States.NewsState = States.NewsState(ListKW.empty())
for (msg in channel) {
when (msg) {
is Commands.NewsGetItemsCommand ->
ServiceMonad().binding {
uiService.showLoading()