τau / cmp /

cmp.tau

source
/Users/niconex/Documents/tau/stdlib/cmp/cmp.tau
1# cmp - comparison and ordering of values.2#3# The == operator compares scalars by value and everything else by identity,4# like Go does with slices and maps. Equal is the structural comparison.56# Equal reports whether a and b hold the same value, walking lists, maps,7# objects and bytes.8Equal = fn(a, b) {9	ta = type(a)10	if ta != type(b) {11		return false12	}1314	if ta == "list" || ta == "bytes" {15		if len(a) != len(b) {16			return false17		}18		for i = 0; i < len(a); ++i {19			if !Equal(a[i], b[i]) {20				return false21			}22		}23		return true24	}2526	if ta == "map" || ta == "object" {27		ka = keys(a)28		if len(ka) != len(keys(b)) {29			return false30		}31		for i = 0; i < len(ka); ++i {32			if !Equal(a[ka[i]], b[ka[i]]) {33				return false34			}35		}36		return true37	}3839	return a == b40}4142# Compare returns -1 if a is less than b, 0 if they are equal, 1 otherwise.43# Only values that the < operator accepts can be compared.44Compare = fn(a, b) {45	if a < b {46		return -147	}48	if b < a {49		return 150	}51	return 052}5354Min = fn(a, b) { if a < b { a } else { b } }55Max = fn(a, b) { if a < b { b } else { a } }