将许多标准方法扩展为新的自定义矢量类型

时间:2015-05-21 02:24:45

标签: julia

我构建了一个新的矢量类型:

private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;

// url to get all products list
private static String url_all_products = "http://10.207.200.73/list/get_details.php";

/// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "list";
private static final String TAG_PID = "quantity";
private static final String TAG_NAME = "items";

// products JSONArray
JSONArray products = null; 

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.all_products);

  ListView lv = getListView();

    // Listview on item click listener
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

             final CheckBox cb = (CheckBox) view.findViewById(R.id.checkbox);

        }
    });

   // Loading products in Background Thread
   new LoadAllProducts().execute();

我想在我的新类型中扩展许多标准方法,例如加法,减法,逐元素比较等。我是否需要为每个方法定义方法定义,例如:

/**
* Background Async Task to Load all product by making HTTP Request
* */class LoadAllProducts extends AsyncTask<String, String, String> {
   /**
    * Before starting background thread Show Progress Dialog
    * */
   @Override
   protected void onPreExecute() {
       super.onPreExecute();
       pDialog = new ProgressDialog(AllDetails.this);
       pDialog.setMessage("Loading Data. Please wait...");
       pDialog.setIndeterminate(false);
       pDialog.setCancelable(false);
       pDialog.show();
   }

   /**
    * getting All products from url
    * */
   protected String doInBackground(String... args) {
       // Building Parameters
       List<NameValuePair> params = new ArrayList<NameValuePair>();

       // getting JSON string from URL
       JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);


       // Check your log cat for JSON reponse
       Log.d("All Products: ", json.toString());

       try {
           // Checking for SUCCESS TAG
           int success = json.getInt(TAG_SUCCESS);

           if (success == 1) {
               // products found
               // Getting Array of Products
               products = json.getJSONArray(TAG_PRODUCTS);

               // looping through All Products
               for (int i = 0; i < products.length(); i++) {
                   JSONObject c = products.getJSONObject(i);

                   // Storing each json item in variable
                   String id = c.getString(TAG_PID);
                   String name = c.getString(TAG_NAME);

                   // creating new HashMap
                   HashMap<String, String> map = new HashMap<String, String>();

                   // adding each child node to HashMap key => value
                   map.put(TAG_PID, id);
                   map.put(TAG_NAME, name);

                   // adding HashList to ArrayList
                   productsList.add(map);
               }
           } 
       } catch (JSONException e) {
           e.printStackTrace();
       }

       return null;
   }

   /**
    * After completing background task Dismiss the progress dialog
    * **/
   protected void onPostExecute(String file_url) {
       // dismiss the dialog after getting all products
       pDialog.dismiss();
       // updating UI from Background Thread
       runOnUiThread(new Runnable() {
           public void run() {
               /**
                * Updating parsed JSON data into ListView
                * */
               ListAdapter adapter = new SimpleAdapter(
                       AllDetails.this, productsList,
                       R.layout.list_item, new String[] { TAG_PID,
                               TAG_NAME},
                       new int[] { R.id.quantity, R.id.items });

               // updating listview
               setListAdapter(adapter);
           }
       });     
    }}

或者我可以在这里使用一些语法快捷方式吗?

2 个答案:

答案 0 :(得分:6)

以下是使用Julia's Metaprogramming的示例:

for op in (:+, :-, :.<)
    @eval ($op)(a::MyType, b::MyType) = ($op)(a.x, b.x)
end

答案 1 :(得分:3)

您可以从AbstractArray继承并定义一个非常小的接口以免费获取所有基本数组操作:

type MyType <: AbstractVector{Float64}
    x::Vector{Float64}
end
Base.linearindexing(::Type{MyType}) = Base.LinearFast()
Base.size(m::MyType) = size(m.x)
Base.getindex(m::MyType,i::Int) = m.x[i]
Base.setindex!(m::MyType,v::Float64,i::Int) = m.x[i] = v
Base.similar(m::MyType, dims::Tuple{Int}) = MyType(Vector{Float64}(dims[1]))

让我们测试一下:

julia> MyType([1,2,3]) + MyType([3,2,1])
3-element Array{Float64,1}:
 4.0
 4.0
 4.0

julia> MyType([1,2,3]) - MyType([3,2,1])
3-element Array{Float64,1}:
 -2.0
  0.0
  2.0

julia> MyType([1,2,3]) .< MyType([3,2,1])
3-element BitArray{1}:
  true
 false
 false