Tag Info

Hot answers tagged

48

The answer seems to be yes. At least, I will try to describe an attempt which would pass the "under 100 lines of code" test. How well it satisfies the other criteria is a subjective matter, but I have already used it for larger-scale project with so far very positive results. I will present two implementations. One is an absolute toy, but extremely simple. ...


20

I know this thread is old and that Leonid provided a great solution, but I'd like to present a bit different approach. My approach is inspired by JavaScript. There are no classes, just "pure objects". To directly answer requirements from question Support instantiation, inheritance and polymorphism. Objects are fully dynamic, everything can be changed in ...


17

At the risk of repeating myself, I would like to stress that one has to be critical towards the superficial flexibility offered by Mathematica, when (particularly mutable) data structures are concerned. Using mutable data structures assumes a programming style for which Mathematica is not optimized. It can emulate it, yes, and we have seen a number of such ...


17

It is probably debatable to what extent it has built-in object oriented features. In any case, this answer is not intended to lead you to try to emulate object oriented programming, which is in general a bad idea. (see @Leonid 's answer) However, it is not debatable that Mathematica is tremendously flexible (as to style and notation at least, the evaluation ...


12

Another, shorter way is Append[d, t] From the docs, DateObject[date,time] represents the specified date list and TimeObject time. If you need the list you mention in the question, just convert the DateObject using DateList: DateList@Append[d, t] (* {2012, 6, 11, 14, 1, 45.} *)


5

How can I recreate this sort of functionality with my own objects and functions? Are there any methodologies for writing down-values of symbols like this? If you want to know how to get a similar output format, here's a silly toy example: (* The icon isn't really that important *) icon = Plot[Sin[x], {x, -5, 5}, Axes -> False, Frame -> True, ...


4

I don't know a way with subvalues, but you can use Module to create objects without explicit identifier in makeObj: makeObj[] := Module[{field = 0}, Switch[#, "increase", field++; #0, "field", field]&] (Note that I slightly changed your "increase" function to return #0, that is, "self", so I can chain ...


4

Well, one obvious idea would be to build on the struct implementation by Bob Beretta. You would have to add information about methods and modify the implementation of --> to consider those as well, and for polymorphism, you'd also have to store the base class (or base classes, if multiple inheritance should be supported), and have --> look there if the ...


4

I believe the crux of your problem is, as you say, how to match n_NewHead. Let's look at the FullForm: FullForm[n_NewHead] Pattern[n, Blank[NewHead]] As you can see this is composed of Blank and Pattern, both of which are special heads with regard to pattern matching. You therefore need to wrap them in Verbatim to make a literal match: MatchQ[ ...


3

Strip off their heads. replace them with List and then Flatten them: dt=DateObject@@(List @@@ {d, t} // Flatten) DateObject[{2012, 6, 11, 14, 1, 45.}] Or .... dt=DateObject @@ Join @@ {d[[1]], t[[1]]}


1

I don't understand why you need this functionality. If you really need Part to write to your object, then again, I suggest you use one of the ways of simulating OOP in MMA (see my linked question in comments). You ask if instead there is a simpler, more obvious approach. I think that all you really need is the following: ContiguousOrderedIntegerMap /: ...



Only top voted, non community-wiki answers of a minimum length are eligible