Sunday, October 27, 2013

Prolog: Insert an element on a given position

%Insert an element onto the n-th position of a list.
domains
element=integer
list=element*

predicates
insert(list, element, integer, integer, list)

clauses
%If initial list is empty(no elements in the list and CurrentPosition is still 1 because it was not incremented)
insert([], Element, InsertionPosition, 1, [Element]).

%We reached the moment in which the initial list is empty, but CurrentPosition is not 1(case handled above)
insert([], Element, InsertionPosition, CurrentPosition, []).

%Reached the case in which CurrentPosition is equal to InsertionPosition so we ll insert the element!
insert(List, Element, InsertionPosition, InsertionPosition, [Element| ResultedListTail]):-
CurrentPositionCopy = InsertionPosition + 1,
insert(List, Element, InsertionPosition, CurrentPositionCopy, ResultedListTail).

%By default we should copy element from initial list to resulted list and increment currentPosition
insert([Head| Tail], Element, InsertionPosition, CurrentPosition, [Head| ResultedListTail]):-
CurrentPositionCopy = CurrentPosition + 1,
insert(Tail, Element, InsertionPosition, CurrentPositionCopy, ResultedListTail).


goal
%We will insert element 2 on position 2
insert([1, 3, 4, 5, 6, 7], 2, 2, 1, ResultedList),
write(ResultedList).

Monday, October 7, 2013

Installing phpunit/PHP_Timer issue

After uninstalling phpunit/PHP_Timer and trying to install another version I was getting the message
No releases available for package "pear.phpunit.de/PHP_Timer" install failed
But I did not have PHP_Timer installed at all, the solution for this was to run
pear clear-cache
After that command to install phpunit/PHP_Timer pear install phpunit/PHP_Timer ran successfully!

Saturday, August 24, 2013

Symfony2 routing tip

Symfony2 routing tip

Did you ever needed to make the default value to NULL of a routing parameter when using XML for symfony2?
Here is a sample configuration
<route id="acme_user_show" pattern="/{id}">
    <default key="_controller">AcmeUserBundle:User:show</default>
    <default key="id" xsi:nil="true" />
</route>

Saturday, July 6, 2013

Continuous Integration In PHP

Here is a nice article I found about continuous integration in PHP wrote by Eric Hogue. He covered some aspects and some useful tools like:


  1. PHPUnit
  2. PHP CodeSniffer
  3. PHP Depend
  4. PHP Mess Detector
  5. PHP Dead Code Detector
  6. Jenkins
Most of this tools were written by Sebastian Bergmann. Great work!

I use PHPUnit and code coverage tools and I must say its a great experience, and its very useful! I am planning to use the new tools I discovered on my next project. 

I recommend them to any PHP programmer, even if some of you are not programming in PHP I am sure that there are similar tools for you too. For example JUnit for Java developers.

Saturday, March 2, 2013

Unix swap fields separated by :

Unix homework 1

Requirement: Using sed(Stream editor) write a command which swaps the first and third field in a given file. The fields in the file are separated by colon (:)

Solution: sed 's/^\([^:]*:\)\([^:]*:\)\([^:]*\)/\3:\2\1/;s/:$//' -i your_filename


Explanations:

'^' start at the beginning of the line
\( \) a grouping 
[^:] any character except ':' 
* zero or more times 
: the character ':'
;s/:$// in here ; is used to separate commands, s/:$// is used to remove last :
-i is used to directly edit the file, if you remove -i then the result will be displayed on the screen instead of editing the file

Example of file content:
field1:field2:field3
somefield1:somefield2:somefield3

After running the command:
field3:field2:field1