El Noguer

Joaquim Perez i Noguer

Catalan numbers en Scala

Filed under: General — Joaquim Perez Noguer at 6:14 pm on Dissabte, Febrer 9, 2019

Demostració de com implementar la seqüència de números Catalans amb Scala. Vist a Rosetta code.


object Catalan {
  def factorial(n: BigInt) = BigInt(1).to(n).foldLeft(BigInt(1))(_ * _)
  def catalan(n: BigInt) = factorial(2 * n) / (factorial(n + 1) * factorial(n))
 
  def main(args: Array[String]) {
    for (n <- 0 to 15) {
      println("catalan(" + n + ") = " + catalan(n))
    }
  }
}

Catalan numbers en Python

Filed under: Ciència i tecnologia — Joaquim Perez Noguer at 6:12 pm on Dissabte, Febrer 9, 2019

Demostració de com implementar la seqüència de números Catalans amb Python. Vist a Rosetta code.

from math import factorial
import functools
 
def memoize(func):
    cache = {}
    def memoized(key):
        # Returned, new, memoized version of decorated function
        if key not in cache:
            cache[key] = func(key)
        return cache[key]
    return functools.update_wrapper(memoized, func)
 
 
@memoize
def fact(n):
    return factorial(n)
 
def cat_direct(n):
    return fact(2*n) // fact(n + 1) // fact(n)
 
@memoize    
def catR1(n):
    return ( 1 if n == 0
             else sum( catR1(i) * catR1(n - 1 - i)
                       for i in range(n) ) )
 
