Logo

Programming-Idioms

History of Idiom 12 > diff from v24 to v25

Edit summary for version 25 by :

Version 24

2015-10-25, 19:12:25

Version 25

2015-10-29, 14:05:12

Idiom #12 Check if list contains a value

Check if list contains a value x.
list is any iterable finite container.

Idiom #12 Check if list contains a value

Check if list contains a value x.
list is any iterable finite container.

Code
x `Data.Foldable.elem` list
Code
x `Data.Foldable.elem` list
Doc URL
http://hackage.haskell.org/package/base-4.8.1.0/docs/Data-Foldable.html#v:elem
Doc URL
http://hackage.haskell.org/package/base-4.8.1.0/docs/Data-Foldable.html#v:elem
Imports
classes
Imports
classes
Code
result := list.IndexOf(x) <> -1;
Code
result := list.IndexOf(x) <> -1;
Comments bubble
If list is a TStrings-descendant
Comments bubble
If list is a TStrings-descendant
Code
result := false;
for e in list do 
	if e=x then
		begin
			result := true;
			break;
		end
Code
result := false;
for e in list do 
	if e=x then
		begin
			result := true;
			break;
		end
Imports
import std.algorithm.searching;
Imports
import std.algorithm.searching;
Code
bool here = canFind(items, x);
Code
bool here = canFind(items, x);
Comments bubble
canFind(haystack, needle)
Comments bubble
canFind(haystack, needle)
Code
in_array ($x, $list, true);
Code
in_array ($x, $list, true);
Comments bubble
in_array returns true if value exists in array $list

Last argument should be set to true. Otherwise unexpected behavior is very likely to happen, for details see php manual.
Comments bubble
in_array returns true if value exists in array $list

Last argument should be set to true. Otherwise unexpected behavior is very likely to happen, for details see php manual.
Code
list.contains(&x); // Or, for most iterable types, `list.iter().any(|v| v == &x)`
Code
list.contains(&x); // Or, for most iterable types, `list.iter().any(|v| v == &x)`
Comments bubble
Technically, the truly general version is `(&list).into_iter().any(|v| v == &x)` but this isn't idiomatic.
Comments bubble
Technically, the truly general version is `(&list).into_iter().any(|v| v == &x)` but this isn't idiomatic.
Code
list.indexOf(x)
Code
list.indexOf(x)
Comments bubble
Where list is an Array object. Result is -1 if the object doesn't exist, otherwise its index into the list.
Comments bubble
Where list is an Array object. Result is -1 if the object doesn't exist, otherwise its index into the list.
Imports
#include <vector>
#include <algorithm>
Imports
#include <vector>
#include <algorithm>
Code
bool Contains(const std::vector<int> list, const int x)
{
	if ( find( list.begin(), list.end(), x) == list.end() )
		return false;
	return true;
}
Code
bool Contains(const std::vector<int> list, const int x)
{
	if ( find( list.begin(), list.end(), x) == list.end() )
		return false;
	return true;
}
Doc URL
http://www.cplusplus.com/reference/algorithm/find/
Doc URL
http://www.cplusplus.com/reference/algorithm/find/
Code
func Contains(list []string, x string) bool {
	for _, item := range list {
		if item == x {
			return true
		}
	}
	return false
}
Code
func Contains(list []string, x string) bool {
	for _, item := range list {
		if item == x {
			return true
		}
	}
	return false
}
Comments bubble
Use a slice of your type, not necessary string.

You may use any type compatible with operator ==
Comments bubble
Use a slice of your type, not necessary string.

You may use any type compatible with operator ==
Demo URL
http://play.golang.org/p/5WJ2ZNQ9WP
Demo URL
http://play.golang.org/p/5WJ2ZNQ9WP