Fat comma

Last updated

The fat comma (also termed hash rocket in Ruby and a fat arrow in JavaScript) is a syntactic construction that appears in a position in a function call (or definition) where a comma would usually appear. The original usage refers to the ")letters:(" construction in ALGOL 60. Newer usage refers to the "=>" operator present in some programming languages. It is primarily associated with PHP, Ruby and Perl programming languages, which use it to declare hashes. Using a fat comma to bind key-value pairs in a hash, instead of using a comma, is considered an example of good idiomatic Perl. [1] In CoffeeScript and TypeScript, the fat comma is used to declare a function that is bound to this. [2] [3]

Contents

# a typical, idiomatic use of the fat comma in Perlmy%hash=(first_name=>"Larry",last_name=>"Wall",);

Subtleties

ALGOL 60

The ALGOL "fat comma" is semantically identical to the comma. [4] In particular, whether letter strings are used, and what their contents are, need not match between the definition of a function and its uses. The following are equivalent:

S(s-5, T, P) S(s-5) t: (T) p: (P) S(s-5) Temperature: (T) Pressure: (P) 

Perl

The "fat comma" forces the word to its left to be interpreted as a string. [5]

Thus, where this would produce a run-time error under strict (barewords are not allowed):

%bad_example=(bad_bareword,"not so cool");

the following use of the fat comma would be legal and idiomatic:

%good_example=(converted_to_string=>"very monkish");

This is because the token converted_to_string would be converted to the string literal "converted_to_string" which is a legal argument in a hash key assignment. The result is easier-to-read code, with a stronger emphasis on the name-value pairing of associative arrays.

PHP

In PHP, the fat comma is termed a double arrow, and is used to specify key/value relationships when declaring an array. Unlike in Perl, the double arrow does not treat what comes before it as a bare word, but rather evaluates it. Hence, constants used with the double arrow will be evaluated:

$array=array("name"=>"PHP","influences"=>array("Perl","C","C++","Java","Tcl"));

Ruby

In Ruby, the fat comma is the token to create hashes. Ruby 1.9 introduced a special syntax to use symbols as barewords. [6] [7] In Ruby, the fat comma is called a hash rocket. [7]

# Old syntaxold_hash={:name=>'Ruby',:influences=>['Perl','Python','Smalltalk']}# New syntax (Ruby >= 1.9)new_hash={name:'Ruby',influences:['Perl','Python','Smalltalk']}

Use as lambda functions

The fat arrow is used to declare single expression anonymous functions in JavaScript, [8] and C sharp. [9]

Related Research Articles

<span class="mw-page-title-main">Ruby (programming language)</span> General-purpose programming language

Ruby is an interpreted, high-level, general-purpose programming language which supports multiple programming paradigms. It was designed with an emphasis on programming productivity and simplicity. In Ruby, everything is an object, including primitive data types. It was developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan.

In computer science, an associative array, map, symbol table, or dictionary is an abstract data type that stores a collection of pairs, such that each possible key appears at most once in the collection. In mathematical terms, an associative array is a function with finite domain. It supports 'lookup', 'remove', and 'insert' operations.

This is a "genealogy" of programming languages. Languages are categorized under the ancestor language with the strongest influence. Those ancestor languages are listed in alphabetic order. Any such categorization has a large arbitrary element, since programming languages often incorporate major ideas from multiple sources.

In computer programming, an assignment statement sets and/or re-sets the value stored in the storage location(s) denoted by a variable name; in other words, it copies a value into the variable. In most imperative programming languages, the assignment statement is a fundamental construct.

<span class="mw-page-title-main">Conditional (computer programming)</span> Control flow statement that executes code according to some condition(s)

In computer science, conditionals are programming language commands for handling decisions. Specifically, conditionals perform different computations or actions depending on whether a programmer-defined Boolean condition evaluates to true or false. In terms of control flow, the decision is always achieved by selectively altering the control flow based on some condition . Although dynamic dispatch is not usually classified as a conditional construct, it is another way to select between alternatives at runtime. Conditional statements are the checkpoints in the programe that determines behaviour according to situation.

In computer programming, a function object is a construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax. In some languages, particularly C++, function objects are often called functors.