@memoize    
def catR2(n):
    return ( 1 if n == 0
             else ( ( 4 * n - 2 ) * catR2( n - 1) ) // ( n + 1 ) )
 
 
if __name__ == '__main__':
    def pr(results):
        fmt = '%-10s %-10s %-10s'
        print ((fmt % tuple(c.__name__ for c in defs)).upper())
        print (fmt % (('='*10,)*3))
        for r in zip(*results):
            print (fmt % r)
 
 
    defs = (cat_direct, catR1, catR2)
    results = [ tuple(c(i) for i in range(15)) for c in defs ]
    pr(results)

Catalan numbers en JS

Filed under: Ciència i tecnologia — Joaquim Perez Noguer at 6:11 pm on Dissabte, Febrer 9, 2019

Demostració de com implementar la seqüència de números Catalans amb Javascript. Vist a Rosetta code.

function disp(x) {
	var e = document.createTextNode(x + '\n');
	document.getElementById('x').appendChild(e);
}
 
var fc = [], c2 = [], c3 = [];
function fact(n) { return fc[n] ? fc[n] : fc[n] = (n ? n * fact(n - 1) : 1); }
function cata1(n) { return Math.floor(fact(2 * n) / fact(n + 1) / fact(n) + .5); }
function cata2(n) {
	if (n == 0) return 1;
	if (!c2[n]) {
		var s = 0;
		for (var i = 0; i < n; i++) s += cata2(i) * cata2(n - i - 1);
		c2[n] = s;
	}
	return c2[n];
}
function cata3(n) {
	if (n == 0) return 1;
	return c3[n] ? c3[n] : c3[n] = (4 * n - 2) * cata3(n - 1) / (n + 1);
}
 
disp("       meth1   meth2   meth3");
for (var i = 0; i <= 15; i++)
	disp(i + '\t' + cata1(i) + '\t' + cata2(i) + '\t' + cata3(i));

Catalan numbers en Java

Filed under: Ciència i tecnologia — Joaquim Perez Noguer at 6:10 pm on Dissabte, Febrer 9, 2019

Demostració de com implementar la seqüència de números Catalans amb Java. Vist a Rosetta code.


import java.util.HashMap;
import java.util.Map;
 
public class Catalan {
	private static final Map<Long, Double> facts = new HashMap<Long, Double>();
	private static final Map<Long, Double> catsI = new HashMap<Long, Double>();
	private static final Map<Long, Double> catsR1 = new HashMap<Long, Double>();
	private static final Map<Long, Double> catsR2 = new HashMap<Long, Double>();
 
	static{//pre-load the memoization maps with some answers 
		facts.put(0L, 1D);
		facts.put(1L, 1D);
		facts.put(2L, 2D);
 
		catsI.put(0L, 1D);
		catsR1.put(0L, 1D);
		catsR2.put(0L, 1D);
	}
 
	private static double fact(long n){
		if(facts.containsKey(n)){
			return facts.get(n);
		}
		double fact = 1;
		for(long i = 2; i <= n; i++){
			fact *= i; //could be further optimized, but it would probably be ugly
		}
		facts.put(n, fact);
		return fact;
	}
 
	private static double catI(long n){
		if(!catsI.containsKey(n)){
			catsI.put(n, fact(2 * n)/(fact(n+1)*fact(n)));
		}
		return catsI.get(n);
	}
 
	private static double catR1(long n){
		if(catsR1.containsKey(n)){
			return catsR1.get(n);
		}
		double sum = 0;
		for(int i = 0; i < n; i++){
			sum += catR1(i) * catR1(n - 1 - i);
		}
		catsR1.put(n, sum);
		return sum;
	}
 
	private static double catR2(long n){
		if(!catsR2.containsKey(n)){
			catsR2.put(n, ((2.0*(2*(n-1) + 1))/(n + 1)) * catR2(n-1));
		}
		return catsR2.get(n);
	}
 
	public static void main(String[] args){
		for(int i = 0; i <= 15; i++){
			System.out.println(catI(i));
			System.out.println(catR1(i));
			System.out.println(catR2(i));
		}
	}
}

JUnit5 Jupiter

Filed under: Ciència i tecnologia — Joaquim Perez Noguer at 12:59 pm on Divendres, Febrer 8, 2019
AnnotationDescription
@TestDenotes that a method is a test method. Unlike JUnit 4’s @Test annotation
@ParameterizedTestDenotes that a method is a parameterized test.
@RepeatedTestDenotes that a method is a test template for a repeated test.
@TestFactoryDenotes that a method is a test factory for dynamic tests.
@TestTemplateDenotes that a method is a template for test cases designed to be invoked multiple times depending on the number of invocation contexts returned by the registered providers. Such methods are inherited unless they are overridden.
@TestMethodOrderUsed to configure the test method execution order for the annotated test class; similar to JUnit 4’s @FixMethodOrder. Such annotations are inherited.
@TestInstanceUsed to configure the test instance lifecycle for the annotated test class. Such annotations are inherited.
@DisplayNameDeclares a custom display name for the test class or test method.
@DisplayNameGenerationDeclares a custom display name generator for the test class.
@BeforeEachAnalogous to JUnit 4’s @Before.
@AfterEachAnalogous to JUnit 4’s @After.
@BeforeAllAnalogous to JUnit 4’s @BeforeClass.
@AfterAllAnalogous to JUnit 4’s @AfterClass.
@NestedDenotes that the annotated class is a non-static nested test class. @BeforeAll and @AfterAll methods cannot be used directly in a @Nested test class unless the “per-class” test instance lifecycle is used. Such annotations are not inherited.
@TagUsed to declare tags for filtering tests, either at the class or method level; analogous to test groups in TestNG or Categories in JUnit 4. Such annotations are inherited at the class level but not at the method level.
@DisabledAnalogous to JUnit 4’s @Ignore.
@ExtendWithUsed to register extensions declaratively. Such annotations are inherited.
@RegisterExtensionUsed to register extensions programmatically via fields. Such fields are inherited unless they are shadowed.
@TempDirUsed to supply a temporary directory via field injection or parameter injection in a lifecycle method or test method; located in the org.junit.jupiter.api.io package.
Exemples
---------------
import java.lang.reflect.Method;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class DisplayNameGeneratorDemo {

    @Nested
    @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
    class A_year_is_not_supported {

        @Test
        void if_it_is_zero() {
        }

        @DisplayName("A negative value for year is not supported by the leap year computation.")
        @ParameterizedTest(name = "For example, year {0} is not supported.")
        @ValueSource(ints = { -1, -4 })
        void if_it_is_negative(int year) {
        }

    }

    @Nested
    @DisplayNameGeneration(IndicativeSentences.class)
    class A_year_is_a_leap_year {

        @Test
        void if_it_is_divisible_by_4_but_not_by_100() {
        }

        @ParameterizedTest(name = "Year {0} is a leap year.")
        @ValueSource(ints = { 2016, 2020, 2048 })
        void if_it_is_one_of_the_following_years(int year) {
        }

    }

    static class IndicativeSentences extends DisplayNameGenerator.ReplaceUnderscores {

        @Override
        public String generateDisplayNameForClass(Class<?> testClass) {
            return super.generateDisplayNameForClass(testClass);
        }

        @Override
        public String generateDisplayNameForNestedClass(Class<?> nestedClass) {
            return super.generateDisplayNameForNestedClass(nestedClass) + "...";
        }

        @Override
        public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) {
            String name = testClass.getSimpleName() + ' ' + testMethod.getName();
            return name.replace('_', ' ') + '.';
        }

    }

}
import static java.time.Duration.ofMillis;
import static java.time.Duration.ofMinutes;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeout;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;

import example.domain.Person;
import example.util.Calculator;

import org.junit.jupiter.api.Test;

class AssertionsDemo {

