Skip to content

Instantly share code, notes, and snippets.

View Synesso's full-sized avatar

Jem Mawson Synesso

View GitHub Profile
@Synesso
Synesso / Async.java
Created January 18, 2015 06:56
Simplification of Android's AsyncTask using functionaljava
import android.app.Activity;
import android.os.AsyncTask;
import fj.F;
import fj.function.Effect1;
public class Async<A, B> extends AsyncTask<A, Void, B> {
private final F<A, B> f;
private Effect1<B> e = new Effect1<B>() {
void rotate() {
final MainActivity activity = getActivity();
activity.setRequestedOrientation(
activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
: ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

Keybase proof

I hereby claim:

  • I am synesso on github.
  • I am jem (https://keybase.io/jem) on keybase.
  • I have a public key whose fingerprint is 2AFD 4BB7 EF77 7F5C 5EF5 383E 3310 1ABB 05C8 620A

To claim this, I am signing this object:

repositories {
mavenCentral()
}
package org.togglz.spring.test.proxy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
18:07:50,630 INFO [org.jboss.arquillian.testenricher.cdi.container.BeanManagerProducer] (pool-5-thread-1) BeanManager not found.
18:07:50,642 ERROR [org.jboss.arquillian.protocol.jmx.JMXTestRunner] (pool-5-thread-1) Failed: org.togglz.spring.test.proxy.FeatureProxyTest.testProxyFromConfigurationBeanWithAutoDetectedProxyType: java.lang.NoClassDefFoundError: org/togglz/spring/test/proxy/SomeServiceConfiguration
at org.togglz.spring.test.proxy.FeatureProxyTest.getSpringBeanFromConfigurationBean(FeatureProxyTest.java:114) [classes:]
at org.togglz.spring.test.proxy.FeatureProxyTest.testProxyFromConfigurationBeanWithAutoDetectedProxyType(FeatureProxyTest.java:91) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_05]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_05]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_05]
at java.lang.reflect.Method.invoke(Method.java:601) [
package ari.dnrs.registry.configuration;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
@Synesso
Synesso / WeightedSelection.scala
Created March 13, 2012 00:53
Weighted Selection
import java.util.Random
case class WeightedItem[T](item: T, weight: Double)
def weightedSelection[T](items: Seq[WeightedItem[T]], numSelections: Int, r: Random): Seq[T] = {
val (alternativesBackwards, limit) = items.foldLeft((Seq.empty[(Double, T)], 0.0)){(acc, wi) =>
val (items, weightSum): (Seq[(Double, T)], Double) = acc
val newWeightSum: Double = wi.weight + weightSum
val newItems: Seq[(Double, T)] = (newWeightSum, wi.item) +: items
(newItems, newWeightSum)
@Synesso
Synesso / ranges.scala
Created February 29, 2012 23:07
Ranges
def ranges(ints: Seq[Int]): Seq[(Int, Int)] = ranges(ints.sorted.distinct, Seq())
def ranges(ints: Seq[Int], pairs: Seq[(Int, Int)]): Seq[(Int, Int)] = {
if (ints.isEmpty) pairs.reverse
else {
val (drop, take) = ints.zipWithIndex.partition{case (n,i) => (n == ints.head+i)}
ranges(take.unzip._1, (drop.unzip._1.head, drop.unzip._1.last) +: pairs)
}
}
assert(ranges(Seq()) == Seq()) // empty
@Synesso
Synesso / isHappy.scala
Created February 28, 2012 06:28
Happy Numbers
def isHappy(num: Int): Boolean = isHappy(num, Set())
def isHappy(num: Int, seen: Set[Int]): Boolean = {
def inner(sum: Int, num: Int): Int = {
val digit = num % 10
val squared = math.pow(digit, 2).toInt
if (num < 10) sum + squared
else inner(sum + squared, (num - digit) / 10)
}
val ssd = inner(0, num)