dry

How to reduce code duplication when dealing with recursive sum types

好久不见. 提交于 2020-02-17 05:16:34
问题 I am currently working on a simple interpreter for a programming language and I have a data type like this: data Expr = Variable String | Number Int | Add [Expr] | Sub Expr Expr And I have many functions that do simple things like: -- Substitute a value for a variable substituteName :: String -> Int -> Expr -> Expr substituteName name newValue = go where go (Variable x) | x == name = Number newValue go (Add xs) = Add $ map go xs go (Sub x y) = Sub (go x) (go y) go other = other -- Replace

How to reduce code duplication when dealing with recursive sum types

百般思念 提交于 2020-02-17 05:15:07
问题 I am currently working on a simple interpreter for a programming language and I have a data type like this: data Expr = Variable String | Number Int | Add [Expr] | Sub Expr Expr And I have many functions that do simple things like: -- Substitute a value for a variable substituteName :: String -> Int -> Expr -> Expr substituteName name newValue = go where go (Variable x) | x == name = Number newValue go (Add xs) = Add $ map go xs go (Sub x y) = Sub (go x) (go y) go other = other -- Replace

I have two methods which are nearly the same, how to refactor them?

别说谁变了你拦得住时间么 提交于 2020-01-25 04:38:53
问题 I am having two DatePickerFragment s because I need to pick up a start and end time. private void showDatePickerTimePeriodStart() { final DatePickerFragment date = new DatePickerFragment(); // Sets up the current date in Dialog. final Calendar calender = Calendar.getInstance(); final Bundle args = new Bundle(); args.putInt("year", calender.get(Calendar.YEAR)); args.putInt("month", calender.get(Calendar.MONTH)); args.putInt("day", calender.get(Calendar.DAY_OF_MONTH)); date.setArguments(args);

How do I DRY up this script?

我只是一个虾纸丫 提交于 2020-01-17 02:50:10
问题 I have been plugging away at this navigation code, and I finally got the 1st layers of my menu to work. Yay! But I've noticed that my code ONLY works on the 1st layers. Well, kinda. Open menus are supposed to close when you click on a different menu. It works for my top-level nav options, but not my sub-menu options. I'm tempted to just copy/paste the code so that everything works for all levels, but I know one big rule in coding is Don't Repeat Yourself (DRY). So could someone have a look at

how to share javascript code between front- and back-end (ES6)

非 Y 不嫁゛ 提交于 2020-01-15 06:27:09
问题 This is an ES6-specific duplicate of this SO thread, which details how to create a javascript module that can be exported for use in both a browser and node context. I would be just as glad for an answer to appear there, if that's more appropriate. For background, here's a quote of the ES5 solution, as well as the problems that arise in trying to translate it to ES6. (Credit to users @caolan and @broesch.) (function(exports){ // Your code goes here. For example: exports.test = function(){

“AngularJS way” to map values for output in a view

断了今生、忘了曾经 提交于 2020-01-14 10:46:49
问题 Am seeking advice on a beginner problem with AngularJS: My app has lots of cases where the API it's working with gives a value from a list of possible types, I've pulled the value into my model and then I want to map it to something, eg icons for each type. As I'm likely to be re-using the mapping of a few of these common types quite frequently, what's a good way to do this that is efficient and allows re-use? I'm trying to keep it "DRY" and stick to the "Separation of concerns" so the

Symfony2 and be DRY approach in controllers

∥☆過路亽.° 提交于 2020-01-13 10:16:29
问题 I'm developing a small CMS for my company using Symfony2. I really love this framework. I love form classes and reusing them (that's all about forms after all). But (yes, there is a "but") I'm feeling like I'm doing the same stuff, copy and paste in all controllers . A code duplication that we hate. With all the business logic moved to Services and forms, events, persist actions in Doctrine, all my controllers do the same thing: Get the respository $this->get('mycompany.repository.entity')

Implementing dry-run in ruby script

时光总嘲笑我的痴心妄想 提交于 2020-01-11 10:32:11
问题 anybody know how to implement dry-run option in Ruby? I need something like this, but only for ruby https://serverfault.com/questions/147628/implementing-dry-run-in-bash-scripts I've tried this, but part after else doesn't work: DRY_RUN = true def perform(*args) command = args if DRY_RUN command.each{|x| puts x} else command.each {|x| x} end end perform("puts 'Hello'") Thanks for any idea in advance. P.S I don't want use something like system ("ruby -e \"puts 'Hello'\"") 回答1: This could help:

Implementing dry-run in ruby script

送分小仙女□ 提交于 2020-01-11 10:32:05
问题 anybody know how to implement dry-run option in Ruby? I need something like this, but only for ruby https://serverfault.com/questions/147628/implementing-dry-run-in-bash-scripts I've tried this, but part after else doesn't work: DRY_RUN = true def perform(*args) command = args if DRY_RUN command.each{|x| puts x} else command.each {|x| x} end end perform("puts 'Hello'") Thanks for any idea in advance. P.S I don't want use something like system ("ruby -e \"puts 'Hello'\"") 回答1: This could help:

Should mapping value be declared in a constant or as an enum?

99封情书 提交于 2020-01-10 19:08:07
问题 I see this scattered throughout code base: @RequestMapping(value = "myValue") I would prefer to use something like this: @RequestMapping(value = Constants.myValue) It seems to break DRY using the actual String value within @RequestMapping instead of constant. But is this good code practice? Should I use an enum instead? I may need to use Constants.myValue elsewhere in the code base. 回答1: Should I use an enum instead? You can't. Annotation variables must be compile-time constants. Enums and