Logo

Programming-Idioms

History of Idiom 20 > diff from v24 to v25

Edit summary for version 25 by :

Version 24

2015-09-04, 19:57:51

Version 25

2015-10-29, 14:05:12

Idiom #20 Return two values

Implement a function search which looks for item x in a 2D matrix m.
Return indices i, j of the matching cell.
Think of the most idiomatic way in the language to return the two values at the same time.

Idiom #20 Return two values

Implement a function search which looks for item x in a 2D matrix m.
Return indices i, j of the matching cell.
Think of the most idiomatic way in the language to return the two values at the same time.

Code
searcb x m = head [ (i, j) | (i, r) <- zip [0..] m,
                             (j, c) <- zip [0..] r, c == x]


Code
searcb x m = head [ (i, j) | (i, r) <- zip [0..] m,
                             (j, c) <- zip [0..] r, c == x]


Comments bubble
idiomatic list comprehension desugaring (head . findIndices (elem x) &&& head . head . filter (/=[]) . map (elemIndices x)) m
Comments bubble
idiomatic list comprehension desugaring (head . findIndices (elem x) &&& head . head . filter (/=[]) . map (elemIndices x)) m
Code
procedure search(m:T2dMatrix; x:TElement; out i,j:integer);
begin
   for i := 0 to high(m) do
	for j := 0 to high(m[i]) do
            if m[i,j] = x then
               exit;
   i := -1;
   j := -1;
end;
Code
procedure search(m:T2dMatrix; x:TElement; out i,j:integer);
begin
   for i := 0 to high(m) do
	for j := 0 to high(m[i]) do
            if m[i,j] = x then
               exit;
   i := -1;
   j := -1;
end;
Code
sub search {
   my ($x, $m) = @_;
   while ( ($k1,$v1) = each @$m ) {
      while ( ($k2, $v2) = each @$v1 ) {
           return $k1, $k2 if $v2 == $x;
      }
   }
}
Code
sub search {
   my ($x, $m) = @_;
   while ( ($k1,$v1) = each @$m ) {
      while ( ($k2, $v2) = each @$v1 ) {
           return $k1, $k2 if $v2 == $x;
      }
   }
}
Code
function search(m, x) {
    for (var i = 0; i < m.length; i++) {
        for (var j = 0; j < m[i].length; j++) {
            if (m[i][j] == x) {
                return [i, j];
            }
        }
    }
    return false;
}
Code
function search(m, x) {
    for (var i = 0; i < m.length; i++) {
        for (var j = 0; j < m[i].length; j++) {
            if (m[i][j] == x) {
                return [i, j];
            }
        }
    }
    return false;
}
Comments bubble
Return an array if found, or false if not found.
Comments bubble
Return an array if found, or false if not found.
Demo URL
https://jsfiddle.net/vzw4577n/
Demo URL
https://jsfiddle.net/vzw4577n/
Code
static class Position{
	int i;
	int j;
}

Position search(int[][] m, int x){
	for(int i=0;i<m.length;i++)
		for(int j=0;j<m[i].length;j++)
			if(m[i][j] == x){
				Position pos= new Position();
				pos.i = i;
				pos.j = j;
				return pos;
			}
	return null;
}
Code
static class Position{
	int i;
	int j;
}

Position search(int[][] m, int x){
	for(int i=0;i<m.length;i++)
		for(int j=0;j<m[i].length;j++)
			if(m[i][j] == x){
				Position pos= new Position();
				pos.i = i;
				pos.j = j;
				return pos;
			}
	return null;
}
Comments bubble
A Java method returns 0 or 1 value only. So we have to create a custom class Position to hold the two values to return.
Comments bubble
A Java method returns 0 or 1 value only. So we have to create a custom class Position to hold the two values to return.
Demo URL
https://ideone.com/OpJmjY
Demo URL
https://ideone.com/OpJmjY
Code
func search(m [][]int, x int) (bool, int, int) {
	for i, _ := range m {
		for j, v := range m[i] {
			if v == x {
				return true, i, j
			}
		}
	}
	return false, 0, 0
}
Code
func search(m [][]int, x int) (bool, int, int) {
	for i, _ := range m {
		for j, v := range m[i] {
			if v == x {
				return true, i, j
			}
		}
	}
	return false, 0, 0
}
Comments bubble
Go functions may return multiple values.
This function returns 3 values : one to indicate if x was found or not, and two for the coordinates.
Comments bubble
Go functions may return multiple values.
This function returns 3 values : one to indicate if x was found or not, and two for the coordinates.
Demo URL
http://play.golang.org/p/JNCbBqWM16
Demo URL
http://play.golang.org/p/JNCbBqWM16