Hello,

I'm new to functional programming and Clojure and I'm trying to
translate a simple java program into Clojure. My program must build
from 2 input lists (stored in a vector) 2 output lists (also stored in
a vector) which have same number of elements as input lists for both
output lists. The elements' values of the output lists will be the
result of a very simple arithmetical calculation based on input lists'
elements.
Since my java program iterates on lists, I wanted to use a 'higher
order function' in the Clojure version of the program (like the 'map'
function). But I didn't manage to write the equivalent program in
Clojure.
Could someone please help me?
Below is the java program that I'd like to translate.
Thanks a lot,

Regards,
Nicolas

// first file (these are the elements of my lists):
public class Pos {

    public String name;
    public int value;

    public Pos(String newName, int newValue) {
        name = newName;
        value = newValue;
    }

    @Override
    public String toString() {
        return "Name: " + name + ", Value: " + value + "\n";
    }
}

// second file that contains the method I'd like to translate using a
higher order function (method called "match"):

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class Matching {

    public static void main(String[] args) throws Exception {
        List<Pos> options = new ArrayList<Pos>(5);
        Pos option1 = new Pos("IBM", -50);
        Pos option2 = new Pos("ACCOR", -30);
        Pos option3 = new Pos("IBM", -10);
        Pos option4 = new Pos("APPLE", -20);
        Pos option5 = new Pos("AIRFRANCE", -20);
        options.add(option1);
        options.add(option2);
        options.add(option3);
        options.add(option4);
        options.add(option5);

        List<Pos> actions = new ArrayList<Pos>(4);
        Pos action1 = new Pos("IBM", 55);
        Pos action2 = new Pos("ACCOR", 40);
        Pos action3 = new Pos("AIRFRANCE", 10);
        Pos action4 = new Pos("LUFTHANSA", 100);
        actions.add(action1);
        actions.add(action2);
        actions.add(action3);
        actions.add(action4);

        Vector<List<Pos>> input = new Vector<List<Pos>>(2);
        input.set(0, options);
        input.set(1, actions);

        System.out.println("Options: " + options);
        System.out.println("Actions: " + actions);
        Vector<List<Pos>> res = Matching.match(input);
        System.out.println("Options: " + res.get(0));
        System.out.println("Actions: " + res.get(1));
    }


    public static Vector<List<Pos>> match(Vector<List<Pos>>
optionsAndActions) {

        if (optionsAndActions == null) {
            return optionsAndActions;
        }
        if (optionsAndActions.size() < 2) {
            return optionsAndActions;
        }

        Vector<List<Pos>> modifiedOptionsAndActions = new
Vector<List<Pos>>(2);
        if (optionsAndActions.get(1) == null) {
            modifiedOptionsAndActions.add(0, new
ArrayList<Pos>(optionsAndActions.get(0)));
            return modifiedOptionsAndActions;
        } else if (optionsAndActions.get(0) == null) {
            modifiedOptionsAndActions.add(1, new
ArrayList<Pos>(optionsAndActions.get(1)));
            return modifiedOptionsAndActions;
        }
        ArrayList<Pos> modifiedOptions = new
ArrayList<Pos>(optionsAndActions.get(0));
        ArrayList<Pos> modifiedActions = new
ArrayList<Pos>(optionsAndActions.get(1));
        modifiedOptionsAndActions.add(0, modifiedOptions);
        modifiedOptionsAndActions.add(1, modifiedActions);

        for (Pos option : modifiedOptions) {
            for (Pos action : modifiedActions) {

                if (option.name.equals(action.name)) {
                    int tempActionValue = Math.max(0, action.value +
option.value);
                    int tempOptionValue = Math.min(0, action.value +
option.value);
                    action.value = tempActionValue;
                    option.value = tempOptionValue;
                }
            }
        }
        return modifiedOptionsAndActions;
    }
}

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to