Value range(FunctionArguments arguments, SymbolTable symbol_table) { arguments.check_num_args_minimum(2); arguments.check_num_args_maximum(3); int start = arguments.get_int_argument(0, null, null, symbol_table, round_double: true); int stop = arguments.get_int_argument(1, null, null, symbol_table, round_double: true); int step = arguments.get_int_argument(2, null, 1, symbol_table, round_double: true); return(new RangeValue(start, stop, step)); }
/// <summary> /// C# version of Python's dictionary::get(). /// </summary> static Value get(Dictionary <Value, Value> dict, FunctionArguments arguments, SymbolTable symbol_table) { arguments.check_num_args_minimum(1); arguments.check_num_args_maximum(2); var key = arguments.get_value_argument(0, null, null, symbol_table); var default_val = arguments.get_value_argument(1, null, NoneValue.NONE, symbol_table); if (dict.ContainsKey(key)) { return(dict[key]); } else { return(default_val); } }
Value min(FunctionArguments arguments, SymbolTable symbol_table) { arguments.check_num_args_minimum(1); var list_arg = arguments.get_list_arguments(0, symbol_table); // A single string is a special case if (list_arg.Count == 1 && list_arg[0].Type == ValueType.String) { // return the smallest character of this string string str = ((StringValue)list_arg[0]).value; if (str.Length == 0) { throw new Exception("min() is called on an empty sequence."); } char smallest_char = str[0]; for (int i = 1; i < str.Length; i++) { if (str[i] < smallest_char) { smallest_char = str[i]; } } return(new StringValue(smallest_char.ToString())); } // Standard case Value smallest = null; for (int i = 0; i < list_arg.Count; i++) { if (list_arg[i].Type == ValueType.List) { // Note: This is NOT standard Python behavior smallest = find_smallest_in_list((ListValue)list_arg[i], smallest); } else { if (smallest == null) { smallest = list_arg[i]; } else { // possible alternative: // var True = new DynamicValue(true); // if (list_arg[i].operate(OperatorType.Less, smallest).Equals(True)) if (ListValue.compare_function(list_arg[i], smallest) < 0) { smallest = list_arg[i]; } } } } if (smallest == null) { throw new Exception("min() is called on an empty sequence."); } return(ListValue.shallow_copy(smallest)); }