<span class="mw-page-title-main">Foreach loop</span> Control flow statement for traversing items in a collection

In computer programming, foreach loop is a control flow statement for traversing items in a collection. foreach is usually used in place of a standard for loop statement. Unlike other for loop constructs, however, foreach loops usually maintain no explicit counter: they essentially say "do this to everything in this set", rather than "do this x times". This avoids potential off-by-one errors and makes code simpler to read. In object-oriented languages, an iterator, even if implicit, is often used as the means of traversal.

In computer programming, the ternary conditional operator is a ternary operator that is part of the syntax for basic conditional expressions in several programming languages. It is commonly referred to as the conditional operator, ternary if, or inline if. An expression a ? b : c evaluates to b if the value of a is true, and otherwise to c. One can read it aloud as "if a then b otherwise c". The form a ? b : c is by far and large the most common, but alternative syntaxes do exist; for example, Raku uses the syntax a ?? b !! c to avoid confusion with the infix operators ? and !, whereas in Visual Basic .NET, it instead takes the form If(a, b, c).

In computer science, the Boolean is a data type that has one of two possible values which is intended to represent the two truth values of logic and Boolean algebra. It is named after George Boole, who first defined an algebraic system of logic in the mid 19th century. The Boolean data type is primarily associated with conditional statements, which allow different actions by changing control flow depending on whether a programmer-specified Boolean condition evaluates to true or false. It is a special case of a more general logical data type—logic does not always need to be Boolean.

In the Perl programming language, autovivification is the automatic creation of new arrays and hashes as required every time an undefined value is dereferenced. Perl autovivification allows a programmer to refer to a structured variable, and arbitrary sub-elements of that structured variable, without expressly declaring the existence of the variable and its complete structure beforehand.

In computer science, a relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities. These include numerical equality and inequalities.

Programming languages are used for controlling the behavior of a machine. Like natural languages, programming languages follow rules for syntax and semantics.

The following outline is provided as an overview of and topical guide to computer programming:

In computer programming, an anonymous function is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types.

This comparison of programming languages compares the features of language syntax (format) for over 50 computer programming languages.

This Comparison of programming languages (associative arrays) compares the features of associative array data structures or array-lookup processing for over 40 computer programming languages.

This article compares a large number of programming languages by tabulating their data types, their expression, statement, and declaration syntax, and some common operating-system interfaces.

This comparison of programming languages compares how object-oriented programming languages such as C++, Java, Smalltalk, Object Pascal, Perl, Python, and others manipulate data structures.

The structure of the Perl programming language encompasses both the syntactical rules of the language and the general ways in which programs are organized. Perl's design philosophy is expressed in the commonly cited motto "there's more than one way to do it". As a multi-paradigm, dynamically typed language, Perl allows a great degree of flexibility in program design. Perl also encourages modularization; this has been attributed to the component-based design structure of its Unix roots, and is responsible for the size of the CPAN archive, a community-maintained repository of more than 100,000 modules.

References

  1. Conway, Damian (2005). "4: Values and Expressions". In Allison Randal and Tatiana Appandi (ed.). Perl Best Practices . O'Reilly Media, Inc. p. 66. ISBN   0-596-00173-8. Whenever you are creating a list of key/value or name/value pairs, use the "fat comma" (=>) to connect the keys to their corresponding values.
  2. Ashkenas, Jeremy. "Coffeescript Documentation: grammar.coffee". Archived from the original on 4 January 2012. Retrieved 11 December 2011.
  3. "Handbook – Functions".
  4. Revised Report on the Algorithmic Language Algol 60 by Peter Naur, et al.
  5. perldoc.perl.org – perlop – Comma Operator
  6. Galero, Michael. "Ruby 1.9 Hash in Ruby 1.8" . Retrieved 3 April 2008.
  7. 1 2 Nash, Phil. "I don't like the Ruby 1.9 hash syntax". Logical Friday. Archived from the original on 25 June 2011. Retrieved 13 July 2011.
  8. "Fat arrows in javascript".
  9. "Hacking Sharp Lambda Expressions into Hash Rockets". 20 July 2013.