Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -0,0 +1,52 @@
|
|||
--Comparing two strings for exact equality
|
||||
set s1 to "this"
|
||||
set s2 to "that"
|
||||
if s1 is s2 then
|
||||
-- strings are equal
|
||||
end if
|
||||
|
||||
--Comparing two strings for inequality (i.e., the inverse of exact equality)
|
||||
if s1 is not s2 then
|
||||
-- string are not equal
|
||||
end if
|
||||
|
||||
-- Comparing two strings to see if one is lexically ordered before than the other
|
||||
if s1 < s2 then
|
||||
-- s1 is lexically ordered before s2
|
||||
end if
|
||||
|
||||
-- Comparing two strings to see if one is lexically ordered after than the other
|
||||
if s1 > s2 then
|
||||
-- s1 is lexically ordered after s2
|
||||
end if
|
||||
|
||||
-- How to achieve both case sensitive comparisons and case insensitive comparisons within the language
|
||||
set s1 to "this"
|
||||
set s2 to "This"
|
||||
|
||||
considering case
|
||||
if s1 is s2 then
|
||||
-- strings are equal with case considering
|
||||
end if
|
||||
end considering
|
||||
|
||||
ignoring case -- default
|
||||
if s2 is s2 then
|
||||
-- string are equal without case considering
|
||||
end if
|
||||
end ignoring
|
||||
|
||||
-- Demonstrate any other kinds of string comparisons that the language provides, particularly as it relates to your type system. For example, you might demonstrate the difference between generic/polymorphic comparison and coercive/allomorphic comparison if your language supports such a distinction.
|
||||
|
||||
-- When comparing the right object is coerced into the same type as the object left from the operator. This implicit coercion enables to compare integers with strings (containining integer values).
|
||||
|
||||
set s1 to "3"
|
||||
set int1 to 2
|
||||
|
||||
if s1 < int1 then
|
||||
-- comparison is lexically
|
||||
end if
|
||||
|
||||
if int1 < s1 then
|
||||
-- comparison is numeric
|
||||
end if
|
||||
39
Task/String-comparison/C++/string-comparison.cpp
Normal file
39
Task/String-comparison/C++/string-comparison.cpp
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
template <typename T>
|
||||
void demo_compare(const T &a, const T &b, const std::string &semantically) {
|
||||
std::cout << a << " and " << b << " are " << ((a == b) ? "" : "not ")
|
||||
<< "exactly " << semantically << " equal." << std::endl;
|
||||
|
||||
std::cout << a << " and " << b << " are " << ((a != b) ? "" : "not ")
|
||||
<< semantically << "inequal." << std::endl;
|
||||
|
||||
std::cout << a << " is " << ((a < b) ? "" : "not ") << semantically
|
||||
<< " ordered before " << b << '.' << std::endl;
|
||||
|
||||
std::cout << a << " is " << ((a > b) ? "" : "not ") << semantically
|
||||
<< " ordered after " << b << '.' << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// Case-sensitive comparisons.
|
||||
std::string a((argc > 1) ? argv[1] : "1.2.Foo");
|
||||
std::string b((argc > 2) ? argv[2] : "1.3.Bar");
|
||||
demo_compare<std::string>(a, b, "lexically");
|
||||
|
||||
// Case-insensitive comparisons by folding both strings to a common case.
|
||||
std::transform(a.begin(), a.end(), a.begin(), ::tolower);
|
||||
std::transform(b.begin(), b.end(), b.begin(), ::tolower);
|
||||
demo_compare<std::string>(a, b, "lexically");
|
||||
|
||||
// Numeric comparisons; here 'double' could be any type for which the
|
||||
// relevant >> operator is defined, eg int, long, etc.
|
||||
double numA, numB;
|
||||
std::istringstream(a) >> numA;
|
||||
std::istringstream(b) >> numB;
|
||||
demo_compare<double>(numA, numB, "numerically");
|
||||
return (a == b);
|
||||
}
|
||||
24
Task/String-comparison/ColdFusion/string-comparison-1.cfm
Normal file
24
Task/String-comparison/ColdFusion/string-comparison-1.cfm
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<cffunction name="CompareString">
|
||||
<cfargument name="String1" type="string">
|
||||
<cfargument name="String2" type="string">
|
||||
<cfset VARIABLES.Result = "" >
|
||||
<cfif ARGUMENTS.String1 LT ARGUMENTS.String2 >
|
||||
<cfset VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is less than '" & ARGUMENTS.String2 & "')" >
|
||||
</cfif>
|
||||
<cfif ARGUMENTS.String1 LTE ARGUMENTS.String2 >
|
||||
<cfset VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is less than or equal to '" & ARGUMENTS.String2 & "')" >
|
||||
</cfif>
|
||||
<cfif ARGUMENTS.String1 GT ARGUMENTS.String2 >
|
||||
<cfset VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is greater than '" & ARGUMENTS.String2 & "')" >
|
||||
</cfif>
|
||||
<cfif ARGUMENTS.String1 GTE ARGUMENTS.String2 >
|
||||
<cfset VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is greater than or equal to '" & ARGUMENTS.String2 & "')" >
|
||||
</cfif>
|
||||
<cfif ARGUMENTS.String1 EQ ARGUMENTS.String2 >
|
||||
<cfset VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is equal to '" & ARGUMENTS.String2 & "')" >
|
||||
</cfif>
|
||||
<cfif ARGUMENTS.String1 NEQ ARGUMENTS.String2 >
|
||||
<cfset VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is not equal to '" & ARGUMENTS.String2 & "')" >
|
||||
</cfif>
|
||||
<cfreturn VARIABLES.Result >
|
||||
</cffunction>
|
||||
24
Task/String-comparison/ColdFusion/string-comparison-2.cfm
Normal file
24
Task/String-comparison/ColdFusion/string-comparison-2.cfm
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<cfscript>
|
||||
function CompareString( String1, String2 ) {
|
||||
VARIABLES.Result = "";
|
||||
if ( ARGUMENTS.String1 LT ARGUMENTS.String2 ) {
|
||||
VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is less than '" & ARGUMENTS.String2 & "')";
|
||||
}
|
||||
if ( ARGUMENTS.String1 LTE ARGUMENTS.String2 ) {
|
||||
VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is less than or equal to '" & ARGUMENTS.String2 & "')";
|
||||
}
|
||||
if ( ARGUMENTS.String1 GT ARGUMENTS.String2 ) {
|
||||
VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is greater than '" & ARGUMENTS.String2 & "')";
|
||||
}
|
||||
if ( ARGUMENTS.String1 GTE ARGUMENTS.String2 ) {
|
||||
VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is greater than or equal to '" & ARGUMENTS.String2 & "')";
|
||||
}
|
||||
if ( ARGUMENTS.String1 EQ ARGUMENTS.String2 ) {
|
||||
VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is equal to '" & ARGUMENTS.String2 & "')";
|
||||
}
|
||||
if ( ARGUMENTS.String1 NEQ ARGUMENTS.String2 ) {
|
||||
VARIABLES.Result = VARIABLES.Result & "('" & ARGUMENTS.String1 & "' is not equal to '" & ARGUMENTS.String2 & "')";
|
||||
}
|
||||
return VARIABLES.Result;
|
||||
}
|
||||
</cfscript>
|
||||
19
Task/String-comparison/Elena/string-comparison.elena
Normal file
19
Task/String-comparison/Elena/string-comparison.elena
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#import system.
|
||||
#import extensions.
|
||||
|
||||
#symbol compareStrings = (:val1 :val2)
|
||||
[
|
||||
(val1 == val2)? [ console writeLine:"The strings ":val1:" and ":val2:" are equal". ].
|
||||
(val1 != val2)? [ console writeLine:"The strings ":val1:" and ":val2:" are not equal". ].
|
||||
(val1 > val2)? [ console writeLine:"The string ":val1:" is lexically after than ":val2. ].
|
||||
(val1 < val2)? [ console writeLine:"The string ":val1:" is lexically before than ":val2. ].
|
||||
(val1 >= val2)? [ console writeLine:"The string ":val1:" is not lexically before than ":val2. ].
|
||||
(val1 <= val2)? [ console writeLine:"The string ":val1:" is not lexically after than ":val2. ].
|
||||
].
|
||||
|
||||
#symbol program =
|
||||
[
|
||||
#var s1 := "this".
|
||||
#var s2 := "that".
|
||||
compareStrings :s1 :s2.
|
||||
].
|
||||
9
Task/String-comparison/Elixir/string-comparison.elixir
Normal file
9
Task/String-comparison/Elixir/string-comparison.elixir
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
s = "abcd"
|
||||
s == "abcd" #=> true
|
||||
s == "abce" #=> false
|
||||
s != "abcd" #=> false
|
||||
s != "abce" #=> true
|
||||
s > "abcd" #=> false
|
||||
s < "abce" #=> true
|
||||
s >= "abce" #=> false
|
||||
s <= "abce" #=> true
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
"a" -lt "b" # lower than
|
||||
"a" -eq "b" # equal
|
||||
"a" -gt "b" # greater than
|
||||
"a" -le "b" # lower than or equal
|
||||
"a" -ne "b" # not equal
|
||||
"a" -ge "b" # greater than or equal
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
"a" -eq "A"
|
||||
"a" -ceq "A"
|
||||
29
Task/String-comparison/Rust/string-comparison.rust
Normal file
29
Task/String-comparison/Rust/string-comparison.rust
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
use std::ascii::AsciiExt; // for case insensitives only
|
||||
|
||||
fn main() {
|
||||
// only same types can be compared
|
||||
// String and String or &str and &str
|
||||
// exception: strict equality and inequality also work on &str and String
|
||||
let a: &str = "abc";
|
||||
let b: String = "Bac".to_owned();
|
||||
|
||||
// Strings are coerced to &str when borrowed and needed
|
||||
if a == b { println!("The strings are equal") }
|
||||
if a != b { println!("The strings are not equal") }
|
||||
if a > &b { println!("The first string is lexically after the second") }
|
||||
if a < &b { println!("The first string is lexically before the second") }
|
||||
if a >= &b { println!("The first string is not lexically before the second") }
|
||||
if a <= &b { println!("The first string is not lexically after the second") }
|
||||
|
||||
// case-insensitives:
|
||||
|
||||
// equality
|
||||
// this avoids new allocations
|
||||
if a.eq_ignore_ascii_case(&b) { println!("Both strings are equal when ignoring case") }
|
||||
|
||||
// everything else, create owned Strings, then compare as above
|
||||
let a2 = a.to_ascii_uppercase();
|
||||
let b2 = b.to_ascii_uppercase();
|
||||
|
||||
// repeat checks
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue