New answers tagged

7

according to Clean Code, is it okay to add a single constructor to a plain data structure? Yes. Constructors are not functions in this context since they're not something that operates on data. They don't hide the data. And if the data changes, they'll need to change, but that's no different from any other initialization strategy.


0

wigy posted a link to some stuff that is highly relevant but I think there are a few simple things that can help here. I'll offer some suggestions that are probably not optimal but might help you move forward. The first thing I would suggest is to avoid iterating over every other user for one user. I would instead iterate through the songs that user has ...


1

Assumed you need a generic solution, where the program is not tied to a specific column structure at run time, you can use an ArrayList of rows, where each row is a string array String[]. You will need an additional HashTable<String,int> to store the mapping of the column name to the column index (a HashMap will probably work, either). So if you have ...


0

If you need performance, you probably need Arrays to hold column values, else 1. you will have memory overhead and 2. accessing lower rows will be slow. Columns can be either a Hashmap (name -> Array) or directly Object fields (the latter one is preferable but requires fields and types to be statically known). This would be the basic, column-based answer ...


6

Yes. Erlang does the same, as well as quite a few other functional programming languages. Using strings as a list of chars makes them very easy to pattern match and reason about. However, their performance is relatively bad: accessing the nth character in a string takes walking through all n-1 items before it. This is thus linear in time. appending ...


2

The "abc" syntax in Prolog is usually a shorthand notation for a list of character codes, i.e. [97,98,99], which is also the case in Erlang. See this question for details. When a Prolog implementation supports a custom string type (e.g. ECLiPSe/SWI/YAP), there are predicates to convert from and to lists of character codes.


5

A Map is precisely the right base data structure here. I'm not sure why it would make you uneasy. It has good lookup and update times, it's dynamic in size, and it's very easy to create derivative data structures from. For example (in haskell): filterWithKey (\k _ -> (snd k) == column) -- All pieces in given column filterWithKey (\k _ -> (fst k) == ...


2

This is a very broad topic. Unfortunately there's no general answer, due to the complex matter of patentability. And in view of the existing practice of patent trolling, there is always be some legal risk to be sued in countries in which software patents are accepted. As a first intro, you may be interested in this WIPO article, and especially TIP3, about ...


5

I've done this recently in F# and I ended up using a one-dimensional list (in F#, that's a single-linked list). In practice, the speed of O(n) list indexer is not a bottleneck for human-usable board sizes. I experimented with other types like 2d array, but in the end, it was the trade-off of either writing my own value-equality-checking code or a translation ...


1

I already have the adjacency info to know neighbors, but how should I store the pieces so that I can detect when to split them into separate objects? What kind of tree would suit this. Rather than a tree I'd use a graph. and know when the branch was severed This could be as simple as waiting until the severing code finishes (and testing for ...


4

You can use the DeriveFunctor extension and just write data Check a = Valid | Invalid | Unsure a deriving Functor


7

One thing you could do is change the definition of your datatype to factour out the Valid / Invalid part like so: data Status = StatusValid | StatusInvalid data Check a = Sure Status | Unsure a You can then implement Functor without having to inspect the Status: instance Functor Check where fmap f (Unsure a) = Unsure (f a) fmap _ (Sure s) = Sure s ...


-1

If the expression is already converted to RPN / postfix, you just need a basic vector or array. Create a simple data structure that holds both values and operator info, and push each item onto the list. struct myToken { int tokType; //1 = value, 2 = operator float tokVal; int opType; //1 = add, 2 = sub, 3 = mult, etc } To re-evaluate the ...


0

(I assume and hope that you want to code in genuine C++11 or C++14 using its standard containers & utilities library; don't use an earlier variant of C++; be sure to have a recent compiler: in june 2016, use GCC version 5 or 6 and/or Clang/LLVM version 3.7 or 3.8, both being free software) You probably want to represent the abstract syntax tree (AST) of ...


2

The problem is that I don't know how to store operators and operands in the same list Consider the composite design pattern: The article goes on to show a math expression example: This lets you build your own tree. If you like you can also store all elements in a list but doThis() is using the tree to traverse. What might be a bit confusing is ...


0

While I think CandiedOrange's answer is a better approach as trees are much more useful for performing operations on expressions, if all you want to do is store it and later evaluate it, a postfix list along with a stack-based evaluatir is adequate and simpler. When you say that there is nothing in common between your operands and operators, that's not ...


0

How to store math expression in c++ list Consider trees The fundamental thing here is that prefix, infix, and postfix notations are only ways of presenting math expressions. When storing you don't have to care how they were presented. The nifty thing about tree's is that you can translate equivalent expressions in these notations into identical tree's. ...


2

This is called sharding in MongoDB and partitioning in SQL Server. If the website actually uses “huge amounts of data”, sharding/partitioning is already implemented. You may probably want to rethink what data you actually need to retrieve, how is it used and how do you store it. If you do auto-completion on product names, you probably don't need to retrieve ...


1

If the equation cannot be solved analytically, you need to use numerical algorithm. To make it simple rewrite the equation into form of X + 2 - Y = 0. Then, it becomes a problem of finding a root of equation using numerical method.


2

Arrays are very low level. You could as well ask, "In java why are int, float, and double not objects, to make them more consistent with the rest of the object-orientated language?" Arrays have a close mapping (in most languages) to the assembly level memory code. This has no real notion of generics, arrays are of pointers or other primitive types. Arrays ...


5

In Java, there is no Array type collection to make arrays feel more consistent, such as in inheritance. Sure there is, it's called List<T>. The only fundamental difference between a hypothetical Array<T> and List<T> is that you can't resize the Array<T>. If Array<T> were added it would be just like List<T> except with ...


3

Regarding Java, Java 1.0 didn't have generics. On the contrary, I'd argue arrays are "first-class citizens" in Java since they're given special treatment by the language. It's true that sometimes you simply need a fixed-size collection, but the case where you need a dynamically-sized collection is more common. Finally, arrays are fine for temporary usage ...



Top 50 recent answers are included