function c(a,b) local x,y = a:gmatch('%d+'), b:gmatch('%d+') for i=1,math.max(#a,#b) do local a = tonumber(x() or 0) local b = tonumber(y() or 0) if a ~= b then return a-b end end return 0 end
Name:
Anonymous2015-01-07 2:43
Io> Version[1.0.0] == Version[1] ==> true Io> // etc.
// Version.io (in cwd) Version := Object clone do( version ::= list() fromString := method(s, self clone setVersion(s split(".") \ map(asNumber) \ map(x, if(x isNan or(x isNil), 0, x))))
size := method(self version size) at := method(x, self version at(x)) push := method(x, self version push(x))
cmp := method(other, o := other clone s := self clone while(s size > o size, o push(0)) while(o size > s size, s push(0))
s version foreach(i,v, if(v > o at(i), return 1, if(v < o at(i), return -1))) 0) )
Version setSlot(">", method(o, self cmp(o) > 0)) Version setSlot(">=", method(o, self cmp(o) >= 0)) Version setSlot("<", method(o, self cmp(o) < 0)) Version setSlot("<=", method(o, self cmp(o) <= 0)) Version setSlot("==", method(o, self cmp(o) == 0))
Name:
Anonymous2015-01-07 3:02
Perl 5. [code] #!/usr/bin/env perl use strict; use warnings;
use 5.10.0;
# The subroutine itself sub cmpver { my $alen = my @a = split /\./, $_[0]; my $blen = my @b = split /\./, $_[1];
push @a, (0) x ($blen - $alen); push @b, (0) x ($alen - $blen);
for (0 .. @a - 1) { return +1 if $a[$_] > $b[$_]; return -1 if $a[$_] < $b[$_]; }
0; }
# Testing say cmpver('1.157.2821.9289', '1.05.00.0156.789'); # 1 (bigger) say cmpver('1.09.1' ,'1.0.2'); # 1 (bigger) say cmpver('1.5.3' ,'1.1'); # 1 (bigger) say cmpver('0.1.8' ,'1.1.1'); # -1 (smaller) say cmpver('1.1.1' ,'1.1.1'); # undef (equal) [code]
v :: String -> [Integer] v = map read . words . map sub
sub '.' = ' ' sub c = c
tests = [ v "1.05.00.0156" > v "1.0.221.9289" , v "1" < v "1.0.1" , v "1.0.1" < v "1.0.2" , v "1.0.2" < v "1.0.3" , v "1.0.3" < v "1.1" , v "1.1" < v "1.1.1" , v "1.1.1" < v "1.1.2" , v "1.1.2" < v "1.2" ]