jueves 5 de noviembre de 2009

practical Perl 5 match operator

Here I am again, with some reminders about a few practical things I (and maybe you) should have in mind when doing pattern matching in perl. I'll mainly talk about retrieving results in matches, and the context things.

So I won't talk about the pattern part.

The match operator is called with the m/OHAI/ pattern, or just /OHAI/ . if you don't specify something to match against, perl will use our good friend $_ , so when you iterate through a file, you are probably ok with this convention:


my $yay;
while(<>){
$yay = $_ if /say/ ;
}
print $yay;



Sometimes we don't want to match against $_, and we'd like to parse another var. That's ok, we use the =~ operator, which tells 'm' the variable to do the match with. But there is where I get somewhat puzzled sometimes, because if I want to assign the match to a var, you have to chain it

$match = $string =~ /yay/;

And that doesn't do what we usually want.

I've written this little test script to help me remember how to do proper matchings, and get the info I want from them.

martes 3 de noviembre de 2009

Test

Cuando una noche te pasa tan rapido, que pierdes la nocion del tiempo, y a las 6 te gustaría volver a empezarla:


a) Has bebido como un cosaco.
b) Te has puesto como un chamán.
c) Te has reído como nunca.
d) Has compartido momentos únicos.
e) Todas las anteriores son correctas.

lunes 2 de noviembre de 2009

Eficient implementation of Tail recursion in Perl 5



I'm getting more and more interested in fringe programming languages (some of them are use the functional paradigm).

I'm no pro by any means, but little by little, I think I'm starting to grasp what's all that about, and I understand small new tricks every now and then.

In the YAPC::EU::2009, there was a loooooong discussion while we were having dinner about tail recursion, the minimizing of stack space and so.

Until this week, in perl 5, you could emulate tail recursion optimization by implementing it manually in the place where you want the optimisation. An explanation of "HOW" can be found in chapter 13 of "Exploring Programming Language Architecture in Perl" (EPLAiP from now on) , when the author explains how you could implement a sort of CPS in perl.

Well, last week,
Yuval Kogman (a great perl hacker with *LOTS* of useful modules) released Sub::Call::Tail , a Perl 5 module that allows you to call a function and make perl optimize the stack usage. For a brief usage howto, you can visit Yuval's post.

Today, I see in another of his blog posts he just created Sub::Call::Recur , a kind of curried version of Sub::Call::Tail, that just loops over the current sub. As he sais, it's a kind of redo but for functions instead of blocks. The resulting code is quite optimized (nearly as fast as plain iteration), so we can say perl5 has now tail recursion optimization. nothingmuch++;

Byez!

UPDATE:
Seeing http://ezrakilty.net/research/2009/11/function_calls_are_not_stack_frames.html I think I might review the post.... sorry for my ignorance :(

domingo 1 de noviembre de 2009

DrScheme halloween easter egg

Yesterday I was about to try some scheme code from HtDP, and when I ran drscheme, I was presented a different splash screen from the one I used to:

Well, that's all for now. In a few days I'll post about what I was trying (if I find the time)

Happy halloween hacking.

miércoles 28 de octubre de 2009

quoting procedures in scheme and cl, first steps

Disclaimer: This is really newbie stuff for someone with 0.1 experience in scheme/cl, so bear with me.... I'm learning...

I've been a bit puzzled with the huge versatility of scheme functions, when reading the first chapters of this book. The code that made my brain halt was

((if (= 0 x) + -) 9 8)


Depending on the value of x, the result will be 1 or 17.

At first I thought why the symbols + and - should be quoted. But there's absolutely no need of it. Let's see how it works little by little. if we ask scheme (I'm using DrScheme with plain R5S5 here) about the plus sign, it answers it's a procedure, the same as if wrote a lambda function there.

For example, an expanded code, and somewhat less strange (to my eyes at least) would be:


((if (= 0 x)
(lambda (x y)
(+ x y))
(lambda (x y)
(- x y))) 9 8)


This would apply 9 and 8 to the resulting lambda. In that case, those lambdas do the same + and - do in the previous example.

Ok, I think I understand it.

In common lisp, it isn't that straightforward, because cl has 2 different 'namespaces' (probably it's not the right word but...), one for variables and the other one for functions, so if you want to eval a list as a s-exp, you have to use funcall.

(funcall (if nil '+ '-) 2 1)

Anyway, you can evaluate the if statement without the quoted + and - (if nil + -), but I haven't managed to make it evaluate the final sexp.

another way to modify a piece of code is accessing to a code (interpreted as a quoted list)

> (cons - (cdr '(+ 3 2)))
(# 3 2)
> (eval (cons - (cdr '(+ 3 2))))
1


Well, for the moment it's all I've hacked with this kind of self-reference evaluations.

Btw, I've started reading HtDP, to start with an easy introduction to scheme. For the moment, the first 5 chapters are pretty easy to understand, but I felt I needed this basic background to fully understand the upcomming subjects. I'm kind of stalled in PLAI, where I understand most of the text I've been reading, but I'd like to do the exercices, and lately I don't have enough time to hack.

lunes 26 de octubre de 2009

A la puta ruina

El Banco Popular presentó hoy (o ayer) resultados del tercer trimestre de 2009.

Es una pena que solamente ganó un 30% menos que en el mismo período del año pasado. Hecatombe, perdición, cataclismo!! Vamos a ayudar a los bancos, que pobrecitos, no tienen para sobrevivir.

El Banco Popular sólo ha ganado la ingente cifra de 630 millones de euros.

Me cagoen su puta madre.

domingo 25 de octubre de 2009

PRACE and BSC training

Last week, I attended the BSC PRACE, "code porting and optimization workshop" given by some of its researchers.

The course was 3 days long, and I can say I quite enjoyed it.

First day, OpenMP . Full day of an openMP training. from quite easy levels to more complicated stuff, like tasks and other features only present in OpenMP 3.

Second and third day were not as productive as the first one, partly because of the subjects was not so much appliable to my daily work, and partly because of the speakers.

Alex Duran made a great OpenMP training course, and Xavier Teruel (my mus partner 5 years ago) helped us (Jdiaz, JMassanas, and $self) with great explanations when doing the exercises.

I don't know if I can share the slides, but I'll ask BSC people, and upload them (If I can).

We didn't do real complex things, but the overall feeling was that it's really cheap to try simple optimizations here and there in a large program (incremental optimizing), but converting a whole program into a highly parallel one is quite thrilling.

We looked at private,shared,threadprivate... clauses to define the scope of variables.

Follows a helloWorld example using OpenMP, with some calls to omp APIs, to return the number of active threads, max threads, or accurate time to do benchmarks.

Then we took a look at 'task' clause (a bit more complex, but good for task parallelism). I won't publish the code in this post, as it needs a thought explanation I'm not willing to do now. If you feel the absolute need to see a omp code using 'task', mail/comment.

After that, we looked at data parallelism, having a look at 'for', and different schedules. Here, we have a parallel matrix multiplication code, optimized using not only the typical 'for' but also using collapse.



Some insights about balancing loads and other common problems ended up the session.