conciseness

Is there a shorter way to require a file in the same directory in ruby?

Is there a shorter way to require a file located in the same directory (as the script being executed)? require File.expand_path(File.dirname(__FILE__) + '/some_other_script') I read that require "my_script" and require "./my_script" will actually load the script twice (ruby will not recognize that it is actually the same script), and ...

Array to Hash or 2 binary element Array

I'm seeking the most concise way to do this Given the following Array: ['a','b','c'] how to get this: {'a'=> 1,'b'=> 2, 'c'=> 3} and [['a',1],['b',2],['c',3]] I have few solutions at mind, just want to see yours :) ...

What's the best way to avoid code duplication in these two functions that do the same thing?

Given this form (which contains a submit button): <form id="review_form"> <input type="submit" id="btn_submit" value="Submit with ajax! (submit button)"> </form> and this link (to submit the same form): <a href="#" id="lnk_submit">Submit with ajax! (hyperlink)</a> In the following jQuery code, when the #btn_submit element is clic...

SVN Noob: quick summary of how to use it?

[for those not following along at home, this is the sequel to Rolling My Own Version Control ;)] So i gave in and installed TortoiseSVN (to work with a friend on a project, my personal version control is alive, well, and working as i want it). Having never seriously used one of these before, can someone give me (a link to a) concise exp...

C# list of past DateTimes

I have a method that returns the past x days and it currently does the following: var dates = new List<DateTime>(); for (int i = 0; i < numDays; i++) { dates.Add(DateTime.Today.AddDays(-i)); } return dates; I feel like there should be a more compact way of doing this, perhaps using LINQ. Suggestions? Also, if I do keep it the ...

is it possible to reproduce python's string interpolation in ocaml?

In python, one can use printf like formatting with the "%" operator: "i am %d years old" % 99 or "%s is %d years old" % ("bob", 101) Is there a way to get the same concise syntax in Ocaml, for arbitrary numbers of arguments? For a single argument, the following works: let (%) = Printf.sprintf in ... "i am %d years old" % 99 Is ...

Get the nth item of a generator in Python

Is there a more syntactically concise way of writing the following? gen = (i for i in xrange(10)) index = 5 for i, v in enumerate(gen): if i is index: return v It seems almost natural that a generator should have a gen[index] expression, that acts as a list, but is functionally identical to the above code. ...

Get a list of every value for a given key in a set of dictionaries?

How can I write this code more cleanly/concisely? /// <summary> /// Creates a set of valid URIs. /// </summary> /// <param name="levelVariantURIDicts">A collection of dictionaries of the form: /// dict["filePath"] == theFilePath </param> /// <returns></returns> private ICol...

Concise C# code for gathering several properties with a non-null value into a collection?

A fairly basic problem for a change. Given a class such as this: public class X { public T A; public T B; public T C; ... // (other fields, properties, and methods are not of interest here) } I am looking for a concise way to code a method that will return all A, B, C, ... that are not null in an enumerable collec...

Compound argument declaration

To make my code more concise, can I do something like int f(int const x, y, z), g(int const x, y, z); to declare functions f and g which each take three int const arguments? Edit: Perhaps here is a better example: int f(int const a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z); How would that not be m...

What's the most concise way to get the inverse of a Java boolean value?

If you have a boolean variable: boolean myBool = true; I could get the inverse of this with an if/else clause: if (myBool == true) myBool = false; else myBool = true; Is there a more concise way to do this? ...