Hot answers tagged

8

I'd design the StateMachine differently. The StateMachine does not need to know about all the possible Transitions between the States. All the StateMachine should care about is the current State it is in. The State itself should know about all possible Transitions (I call them Edges). So if an Event occurs, the StateMachine asks the current State, whether ...


8

First, I have to mention that I'm very happy that you chose to explore antlr! ...looking at what you've done here, you now know more about Visitor and ErrorNode than I do! But ANTLR aside... Classes are internal unless specified otherwise; still it would be nice to be explicit about it, and stick that internal modifier in front of your internal classes. I ...


7

Removing duplicated code The same code seems to be duplicated in various places. You can easily define a generic function that you'll be able to reuse in all your definitions. Here's what I have done (not so tested) : def generic_compare(self, command, func, error): if command[1] == self.st_brace and command[3] in self.OPERATORS and command[5] == self....


6

I found your code to be very interesting and I'd like to thank you for putting it up for review. You are doing some things here that I found very interesting, especially the usage of returning a ReadOnlyProperty dynamically like that - That's something I haven't thought of before and might find some use for in the future. Is this anything new? The idea of ...


6

Your code isn't fully PEP-8 (the Python style guide) compliant. Function names should be lowercase_with_underscores. You also have a lot of duplicated code. I would suggest pulling out the common components and making it a generic function. I basically just pulled out the core logic, added a type parameter, and added in separate cases for int, str, and etc....


6

I'll start with the code and explain after let foldScript = let isScriptNonEmpty { Script = script } = script <> "" let foldAppScripts = let foldScripts = Seq.fold (fun acc { Script = script } -> sprintf "%s\r\n%s" acc script) "" Seq.groupBy (fun { App = app } -> app) >> Seq.map (fun (app, group) -> sprintf "\r\n\tuse ...


5

In general, the techniques and logic appear to be fine. My suggestions for making this more idiomatic would include: Extract out the tuple into a record. Using a string*string*string to represent the values(store, app, script) is less clear than having a type for it: type Script = { Store : string ; App : string ; Script : string } Consider using ....


4

A few thoughts... It's not at all obvious how the user gets started. new PersonBuilder().blahblahblah()? A common alternative idiom is to use a public static method as the bootstrap point. PersonBuilder.newInstance(), PersonBuilder.create(), or even PersonBuilder.start() I'm bothered by the presence of new in the middle of the test setup public class ...


4

Can anyone suggest a more idiomatic way of representing this? The equations for the ALU have the form (out !! i, cout !! i) = aluc (a !! i) (b !! i) (cout !! (i - 1)) so we should be able to express all of the equations with a single equation that manipulates lists. If only cout !! (i - 1) would be something !! i, this would be easier because all the ...


4

Cool stuff! You might also be interested in Galois's Ivory DSL, which does similar things, and has been used to write the code for an autonomous quadcopter / some Boeing autonomous helicopter. It's full of cool tricks that may be relevant: https://hackage.haskell.org/package/ivory http://dl.acm.org/citation.cfm?id=2804318 For FFI with C, it might also ...


4

Is Janua really a valid month? If so, then you're fine, but I have a feeling that this would be more appropriate. DateMonthJanuary ::= 'January' | 'Jan' | '0'? '1' Which brings me to my next problem. It's case sensitive, right? So, 22-JAN-2015 wouldn't match. I believe it should, but I'm not familiar with Robol. Please correct me if I'm wrong. I've seen ...


4

You could take a page out of functional programming and shorten the visitors for the multiply, divide, subtract, and add: public override int VisitAddition(BasicMathParser.AdditionContext context) { return VisitHelper(context, (left, right) => left + right); } public override int VisitSubtraction(BasicMathParser.SubtractionContext context) { ...


4

Things you've done well There's a lot to like here. This is good code, easy to read and understand. I especially like: Comments Standard formatting (two-space indent, etc.) Short methods A little more vertical white space Comment blocks that preceed a method, module, or class definition should be preceded by a blank line. So instead of this: module ...


3

I'm sure there might be a few minor things that could still be improved upon, but I believe that what we ended up with after Simon's fantastic advice is worth sharing as it is now. fun chooseVehicles(builderBlock: ColorChoice.() -> CompleteVehicle): List<Vehicle> { return ColorChoice().builderBlock().toList() } class ColorChoice internal ...


3

The App class groups a huge list of functionalities: it parses different commands ("new", "del", "connect" etc) and queries, executes them, produces output et alia. It looks like it is very much against the SRP and is just not easy to navigate into. I'd suggest that each CommandHandler definition should represent a dedicated class outside App. All of these ...


3

Escaping and SQL injections One of the major problems in the presented code is that the code is vulnerable to SQL injection attacks and does not properly sanitize, validate and escape the field values. For example, if a last_name contains a single quote, for example O'Reilly: { "field": "last_name", "operator": "NEQ&...


3

try-with-resources try { BufferedReader br = new BufferedReader(new FileReader(readerConfig.getPath())); String row = ""; while ((row = br.readLine()) != null) { String[] properties = row.split(readerConfig.getDelimiter()); rows.add(properties); } br.close()...


2

This is a case where a dynamic variable is the ideal solution: (def ^:dynamic *described-entity*) (defn it [fun & v] (assert (apply fun *described-entity* v))) (def it-is it) (def it-is-the it) (defmacro describe [value nest] `(binding [*described-entity* ~value] ~nest)) (defmacro when-applied-to [& args-and-nest] (let [args (...


2

There is already a gem for encrypting data at rest. Does this work for you? https://github.com/attr-encrypted/attr_encrypted


2

Some notes: The recommended way to overwrite accessors is to use super. As @Flambino says, it's better not to hardcode secrets directly in the code. It can be a file not committed to the repository. I'd write the crypt method this way: def crypt @crypt ||= begin key = ActiveSupport::KeyGenerator.new('password').generate_key('salt') ...


1

This is a really cool idea. As far as limiting the cognitive overhead of the lambdas, you could do a combination of what Gjallarhorn and Argu do: The idea from Gjallarhorn is to have the user create a dummy value of the type you want to make a lambda of, and then instead of a lambda have the user just access a property on that type: let dummyInput: ...


1

Here's another way to implement the list() method: switch(elements.size()) { case 0: null break case 1: elements[0] break case 2: elements.join(" $conjunction ") break default: new StringBuilder().with { append elements.take(elements.size() - 1).join(', ') ...


1

From a now deleted answer by Bart Kiers. You've given multiplication a higher precedence than division. Usually, these have the same precedence. I.e., the expression 1 / 2 * 3 is usually evaluated as (1 / 2) * 3 (from left to right), which is 1.5. But in your case, it will be evaluated as 1 / (2 * 3), which is 0.16666666666. Also, your NUMBER ...


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