Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Feb 26 '11 at 3:53

This question came from our site for professional and enthusiast programmers.

    
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
show 4 more comments

8 Answers 8

up vote 8 down vote accepted

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!

share|improve this answer
add comment

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) {
    /*...*/
}

XtAddCallback(button, XmNactivateCallback, get_funky, NULL);

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

share|improve this answer
add comment

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}
share|improve this answer
4  
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  
+1 from me because this answer shows how silly the question is, IMO... –  Dean Harding Feb 26 '11 at 5:21
1  
Whitespace is the new Brainfuck by the way :) en.wikipedia.org/wiki/Whitespace_(programming_language) –  Tungsten Feb 26 '11 at 6:05
2  
Maybe you can't. –  Jon Purdy Feb 26 '11 at 9:04
add comment

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

void foo( int * pInt) 
share|improve this answer
add comment

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.

share|improve this answer
2  
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
add comment

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
share|improve this answer
    
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
add comment

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.

share|improve this answer
    
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
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.