# Expressions that are idiomatic in one language but not used or impossible in another [on hold]

I often find myself working in unfamiliar languages. I like to read code written by others and then jump in and write something myself before going back and learning the corners of each language.

To speed up this process, it really helps to know a few of the idioms you'll encounter ahead of time.

Some of these, I've found are fairly unique.

In Python you might do something like this:

'\n'.join(listOfThings)


Not all languages allow you to call methods on string literals like this.

In C, you can write a loop like this:

int i = 50;
while(i--) {
/* do something 50 times */
}


C lets you decrement in the loop condition expression. Most more modern languages disallow this.

Do you have any other good examples? I'm interested in often used constructions not odd corner cases.

-

## migration rejected from stackoverflow.com8 hours ago

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

## put on hold as too broad by MichaelT, KChaloux, GlenH7, Giorgio, Josh Kelley8 hours ago

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

I voted to move this to programmers.stackexchange.com. –  Andrew Hare Feb 26 '11 at 3:43
@Robert: In which language? That should be an implicit conversion in C and C++. –  Jeremiah Willcock Feb 26 '11 at 4:08
Under what compiler? I have never encounter a C compiler where that wasn't valid. Any none zero value is considered TRUE in C –  Pemdas Feb 26 '11 at 4:09
Aw, damn. Didn't look at the tags. Still, I wouldn't exactly call the second example idiomatic. –  Robert Harvey Feb 26 '11 at 4:11
@Robert Yeah, maybe not idiomatic, but I've seen it several times in the wild. (As for it being an error, it does say "C" both above and below the code.) –  Tungsten Feb 26 '11 at 4:22

Dealing with laziness is something that's very idiomatic in Haskell and, say, Clojure, but quite rare in most languages. In Haskell, it's common to see [0..], the infinite list, make appearances!

-

I cannot do the following in Brainfuck:

\begin{tabular}{ r c l }
$$10xy^2+15x^2y-5xy$$ & $$=$$ & $$5\left(2xy^2+3x^2y-xy\right)$$ \\
& $$=$$ & $$5x\left(2y^2+3xy-y\right)$$ \\
& $$=$$ & $$5xy\left(2y+3x-1\right)$$
\end{tabular}

-
comparing brainfuck to latex? What the fried monkey? –  Winston Ewert Feb 26 '11 at 4:33
@Winston Ewert, I would like to remind you of the title: "Expressions that are idiomatic in one language but not used or impossible in another". –  Job Feb 26 '11 at 4:37
+1 from me because this answer shows how silly the question is, IMO... –  Dean Harding Feb 26 '11 at 5:21
Whitespace is the new Brainfuck by the way :) en.wikipedia.org/wiki/Whitespace_(programming_language) –  Tungsten Feb 26 '11 at 6:05
Maybe you can't. –  Jon Purdy Feb 26 '11 at 9:04

In ruby you can re-open classes at any time to re/define functionality, for example:

class String
def greet
"Hello, #{self}!"
end
end
"World".greet # => "Hello, World!"
"Jimmy".greet # => "Hello, Jimmy!"


However, most other languages I can think of only allow you to define a class in a single place.

-
A number of other languages, including other major scripting languages (JavaScript, Perl and Python), allow such monkeypatching. To the best of my knowledge, Ruby is the only one with a community that both tolerates and encourages it. Needless to say, the practice is an excellent way to get obscure and difficult to debug software conflicts. –  btilly Feb 26 '11 at 5:45
C# allows "monkey patching" via extension methods. It also has the concept of partial classes, where a class's definition can be split across multiple file within an assembly. –  Ben Hughes Feb 26 '11 at 9:10

Pointers are pretty idiomatic in C and don't exist in languages like VB

void foo( int * pInt)

-

Anonymous functions in JavaScript are all over the place:

o.click(function() { whereIsPancakeHouse() });


but you can't do it in C, in C you have to define a separate named function and pass a pointer to it:

void get_funky(Widget widget, XtPointer client_data, XtPointer call_data) {
/*...*/
}



Anonymous functions and blocks are idiomatic in Perl and Ruby as well.

-

The various ways in Ruby to deal with string literals was impressive to me. Nothing this powerful is in .NET, aside from IronRuby of course ;)

Examples:

myString = %&Any text between the ampersands is considered valid, including "this" and 'this'&

myString = %*Any text between the asterisks is considered valid, including "this" and 'this'*

myString = <<DOC
Alternatively, any text between DOC and DOC is considered
valid, including "this" and 'this'.
Multi-line is valid too.
DOC

-
Heredocs (<<DOC) come from the Borne Shell probably via Perl. The various quote operators (%q, %Q, ...) come from Perl's q{}, qq{}, ... Just a little history lesson but, yeah, the extra quoting options are pretty nice for dealing with embedded quotes or embedding SQL (or other multi-line blocks of text) in your code. –  mu is too short Feb 27 '11 at 3:20

In Ruby, people add methods to integers. See ActiveSupport::CoreExtensions::Numeric::Time for an example. Ruby blocks are also fairly distinctive, even though under the hood they are basically just closures.

Perl has quite a few unique ideas, including context and tie. Some prominent ones, including the syntax for regular expressions and default variables, also appear in Ruby.

Python generators and list comprehensions are idiomatic and not widely copied elsewhere.

Various Lisp dialects use macros in ways that are unthinkable in most other languages. Read On Lisp to understand what I mean.

-
Although it should be noted that Python copied generators and list comps from Haskell, then Javascript copied the comprehensions part from Python. –  ncoghlan Feb 28 '11 at 2:33

C:

while(ch=getchar()!='0'){ ... }


Stores a character into ch and runs the loop body (...) unless the character was the ASCII representation of 0.

This is possible in C because assignment is an operator and not an expression. The = operator evaluates to the value on the right side of the operator (that ch gets assigned is just a side-effect of the operator). This value is then compared to '0'.

-

C:

var = ((a = foo()), b = bar(), baz(), a + b);

Using the comma operator to perform multiple operations, but return the right most expression.

Can use used in macros for eg, growing an array and assigning a value (and many other things of course).

#define ARRAY_DECLARE(arr)                                                    \
int   _##arr##_count = ((void)(sizeof(*(arr))), 0);                       \

#define ARRAY_APPEND_R(arr, item)  (                                          \
(void) assert(_##arr##_count < (sizeof(arr) / sizeof(*arr))),             \
(void) (arr[_##arr##_count - 1] = item),                                  \
(&arr[_##arr##_count - 1])                                                \

/* example use */

{
int arr[32];
ARRAY_DECLARE(arr);

int *arr_first  = ARRAY_APPEND_R(arr, 4);
int *arr_second = ARRAY_APPEND_R(arr, 2);
}

-
The equivalent of the Comma Operator exists in many languages: The semicolon of Ocaml, the progn in Common Lisp, the begin of Scheme, etc... –  Basile Starynkevitch 15 hours ago
While this works in some other languages, the ability to do assignment within expressions makes it more adaptable. You wouldn't be able to do this in Python (unless you do tricks manipulating locals() / globals()) for example, and Python would treat the commas as a constructor for a tuple. –  ideasman42 15 hours ago