    private final Calculator calculator = new Calculator();

    private final Person person = new Person("Jane", "Doe");

    @Test
    void standardAssertions() {
        assertEquals(2, calculator.add(1, 1));
        assertEquals(4, calculator.multiply(2, 2),
                "The optional failure message is now the last parameter");
        assertTrue('a' < 'b', () -> "Assertion messages can be lazily evaluated -- "
                + "to avoid constructing complex messages unnecessarily.");
    }

    @Test
    void dependentAssertions() {
        // Within a code block, if an assertion fails the
        // subsequent code in the same block will be skipped.
        assertAll("properties",
            () -> {
                String firstName = person.getFirstName();
                assertNotNull(firstName);

                // Executed only if the previous assertion is valid.
                assertAll("first name",
                    () -> assertTrue(firstName.startsWith("J")),
                    () -> assertTrue(firstName.endsWith("e"))
                );
            },
            () -> {
                // Grouped assertion, so processed independently
                // of results of first name assertions.
                String lastName = person.getLastName();
                assertNotNull(lastName);

                // Executed only if the previous assertion is valid.
                assertAll("last name",
                    () -> assertTrue(lastName.startsWith("D")),
                    () -> assertTrue(lastName.endsWith("e"))
                );
            }
        );
    }

    @Test
    void timeoutNotExceededWithResult() {
        // The following assertion succeeds, and returns the supplied object.
        String actualResult = assertTimeout(ofMinutes(2), () -> {
            return "a result";
        });
        assertEquals("a result", actualResult);
    }

    @Test
    void timeoutExceeded() {
        // The following assertion fails with an error message similar to:
        // execution exceeded timeout of 10 ms by 91 ms
        assertTimeout(ofMillis(10), () -> {
            // Simulate task that takes more than 10 ms.
            Thread.sleep(100);
        });
    }


    private static String greeting() {
        return "Hello, World!";
    }
}
@Test
@EnabledOnOs({ LINUX, MAC })
void onLinuxOrMac() {
    // ...
}

@Test
@EnabledOnJre({ JAVA_9, JAVA_10 })
void onJava9Or10() {
    // ...
}

@Test
@DisabledIfEnvironmentVariable(named = "ENV", matches = ".*development.*")
void notOnDeveloperWorkstation() {
    // ...
}

@Test
@EnabledIfSystemProperty(named = "os.arch", matches = ".*64.*")
void onlyOn64BitArchitectures() {
    // ...
}

Més informació: https://junit.org/junit5/docs/current/user-guide/

AspectJ

Filed under: Ciència i tecnologia — Joaquim Perez Noguer at 12:30 pm on Divendres, Febrer 8, 2019

Enllaços:

Spring Annotations
---------------

Spring AspectJ AOP implementation provides many annotations:

    @Aspect declares the class as aspect.
    @Pointcut declares the pointcut expression.

The annotations used to create advices are given below:

    @Before declares the before advice. It is applied before calling the actual method.
    @After declares the after advice. It is applied after calling the actual method and before returning result.
    @AfterReturning declares the after returning advice. It is applied after calling the actual method and before returning result. But you can get the result value in the advice.
    @Around declares the around advice. It is applied before and after calling the actual method.
    @AfterThrowing declares the throws advice. It is applied if actual method throws exception.


Exemple Spring  @AfterReturning sense @PointCut
-----------------------------------------------
  
import org.aspectj.lang.JoinPoint;  
import org.aspectj.lang.annotation.AfterReturning;  
import org.aspectj.lang.annotation.Aspect;  
  
@Aspect  
public class TrackOperation{  
    @AfterReturning(  
              pointcut = "execution(* Operation.*(..))",  
              returning= "result")  
                
    public void myadvice(JoinPoint jp,Object result)//it is advice (after returning advice)  
    {  
        System.out.println("additional concern");  
        System.out.println("Method Signature: "  + jp.getSignature());  
        System.out.println("Result in advice: "+result);  
        System.out.println("end of after returning advice...");  
    }  
}  


Exemple amb @PointCut
---------------------
import org.aspectj.lang.JoinPoint;  
import org.aspectj.lang.annotation.Aspect;  
import org.aspectj.lang.annotation.After;  
import org.aspectj.lang.annotation.Pointcut;  
  
@Aspect  
public class TrackOperation{  
    @Pointcut("execution(* Operation.*(..))")  
    public void k(){}//pointcut name  
      
    @After("k()")//applying pointcut on after advice  
    public void myadvice(JoinPoint jp)//it is advice (after advice)  
    {  
        System.out.println("additional concern");  
        //System.out.println("Method Signature: "  + jp.getSignature());  
    }  
}  


