# Expressions that are idiomatic in one language but not used or impossible in another

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.

-
 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

## migrated from stackoverflow.comFeb 26 '11 at 3:53

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 (<

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'`.

-