Discussion of Common Lisp
-
joeish80829
- Posts: 153
- Joined: Tue Sep 03, 2013 5:32 am
Post
by joeish80829 » Sat Dec 07, 2013 6:37 pm
How do i convert c++ class to lisp?
I am new to c++ classes and have 400 lines of basic c++ classes to convert and I was hoping some one could show me how to convert this c++ class to lisp. I know about defclass and defmethod but I was hoping to have a quick example of a proffessional conversion I could use as a template to match my conversion of the rest of the code against...
here is the class
Code: Select all
class TrainingData
{
public:
TrainingData(const string filename);
bool isEof(void) { return m_trainingDataFile.eof(); }
void getTopology(vector<unsigned> &topology);
// Returns the number of input values read from the file:
unsigned getNextInputs(vector<double> &inputVals);
unsigned getTargetOutputs(vector<double> &targetOutputVals);
private:
ifstream m_trainingDataFile;
};
-
marcoxa
- Posts: 85
- Joined: Thu Aug 14, 2008 6:31 pm
Post
by marcoxa » Sun Dec 08, 2013 3:40 am
With care and understanding that C++ "hiding rules" are not the same as CL. Plus, and this is the biggie, methods (or "member functions" in C++) are *NOT* part of a class definition. CLOS classes are essentially "data only classes" (something that some SW engineer does not grasp as useful, but I digress

)
A per your case...
Code: Select all
#|
class TrainingData
{
public:
TrainingData(const string filename);
bool isEof(void) { return m_trainingDataFile.eof(); }
void getTopology(vector<unsigned> &topology);
// Returns the number of input values read from the file:
unsigned getNextInputs(vector<double> &inputVals);
unsigned getTargetOutputs(vector<double> &targetOutputVals);
private:
ifstream m_trainingDataFile;
};
|#
(defclass training-data ()
((training-data-stream :reader training-data-stream
:writer |set training data stream (using a funky name)|
:initarg :training-data-stream))
)
;;; initialize-instance :after -- This is your 'constructor' code.
(defmethod initialize-instance :after ((td training-data)
&key
training-data-stream
&allow-other-keys)
;; Initialize the slot.
;; Note that 'opening' a stream, as it is implied by the C++ class
;; definiton and leaking the open stream handle is very impolite.
(let ((is (etypecase training-data-stream
((string pathname) (open training-data-stream :direction :input))
(stream (if (input-stream-p training-data-stream)
training-data-stream
(error "Not an input stream.")))))
)
(|set training data stream (using a funky name)| is td)
))
(defgeneric is-eof (td)
(:method ((td training-data))
;; Are you sure you want this method?
))
(defgeneric get-topology (td))
(defgeneric get-next-inputs (td))
(defgeneric get-target-outputs (td))
;;; Later, or elsewhere...
(defmethod get-topology ((td training-data))
;; Get the 'topology'
;; yadda-yadda-yadda
topology ; a (VECTOR UNSIGNED-BYTE *)
)
(defmethod get-next-inputs ((td training-data))
;; Get the next inputs
(values
n-next-inputs
next-inputs ; a (VECTOR DOUBLE-FLOAT *)
)
)
(defmethod get-target-outputs ((td training-data))
;; Get the next inputs
(values
n-targets
target-outputs ; a (VECTOR DOUBLE-FLOAT *)
)
)
Marco Antoniotti