Exemple @Around
-----------------
@Aspect
public class MethodLogger {
  @Around("execution(* *(..)) && @annotation(Loggable)")
  public Object around(ProceedingJoinPoint point) {
    long start = System.currentTimeMillis();
    Object result = point.proceed();
    Logger.info(
      "#%s(%s): %s in %[msec]s",
      MethodSignature.class.cast(point.getSignature()).getMethod().getName(),
      point.getArgs(),
      result,
      System.currentTimeMillis() - start
    );
    return result;
  }
}


Example Eclipse AspectJ
---------------------

aspect ColorControl {
    pointcut CCClientCflow(ColorControllingClient client):
        cflow(call(* * (..)) && target(client));   // controla quan quan es fa qualsevol crida a client

    pointcut make(): call(FigureElement Figure.make*(..));   // controla quan executa qualsevol mètode make...() de Figure.java

    after (ColorControllingClient c) returning (FigureElement fe):
            make() && CCClientCflow(c) {
        fe.setColor(c.colorFor(fe));
    }
}


aspect ContextFilling {
    pointcut parse(JavaParser jp):
        call(* JavaParser.parse*(..))
        && target(jp)
        && !call(Stmt parseVarDec(boolean)); // var decs
                                              // are tricky

    around(JavaParser jp) returns ASTObject: parse(jp) {
        Token beginToken = jp.peekToken();
        ASTObject ret = proceed(jp);
        if (ret != null) jp.addContext(ret, beginToken);
        return ret;
     }
}

Reactive Java

Filed under: Ciència i tecnologia — Joaquim Perez Noguer at 12:27 pm on Divendres, Febrer 8, 2019

Reactive té a veure com optimitzar la gestió de cues de treball. Intentant minimitzar la interdependència de threads i evitar bloquejos.

Generació 0 :

Quan java feia servir callbacsk de l’estil addXXXListener(…) en Swing, AWT, Android, …

Generació 1:

Rx.NET, REactive4Java, RxJava, IObservable / IObserver. La implementació no suportava massa càrrega quan el consumidor tardava massa en processar.

Generació 2:

Per millorar l’eficiència s’inclou isUnsubscribed() i lift().

Generació 3:

Es crea l’especificació Reactive-Streams per fer compatible les implementacions.

Apareix RxJava 2, Project Reactor, Akka-Streams.

Generació 4:

S’inclou un optimitzador operator-fusion.

Apareix la implementació Fluent.

Més informació: https://akarnokd.blogspot.com/2016/03/operator-fusion-part-1.html

Webcams a la muntanya

Filed under: Activitats culturals — Joaquim Perez Noguer at 9:39 am on Dimecres, Gener 30, 2019

Webcams a la muntanya, amb vídeo timelapse.

Molt recomanable per saber el temps que fa a la muntanya i quina evolució ha fet els últims dies

Per exmple a la Vall de Núria

https://www.lookr.com/es/lookout/1260608835-Queralbs#action-play-month

Pipe entre servidors

Filed under: Ciència i tecnologia — Joaquim Perez Noguer at 9:32 am on Dimecres, Gener 30, 2019

Solució tar + ssh:

tar -cf - /path/to/backup/dir | ssh remotehost "cat - > backupfile.tar"


Solució cat + ssh:
cat local_script.sh | ssh -A usera@bastion ssh -A userb@privateserver "cat > remote_copy_of_local_script.sh; bash remote_copy_of_local_script.sh"

Solució cat + ssh amb RSA:

ssh root@example.com 'cat >> .ssh/authorized_keys' < .ssh/id_rsa.pub

Solució tar + compress + ssh:

tar czf - . | ssh servername "cd /path-to-dir && tar xzf -"


Solució netcat:

tar -cf - /path/to/backup/dir | ssh remotehost "cat - > backupfile.tar"

Més informació:

Java logs benchmark

Filed under: Ciència i tecnologia — Joaquim Perez Noguer at 10:23 pm on Dimarts, Gener 22, 2019

Vist a https://blog.overops.com/the-logging-olympics-a-race-between-todays-top-5-logging-frameworks/

Conclusions:

  • Log4j és el més ràpid escrivint constants però el més lent fent toString().
  • Log4j2 de Apache és el més ràpid fent toString() o amb un Throwable i és un dels frameworks més recents i amb més opcions.
  • Logback és semblant amb log4j en quant a velocitat, però és molt pitjor escrivint constants.
  • SLF4J SL és el pitjor quan cal escriure un throwable. Només supera a Log4j quan cal escriure toString()
  • JUL, java.utils.logging, és la biblioteca estàndard de logs del Java. Té un bon rendiment quan cal escriure throwable i toString() però té molt mal rendiment amb les constants.


« Previous PageNext Page »